QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#352317#7991. 最小环Kevin5307WA 382ms100952kbC++203.0kb2024-03-13 09:20:352024-03-13 09:20:36

Judging History

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

  • [2024-03-13 09:20:36]
  • 评测
  • 测评结果:WA
  • 用时:382ms
  • 内存:100952kb
  • [2024-03-13 09:20:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using ll=long long;
set<int> G[400400];
ll f[400400],g[400400];
int u[400400],v[400400],tot;
map<int,int> E[400400];
int ind[300300];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	memset(f,0x3f,sizeof(f));
	memset(g,0x3f,sizeof(g));
	int n,m;
	cin>>n>>m;
	ll ans=0x3f3f3f3f3f3f3f3f;
	while(m--)
	{
		int u,v;
		ll w;
		cin>>u>>v>>w;
		if(u==v)
		{
			ans=min(ans,w);
			continue;
		}
		if(E[u].count(v))
		{
			int e=E[u][v];
			if(::u[e]==u)
				f[e]=min(f[e],w);
			else
				g[e]=min(g[e],w);
			G[u].insert(e);
			G[v].insert(e);
		}
		else
		{
			int e=++tot;
			f[e]=w;
			::u[e]=u;
			::v[e]=v;
			E[u][v]=E[v][u]=e;
			G[u].insert(e);
			G[v].insert(e);
		}
	}
	queue<int> q;
	for(int i=1;i<=n;i++)
		if(G[i].size()<=2)
			q.push(i);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		if(!G[x].size()) break;
		if(G[x].size()==1)
		{
			int e=*G[x].begin();
			int y=u[e]+v[e]-x;
			G[x].clear();
			G[y].erase(e);
			if(G[y].size()==2)
				q.push(y);
		}
		else
		{
			int e1=*G[x].begin();
			G[x].erase(e1);
			int e2=*G[x].begin();
			G[x].erase(e2);
			int y1=u[e1]+v[e1]-x;
			int y2=u[e2]+v[e2]-x;
			G[y2].erase(e2);
			if(u[e1]!=y1)
			{
				swap(u[e1],v[e1]);
				swap(f[e1],g[e1]);
			}
			if(u[e2]!=x)
			{
				swap(u[e2],v[e2]);
				swap(f[e2],g[e2]);
			}
			f[e1]+=f[e2];
			g[e2]+=g[e2];
			G[y2].insert(e1);
			v[e1]=y2;
		}
	}
	vector<int> vertex,edge;
	for(int i=1;i<=n;i++)
		if(G[i].size())
			vertex.push_back(i);
	for(auto x:vertex)
		for(auto e:G[x])
			if(x==u[e])
				edge.push_back(e);
	for(int i=0;i<vertex.size();i++)
		ind[vertex[i]]=i+1;
	for(auto ban:edge)
	{
		static ll dist[3030];
		memset(dist,0x3f,sizeof(dist));
		priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
		dist[ind[v[ban]]]=0;
		pq.emplace(0,v[ban]);
		while(!pq.empty())
		{
			int x=pq.top().second;
			ll d=pq.top().first;
			pq.pop();
			if(dist[ind[x]]!=d)
				continue;
			for(auto e:G[x])
			{
				int y=u[e]+v[e]-x;
				ll nd=d+(u[e]==x?f[e]:g[e]);
				if(nd<dist[ind[y]])
				{
					dist[ind[y]]=nd;
					pq.emplace(nd,y);
				}
			}
		}
		ans=min(ans,f[ban]+dist[ind[u[ban]]]);
	}
	for(auto e:edge)
		swap(f[e],g[e]);
	for(auto ban:edge)
	{
		static ll dist[3030];
		memset(dist,0x3f,sizeof(dist));
		priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
		dist[ind[v[ban]]]=0;
		pq.emplace(0,v[ban]);
		while(!pq.empty())
		{
			int x=pq.top().second;
			ll d=pq.top().first;
			pq.pop();
			if(dist[ind[x]]!=d)
				continue;
			for(auto e:G[x])
			{
				int y=u[e]+v[e]-x;
				ll nd=d+(u[e]==x?f[e]:g[e]);
				if(nd<dist[ind[y]])
				{
					dist[ind[y]]=nd;
					pq.emplace(nd,y);
				}
			}
		}
		ans=min(ans,f[ban]+dist[ind[u[ban]]]);
	}
	if(ans==0x3f3f3f3f3f3f3f3f)
	{
		cout<<"-1\n";
		return 0;
	}
	cout<<ans<<endl;
	return 0;
}

詳細信息

Test #1:

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

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

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

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

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 183ms
memory: 100952kb

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: 382ms
memory: 98232kb

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:

-9114861777324918563

result:

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