QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#352637#7991. 最小环wjh213WA 289ms88088kbC++142.7kb2024-03-13 14:35:392024-03-13 14:35:40

Judging History

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

  • [2024-03-13 14:35:40]
  • 评测
  • 测评结果:WA
  • 用时:289ms
  • 内存:88088kb
  • [2024-03-13 14:35:39]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int,int>
#define fi first
#define se second
int const MAX=4e5+10;
int ru[MAX],chu[MAX];
vector<pr> V[MAX],V2[MAX];
int del[MAX],in[MAX];
vector<pr> E[MAX],E2[MAX];
int fan[MAX];
int vis[5005],dis[5005];
signed main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v,w;
		cin>>u>>v>>w;
		V[u].push_back({v,w});
		V2[v].push_back({u,w});
		chu[u]++;
		ru[v]++;
	}
	queue<int> shan;
	for(int i=1;i<=n;i++){
		if(ru[i]==1&&chu[i]==1)shan.push(i),in[i]=true;
		if(!ru[i]||!chu[i])shan.push(i),in[i]=true;
	}
	int ans=0x3f3f3f3f3f3f3f3f;
	while(!shan.empty()){
		//if(shan.size()>n)return -1;
		//cerr<<shan.size()<<"\n";
		int tp=shan.front();
		shan.pop();
		if(!ru[tp]||!chu[tp]){
			del[tp]=1;
			if(chu[tp]){
				for(auto q:V[tp]){
					int it=q.fi;
					if(del[it])continue;
					ru[it]--;
					if(ru[it]==0||chu[it]+ru[it]==2){
						if(!in[it]){
							shan.push(it);
							in[it]=true;
						}
					}
				}
			}
			if(ru[tp]){
				for(auto q:V2[tp]){
					int it=q.fi;
					if(del[it])continue;
					chu[it]--;
					if(chu[it]==0||chu[it]+ru[it]==2){
						if(!in[it]){
							shan.push(it);
							in[it]=true;
						}
					}
				}
			}
		}
		if(ru[tp]==chu[tp]&&chu[tp]==1){
			int s,t,w1=0,w2=0;
			for(auto q:V[tp]){
				int it=q.fi;
				if(del[it])continue;
				t=it;
				w1=q.se;
				break;
			}
			for(auto q:V2[tp]){
				int it=q.fi;
				if(del[it])continue;
				s=it;
				w2=q.se;
				break;
			}
			if(s==t&&t==tp){
				ans=min(ans,w1);
				del[tp]=true;
				continue;
			}
			del[tp]=true;
			V[s].push_back({t,w1+w2});
			V2[t].push_back({s,w1+w2});
		}
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		if(!del[i])fan[i]=++cnt;
	}
	for(int i=1;i<=n;i++){
		if(!del[i]){
			//cerr<<i<<"\n";
			for(auto it:V[i]){
				if(!del[it.fi]){
					E[fan[i]].push_back({fan[it.fi],it.se});
				}
			}
			for(auto it:V2[i]){
				if(!del[it.fi]){
					E2[fan[i]].push_back({fan[it.fi],it.se});
				}
			}
		}
	}
	//cerr<<" "<<cnt<<"\n";
	//for(int i=1;i<=cnt;i++){
	//	for(auto it:E2[i])cerr<<i<<" "<<it.fi<<" "<<it.se<<"\n";
	//}
	for(int i=1;i<=cnt;i++){
		priority_queue<pr,vector<pr>,greater<pr>> Q;
		memset(dis,0x3f,sizeof(dis));
		memset(vis,0,sizeof(vis));
		dis[i]=0;
		Q.push({0,i});
		while(!Q.empty()){
			int tp=Q.top().se;
			Q.pop();
			if(vis[tp])break;
			vis[tp]=true;
			for(auto it:E[tp]){
				if(vis[it.fi])continue;
				if(it.se+dis[tp]<dis[it.fi]){
					dis[it.fi]=it.se+dis[tp];
					Q.push({dis[it.fi],it.fi});
				}
			}
		}
		for(auto it:E2[i]){
			ans=min(ans,dis[it.fi]+it.se);
		}
	}
	cout<<(ans==0x3f3f3f3f3f3f3f3f?-1:ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 48540kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 7ms
memory: 48632kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 213ms
memory: 72764kb

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: 0
Accepted
time: 262ms
memory: 83880kb

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:

98674714245

result:

ok single line: '98674714245'

Test #6:

score: -100
Wrong Answer
time: 289ms
memory: 88088kb

input:

270530 271285
80489 81855 218173724
188930 190845 783975756
29830 30626 22189315
234320 234472 70840355
198096 198272 300313423
224194 226906 105128197
115010 115834 37228105
134788 135583 18647938
257292 257358 98569041
146988 147215 69398857
248752 250002 409565478
62128 63751 839744551
121918 122...

output:

136244513930

result:

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