Roadblocks
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 8670 |
|
Accepted: 3138 |
Description
Bessie have moved to a small farm and sometimes enjoys returning to visit one of hers best friends. She does not want-to get-to-her-old home too quickly, because she likes the scenery along the. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1≤ R ≤100,000) bidirectional roads, each linking both of the N (1≤ N ≤5000) intersections, conveniently numbered 1.. n. Bessie starts at intersection 1, and she friend (the destination) is at intersectionN.
The Second-shortest path may share roads with any of the shortest paths, and it could backtrack i.e., use the same road or I Ntersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path (s) (i.e., if EST paths exist, the second-shortest path is the one whose length are longer than those but no longer than any other path).
Input
Line 1:two space-separated integers:
Nand
R
Lines 2..
R+1:each line contains three space-separated integers:
A,
B, and
DThat's describe a road that connects intersections
Aand
Band has length
D(1≤
D≤5000)
Output
Line 1:the length of the second shortest path between node 1 and node
N
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
Hint
3 (length 100+250+100=450), 4 (length 100+200=300) and 1, 2, routes:1, 2
Source
Usaco 2006 November Gold
AC Code
#include <stdio.h> #include <string.h> #include <queue> #include <iostream>using namespace std; #define INF 0xfffffffint n,m;struct s{int u,v,w,next;} Edge[200100];int head[5050],cnt,disr[5050],dis[5050],vis[5050],ans;void Add (int u,int v,int W) {edge[cnt].u=u;edge[ cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;} void init () {int i;for (i=0;i<=n;i++) {Dis[i]=inf;disr[i]=inf;}} void SPFA (int s,int *dis) {queue<int>q;memset (vis,0,sizeof (VIS));d Is[s]=0;vis[s]=1;q.push (s); while (!q.empty ( ) {int U=q.front (); Q.pop (); vis[u]=0;for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;int w=edge[i].w;if (dis) [v]>dis[u]+w) {dis[v]=dis[u]+w;if (!vis[v]) {Vis[v]=1;q.push (v);}}}} int main () {while (scanf ("%d%d", &n,&m)!=eof) {int I,j;memset (head,-1,sizeof (head)); Cnt=0;init (); for (i=0;i <m;i++) {int u,v,w;scanf ("%d%d%d", &u,&v,&w); add (u,v,w); add (v,u,w);} SPFA (1,dis); SPFA (N,DISR); Ans=inf;for (i=1;i<=n;i++) {for (j=head[i];j!=-1;j=edge[j].next) {int v=edge[j].v;inT w=edge[j].w;int temp=dis[i]+disr[v]+w;if (Temp>dis[n]&&temp<ans) ans=temp;} printf ("%d\n", ans);}}
POJ topic 3255Roadblocks (secondary short circuit)