bzoj1631[Usaco2007 Feb]Cow Party*
bzoj1631[Usaco2007 Feb]Cow Party
题意:
给一个带权有向图,和一个源点,求往返源点最短距离最长的点往返源点的最短距离。
题解:
正插边做spfa,倒着插边再做一次spfa。两次最短路之和最大值为所求。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 #define maxn 1010 7 #define INF 0x3ffffff 8 using namespace std; 9 10 inline int read(){ 11 char ch=getchar(); int f=1,x=0; 12 while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} 13 while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); 14 return f*x; 15 } 16 struct e{int t,w,n;}; e es[maxn*100]; int g[maxn],ess; 17 void pe(int f,int t,int w){es[++ess]=(e){t,w,g[f]}; g[f]=ess;} 18 int d[maxn],sm[maxn],f[maxn*100],t[maxn*100],w[maxn*100],n,m,s; bool inq[maxn]; 19 queue <int> q; 20 void spfa(){ 21 while(!q.empty())q.pop(); memset(inq,0,sizeof(inq)); inc(i,1,n)d[i]=INF; 22 q.push(s); inq[s]=1; d[s]=0; 23 while(!q.empty()){ 24 int x=q.front(); q.pop(); inq[x]=0; 25 for(int i=g[x];i;i=es[i].n)if(d[x]+es[i].w<d[es[i].t]){ 26 d[es[i].t]=d[x]+es[i].w; 27 if(!inq[es[i].t])inq[es[i].t]=1,q.push(es[i].t); 28 } 29 } 30 } 31 int main(){ 32 n=read(); m=read(); s=read(); inc(i,1,m)f[i]=read(),t[i]=read(),w[i]=read(); 33 ess=0; memset(g,0,sizeof(g)); inc(i,1,m)pe(f[i],t[i],w[i]); spfa(); inc(i,1,n)sm[i]+=d[i]; 34 ess=0; memset(g,0,sizeof(g)); inc(i,1,m)pe(t[i],f[i],w[i]); spfa(); inc(i,1,n)sm[i]+=d[i]; 35 int ans=0; inc(i,1,n)ans=max(ans,sm[i]); printf("%d",ans); return 0; 36 }
20160803