QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#352376#7991. 最小环lexiyvvWA 73ms41388kbC++141.8kb2024-03-13 10:37:262024-03-13 10:37:30

Judging History

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

  • [2024-03-13 10:37:30]
  • 评测
  • 测评结果:WA
  • 用时:73ms
  • 内存:41388kb
  • [2024-03-13 10:37:26]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,fa[300005],sp[300005],ord[300005],tot,tg[300005];
vector<pair<int,int> >v[300005],v2[300005],to[300005];
int dis[5005],vis[5005],ans,InF=1e18,I;
struct Node{
	int x,y,w;
};
vector<Node>e;
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void dfs(int x,int y){
	if(sp[x]&&x!=I){
//		cout<<I<<"->"<<ord[x]<<endl;
		v2[I].push_back({ord[x],y});
		return ;
	}
	for(auto i:v[x]){
		dfs(i.first,y+i.second);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ans=InF;
	cin>>n>>m;
	for(int i=1;i<=n;i++)fa[i]=i,tg[i]=-1;
	for(int i=1;i<=m;i++){
		int x,y,w;
		cin>>x>>y>>w;
		if(x==y){
			ans=min(ans,w);
			continue;
		}
		if(find(x)!=find(y)){
			fa[find(x)]=find(y);
			v[x].push_back({y,w});
		}
		else{
			sp[x]=sp[y]=1;
			e.push_back({x,y,w});
		}
	}
	for(int i=1;i<=n;i++){
		if(sp[i])ord[i]=++tot;
	}
	for(int i=1;i<=n;i++){
		if(sp[i])I=ord[i],dfs(i,0);
	}
	for(auto i:e){
		int x=i.x,y=i.y,w=i.w;
//		cout<<x<<' '<<y<<' '<<w<<endl;
		v2[ord[x]].push_back({ord[y],w});
		to[ord[y]].push_back({ord[x],w});
	}
	for(int s=1;s<=tot;s++){
		priority_queue<pair<int,int> >pq;
		pq.push({0,s});
		memset(vis,0,sizeof(vis));
		memset(dis,0x3f,sizeof(dis));
		dis[s]=0;
		while(!pq.empty()){
			int x=pq.top().second;pq.pop();
			if(vis[x])continue;
			vis[x]=1;
			for(auto i:v2[x]){
				int y=i.first,w=i.second;
				if(dis[y]>dis[x]+w){
					dis[y]=dis[x]+w;
					pq.push({-dis[y],y});
				}
			}
		}
		for(auto i:to[s]){
			int t=i.first;
//			cout<<s<<' '<<t<<":"<<i.second<<' '<<dis[t]<<endl;
			ans=min(ans,i.second+dis[t]);
		}
	}
	cout<<(ans>=1e16?-1:ans)<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 30724kb

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: 2ms
memory: 26988kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 28020kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 65ms
memory: 40000kb

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
Wrong Answer
time: 73ms
memory: 41388kb

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:

-1

result:

wrong answer 1st lines differ - expected: '98674714245', found: '-1'