QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#791614#7991. 最小环yhdddRE 4ms33880kbC++142.4kb2024-11-28 19:52:212024-11-28 19:52:26

Judging History

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

  • [2024-11-28 19:52:26]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:33880kb
  • [2024-11-28 19:52:21]
  • 提交

answer

// Problem: P10044 [CCPC 2023 北京市赛] 最小环
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P10044
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// Written by yhm.
// Start codeing:2024-11-28 18:06:20
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=200010;
const int inf=1e18;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
	return x*f;
}
bool Mbe;

int n,m,ans=inf;
set<pii> e[maxn],g[maxn];
queue<int> q;
int dis[maxn];
bool vis[maxn];
priority_queue<pii> qq;
vector<pii> fr[maxn],bk[maxn];
vector<int> p;
void work(){
	n=read();m=read();
	for(int i=1;i<=m;i++){
		int u=read(),v=read(),w=read();
		e[u].insert({v,w}),g[v].insert({u,w});
		if(u==v)ans=min(ans,w);
	}
	for(int u=1;u<=n;u++)if(!e[u].size()||!g[u].size())q.push(u);
	while(!q.empty()){
		int u=q.front();q.pop();
		if(e[u].size()){
			for(auto[v,w]:e[u]){
				g[v].erase({u,w});
				if(!e[v].size()||!g[v].size())q.push(v);
			}
			e[u].clear();
		}
		else{
			for(auto[v,w]:g[u]){
				e[v].erase({u,w});
				if(!e[v].size()||!g[v].size())q.push(v);
			}
			g[u].clear();
		}
	}
	for(int u=1;u<=n;u++)if(e[u].size()==1&&g[u].size()==1){
		auto[v,w1]=*e[u].begin();
		auto[w,w2]=*g[u].begin();
		g[v].erase({u,w1}),e[w].erase({u,w2});
		g[v].insert({w,w1+w2}),e[w].insert({v,w1+w2});
	}
	for(int u=1;u<=n;u++)if(e[u].size()&&g[u].size()){
		p.pb(u);
		for(auto[v,w]:e[u])bk[u].pb({v,w});
		for(auto[v,w]:g[u])fr[u].pb({v,w});
	}
	for(int s:p){
		for(int i:p)dis[i]=inf,vis[i]=0;dis[s]=0;qq.push({0,s});
		while(!qq.empty()){
			int u=qq.top().se;qq.pop();
			if(vis[u])continue;vis[u]=1;
			for(auto[v,w]:bk[u]){
				if(dis[v]>dis[u]+w){
					dis[v]=dis[u]+w,qq.push({-dis[v],v});
				}
			}
		}
		for(auto[v,w]:fr[s])ans=min(ans,dis[v]+w);
	}
	if(ans==inf)puts("-1");
	else printf("%lld\n",ans);
}

// \
444

bool Med;
int T;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	
//	cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
	
	T=1;
	while(T--)work();
}

詳細信息

Test #1:

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

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: 4ms
memory: 33684kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

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

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Runtime Error

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:


result: