QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611114#7991. 最小环DengDuckRE 116ms68088kbC++201.8kb2024-10-04 19:27:132024-10-04 19:27:14

Judging History

你现在查看的是最新测评结果

  • [2024-10-04 19:27:14]
  • 评测
  • 测评结果:RE
  • 用时:116ms
  • 内存:68088kb
  • [2024-10-04 19:27:13]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define Edge pair<int,LL>
#define pLL pair<LL,int>
#define To first
#define W second
using namespace std;
const int N=3e5+5;
const LL Inf=1e18;
const int B=1e8;
set<Edge>E[N],R[N];
LL D[N],Ans=Inf;
int n,m,Vis[N],TOT;
inline void Add(int x,int y,int w){E[x].insert({y,w}),R[y].insert({x,w});}
inline void Del(int x,int y)
{
	E[x].erase(E[x].lower_bound({y,0}));
	R[y].erase(R[y].lower_bound({x,0}));
}
inline void Dij(int x)
{
	priority_queue<pLL,vector<pLL>,greater<pLL> >Q;
	Q.push({D[x]=0,x});
	vector<int>V;V.pb(x);
	
	while(!Q.empty())
	{
		int x=Q.top().W;Q.pop();
		if(Vis[x])continue;Vis[x]=1;
		for(Edge i:E[x])
		{
			TOT++;
			if(D[x]+i.W<D[i.To]&&D[x]+i.W<Ans)
				Q.push({D[i.To]=D[x]+i.W,i.To}),V.pb(i.To),TOT+=10;
		}
	}
	for(Edge i:R[x])Ans=min(Ans,D[i.To]+i.W);
	for(int i:V)D[i]=Inf,Vis[i]=0;
}
queue<int>Q;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1,x,y,w;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&w);
		if(x==y)Ans=min(Ans,w*1ll);
		else Add(x,y,w);
	}
	for(int i=1;i<=n;i++)Q.push(i);
	while(!Q.empty())
	{
		int x=Q.front();Q.pop();
		if(E[x].empty()&&R[x].size()==1)
		{
			int i=R[x].begin()->To;
			Q.push(i),Del(i,x);
		}
		
		if(R[x].empty()&&E[x].size()==1)
		{
			int i=E[x].begin()->To;
			Q.push(i),Del(x,i);
		}
			
		if(E[x].size()==1&&R[x].size()==1)
		{
			Edge A=*R[x].begin(),B=*E[x].begin();
			Del(A.To,x),Del(x,B.To);
			if(A.To==B.To)
			{
				Ans=min(Ans,A.W+B.W);
				Q.push(A.To);
				continue;
			}
			Add(A.To,B.To,A.W+B.W);
			Q.push(A.To),Q.push(B.To);
		}
	}
	vector<int>V;
	for(int i=1;i<=n;i++)
	{
		if(!E[i].empty())V.pb(i),D[i]=Inf;
	}
	random_shuffle(V.begin(),V.end());
	for(int i:V)
	{
		Dij(i);
		if(TOT>B)break;
	}
	if(Ans==Inf)puts("-1");
	else printf("%lld\n",Ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 34412kb

input:

4 6
1 2 1
4 3 3
4 1 9
2 4 1
3 1 2
3 2 6

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 3ms
memory: 35016kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 5ms
memory: 35156kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 116ms
memory: 68088kb

input:

258420 258419
33061 33062 767169384
212916 212917 1741339
229881 229882 896760805
173467 173468 273055172
233189 233190 800433307
10157 10158 126766550
174605 174606 552176083
224030 224031 886617880
229102 229103 783848581
67588 67589 510826095
233648 233649 879695751
214453 214454 867104578
153140...

output:

-1

result:

ok single line: '-1'

Test #5:

score: -100
Runtime Error

input:

248016 248896
82688 82755 592665252
202847 203260 294408121
26821 28237 269132335
150967 152178 3125829
246069 247390 29492546
7470 7673 55843492
33975 35414 442802995
28451 28948 193736988
34484 34637 84441058
60168 60309 501991354
79579 79844 26854803
239672 239706 111702667
73548 73658 149840530
...

output:


result: