QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#610992#7991. 最小环DengDuckRE 128ms68564kbC++201.7kb2024-10-04 18:40:152024-10-04 18:40:16

Judging History

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

  • [2024-10-04 18:40:16]
  • 评测
  • 测评结果:RE
  • 用时:128ms
  • 内存:68564kb
  • [2024-10-04 18:40:15]
  • 提交

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;
set<Edge>E[N],R[N];
LL D[N],Ans=Inf;
int n,m,Vis[N];
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])
		{
			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);
		}
	}
	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())
		{
			set<Edge>V=R[x];
			for(Edge i:V)Q.push(i.To),Del(i.To,x);
		}
		
		if(R[x].empty())
		{
			set<Edge>V=E[x];
			for(Edge i:V)Q.push(i.To),Del(x,i.To);
		}
			
		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);
		}
	}
	for(int i=1;i<=n;i++)D[i]=Inf;
	for(int i=1;i<=n;i++)
	{
		if(E[i].empty())continue;
		Dij(i);
	}
	if(Ans==Inf)puts("-1");
	else printf("%lld\n",Ans);
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 33972kb

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: 33564kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 6ms
memory: 35412kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 128ms
memory: 68564kb

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: