QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611219#7992. 【模板】线段树DengDuckRE 0ms0kbC++201.7kb2024-10-04 19:57:022024-10-04 19:57:03

Judging History

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

  • [2024-10-04 19:57:03]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-04 19:57:02]
  • 提交

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];
vector<Edge>G[N];
LL D[N],Ans=Inf;
int n,m,Vis[N];
inline void Add(int x,int y,LL 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);
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!E[i].empty())D[i]=Inf;
	}
	for(int i=1;i<=n;i++)
	{
		if(!E[i].empty())Dij(i);
	}
	if(Ans==Inf)puts("-1");
	else printf("%lld\n",Ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

10 10
969575 741825 24903 1047319 450475 256145 1045323 479255 810659 768323
1 5 6 3034
2 1 10
2 1 9
2 1 4
1 3 6 126904
2 5 5
2 9 9
1 7 7 853094
1 4 9 1025178
2 5 8

output:


result: