Topic Connection:
http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723
Test instructions
Give you an n-point m-Graph edge
1 to n There are multiple shortest circuits, asking you what the sum of all the edges
Exercises
SPFA on the shortest path of single source for 1,n
Enumerates whether an edge is the shortest edge
i.e. Benquan +disa[i] + disb[i] = Shortest circuit length, is the answer
#include <bits/stdc++.h>using namespacestd;Const intN = 1e6+ -, M = 1e6+Ten, mod = 1e9+7, INF = 1e9+ +; typedefLong Longll;intHead[n],t,vis[n],dis[n],n,m,disa[n],disb[n];structedge{intTo,value,next;} e[n*2];voidAddintUintVintW) {e[t].next=head[u],e[t].to=v,e[t].value=w;head[u]=t++;}voidinit () {T=0; memset (Head,-1,sizeof(head));}voidSPFA (intu) { for(intI=1; i<=n;i++) Dis[i] = inf, vis[i] =0; vis[1] =1; Dis[u]=0; Queue<int>Q; Q.push (U); while(!Q.empty ()) { intK =Q.front (); Q.pop (); VIS[K]=0; for(inti=head[k];i!=-1; i=E[i].next) { intto =e[i].to; if(Dis[k] + E[i].value <Dis[to]) {Dis[to]= Dis[k] +E[i].value; if(!Vis[to]) {Q.push (to); Vis[to]=1; } } } }}intMain () { while(~SCANF ("%d%d",&n,&m)) {init (); for(intI=1; i<=m;i++) { intA,b,c; scanf ("%d%d%d",&a,&b,&c); A++,b++; Add (A,B,C); Add (B,A,C); } SPFA (1); for(intI=1; i<=n;i++) Disa[i] =Dis[i]; SPFA (n); for(intI=1; i<=n;i++) Disb[i] =Dis[i]; intMX = dis[1]; ll ans=0; for(intI=1; i<=n;i++) { for(intj=head[i];j!=-1; j=E[j].next) { intto =e[j].to; intValue =E[j].value; if(Disa[i] + disb[to] + value = = mx) ans+=value; }} printf ("%lld\n", 2ll*ans); }}
Uvalive 6885 Flowery Trails shortest-circuit enumeration