1726: [Usaco2006 nov]roadblocks second short circuit time limit:5 Sec Memory limit:64 MB
submit:768 solved:369
[Submit] [Status] Description
Bessie moved home to a small farm, but she often went back to FJ's farm to visit her friends. Bessie liked the roadside scenery so much that she did not want to end her journey so quickly, so every time she returned to the farm, she chose the second shortest path, rather than the shortest one, as we used to. Bessie's Village has R (1<=r<=100,000) Two-way roads, each connecting one of the two of all N (1<=n<=5000) farms. Bessie lived on Farm 1, and her friends lived on Farm N (that is, Bessie's destination for every trip). The second short path that Betsy chooses can contain any road that appears in the shortest path, and a road can be repeated several times. Of course, the length of the second short circuit must be strictly greater than the shortest path (there may be more than one), but its length must not be greater than the length of all paths except the shortest route.
Input
* Line 1th: two integers, N and R, separated by a space
* 2nd. R+1 Line: Each line contains three integers a, b, and D separated by spaces, indicating that a road with a length of D (1 <= d <= 5000) is connected to farm A and farm B
Output
* Line 1th: Output An integer, that is, the length of the second short circuit from farm 1 to farm N
Sample Input4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output450
Output Description:
Shortest: 1--2--4 (length 100+200=300)
Second Short circuit: 1, 2, 3, 4 (length 100+250+100=450)HINT Source
Gold
Solution: A good idea is to start with 1 and N as the starting point to do a SPFA, so long as the enumeration of each edge with D[0][I]+D[1][E[J].GO]+E[J].W update the answer can be code:
1#include <cstdio>2#include <cstdlib>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <iostream>7#include <vector>8#include <map>9#include <Set>Ten#include <queue> One #defineINF 1000000000 A #defineMAXN 5000+100 - #defineMAXM 100000+100 - #definell Long Long the using namespacestd; - inline ll read () - { -ll x=0, f=1;CharCh=GetChar (); + while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} + returnx*F; A } at structedge{int from, Go,next,w;} e[2*MAXM]; - intn,m,s,tot,q[maxn],d[2][MAXN],HEAD[MAXN]; - BOOLV[MAXN]; - voidInsintXintYintz) - { -E[++tot].go=y;e[tot]. from=x;e[tot].w=z;e[tot].next=head[x];head[x]=tot; in } - voidInsertintXintYintz) to { + ins (x, y, z); ins (y,x,z); - } the voidSPFA (intt) * { $ for(intI=1; i<=n;++i) d[t][i]=inf;Panax Notoginsengmemset (V,0,sizeof(v)); - intL=0, r=1, x, y; theS= (t==0)?1: n;q[1]=s;d[t][s]=0; + while(l!=R) A { theX=Q[++L];if(L==MAXN) l=0; v[x]=0; + for(intI=head[x];i;i=e[i].next) - if(d[t][x]+e[i].w<d[t][y=E[i].go]) $ { $d[t][y]=d[t][x]+E[I].W; - if(!v[y]) {v[y]=1; q[++r]=y;if(R==MAXN) r=0;} - } the } - }Wuyi intMain () the { -Freopen ("Input.txt","R", stdin); WuFreopen ("output.txt","W", stdout); -N=read (); m=read (); About while(m--) $ { - intX=read (), Y=read (), z=read (); Insert (x, y, z); - } -SPFA (0); SPFA (1); A intans=inf; + for(intI=1; i<=n;i++) the { - for(intj=head[i];j;j=e[j].next) $ { the inttmp=d[0][i]+d[1][e[j].go]+E[J].W; the if(tmp!=d[0][n]) ans=min (ans,tmp); the } the } -printf"%d\n", ans); in return 0; the}View Code