求助
查看原帖
求助
118161
薛定谔的驴楼主2020/8/15 15:27
#include<bits/stdc++.h>
using namespace std;
const int N=1000000+100;
int d[N],vis[N],n,m,s,ans;
vector<int>v[N],v2[N];
vector<int>w[N],w2[N];
queue<int>q;
inline int read()
{
	int z=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')z=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return z*x;
}
void write(int a)
{
	if(a<0){putchar('-');a=-1;}
	if(a>9)write(a/10);
	putchar(a%10+'0');
}
void spfa(int s)
{
	d[s]=0,q.push(s),vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=0;i<v[u].size();i++)
		{
			int x=v[u][i];
			if(d[u]+w[u][i]<d[x])
			{
				d[x]=d[u]+w[u][i];
				if(vis[x]==0)
				{
					q.push(x);
					vis[x]=1;
				}
			}
		} 
	}
}
void spfa2(int s)
{
	d[s]=0,q.push(s),vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=0;i<v2[u].size();i++)
		{
			int x=v2[u][i];
			if(d[u]+w2[u][i]<d[x])
			{
				d[x]=d[u]+w2[u][i];
				if(vis[x]==0)
				{
					q.push(x);
					vis[x]=1;
				}
			}
		} 
	}
}
int main()
{
	int x,y,z;
	n=read();m=read();
	for(int i=1;i<=n;i++)d[i]=2147483647;
	for(int i=1;i<=m;i++)
	{
		x=read();y=read();z=read();
		v[x].push_back(y);w[x].push_back(z);
		v2[y].push_back(x);w2[y].push_back(z);
	} 
	spfa(1);
	for(int i=1;i<=n;i++)ans+=d[i],d[i]=2147483647;
	memset(vis,0,sizeof(vis));
	spfa2(1);
	for(int i=1;i<=n;i++)ans+=d[i];
	write(ans);
	return 0;
}
2020/8/15 15:27
加载中...