Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. 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 two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
4 4 1 2 100 2 4 200 2 3 250 3 4 100
450
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<long long,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct arc{ 18 int to,w; 19 arc(int x = 0,int y = 0){ 20 to = x; 21 w = y; 22 } 23 }; 24 const int maxn = 5010; 25 vector<arc>g[maxn]; 26 int d[maxn],d2[maxn],n,m; 27 priority_queue< pii,vector< pii >,greater< pii > >q; 28 void dijkstra(){ 29 for(int i = 0; i <= n; i++) d[i] = d2[i] = INF; 30 while(!q.empty()) q.pop(); 31 d[1] = 0; 32 q.push(make_pair(d[1],1)); 33 while(!q.empty()){ 34 int u = q.top().second; 35 int w = q.top().first; 36 q.pop(); 37 if(d2[u] < w) continue; 38 for(int i = 0; i < g[u].size(); i++){ 39 int dis = w + g[u][i].w; 40 if(d[g[u][i].to] > dis){ 41 swap(dis,d[g[u][i].to]); 42 q.push(make_pair(d[g[u][i].to],g[u][i].to)); 43 } 44 if(d2[g[u][i].to] > dis && d[g[u][i].to] < dis){ 45 d2[g[u][i].to] = dis; 46 q.push(make_pair(d2[g[u][i].to],g[u][i].to)); 47 } 48 } 49 } 50 } 51 int main(){ 52 int u,v,w; 53 while(~scanf("%d %d",&n,&m)){ 54 for(int i = 0; i <= n; i++) g[i].clear(); 55 for(int i = 0; i < m; i++){ 56 scanf("%d %d %d",&u,&v,&w); 57 g[u].push_back(arc(v,w)); 58 g[v].push_back(arc(u,w)); 59 } 60 dijkstra(); 61 printf("%d\n",d2[n]); 62 } 63 return 0; 64 }
邻接表
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<long long,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 100100; 18 struct arc { 19 int v,w,next; 20 arc(int x = 0,int y = 0,int z = 0) { 21 v = x; 22 w = y; 23 next = z; 24 } 25 }; 26 arc e[maxn<<1]; 27 int tot,n,m,d[5010],d2[5010],u[maxn],v[maxn],w[maxn],head[5010]; 28 void add(int x,int y,int z){ 29 e[tot] = arc(y,z,head[x]); 30 head[x] = tot++; 31 } 32 priority_queue< pii,vector< pii >,greater< pii > >q; 33 void dijkstra(){ 34 for(int i = 1; i <= n; i++) d[i] = d2[i] = INF; 35 while(!q.empty()) q.pop(); 36 d[1] = 0; 37 q.push(make_pair(d[1],1)); 38 while(!q.empty()){ 39 int x = q.top().second; 40 int y = q.top().first; 41 q.pop(); 42 for(int i = head[x]; ~i; i = e[i].next){ 43 int dis = y+e[i].w; 44 if(d[e[i].v] > dis){ 45 swap(d[e[i].v],dis); 46 d[e[i].v] = y+e[i].w; 47 q.push(make_pair(d[e[i].v],e[i].v)); 48 } 49 if(d2[e[i].v] > dis && d[e[i].v] < dis){ 50 d2[e[i].v] = dis; 51 q.push(make_pair(d2[e[i].v],e[i].v)); 52 } 53 } 54 } 55 } 56 int main() { 57 while(~scanf("%d %d",&n,&m)){ 58 memset(head,-1,sizeof(head)); 59 for(int i = 0; i < m; i++){ 60 scanf("%d %d %d",u+i,v+i,w+i); 61 add(u[i],v[i],w[i]); 62 add(v[i],u[i],w[i]); 63 } 64 dijkstra(); 65 printf("%d\n",d2[n]); 66 } 67 }
比较容易理解的写法:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<long long,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 100100; 18 struct arc { 19 int v,w,next; 20 arc(int x = 0,int y = 0,int z = 0) { 21 v = x; 22 w = y; 23 next = z; 24 } 25 }; 26 arc e[maxn<<1]; 27 int tot,n,m,d[5010][2],head[5010]; 28 void add(int x,int y,int z){ 29 e[tot] = arc(y,z,head[x]); 30 head[x] = tot++; 31 } 32 priority_queue< pii,vector< pii >,greater< pii > >q; 33 void dijkstra(int src,int k){ 34 for(int i = 1; i <= n; i++) d[i][k] = INF; 35 while(!q.empty()) q.pop(); 36 d[src][k] = 0; 37 q.push(make_pair(d[src][k],src)); 38 while(!q.empty()){ 39 int x = q.top().second; 40 int y = q.top().first; 41 q.pop(); 42 if(d[x][k] < y) continue; 43 for(int i = head[x]; ~i; i = e[i].next){ 44 if(d[e[i].v][k] > d[x][k]+e[i].w){ 45 d[e[i].v][k] = d[x][k]+e[i].w; 46 q.push(make_pair(d[e[i].v][k],e[i].v)); 47 } 48 } 49 } 50 } 51 int main() { 52 int u,v,w; 53 while(~scanf("%d %d",&n,&m)){ 54 memset(head,-1,sizeof(head)); 55 tot = 0; 56 for(int i = 0; i < m; i++){ 57 scanf("%d %d %d",&u,&v,&w); 58 add(u,v,w); 59 add(v,u,w); 60 } 61 dijkstra(1,0); 62 dijkstra(n,1); 63 int ans = INF; 64 for(int i = 0; i < tot; i += 2){ 65 int res = d[e[i].v][0] + d[e[i+1].v][1]+e[i].w; 66 if(res > d[n][0]) ans = min(ans,res); 67 res = d[e[i].v][1] + d[e[i+1].v][0]+e[i+1].w; 68 if(res > d[n][0]) ans = min(ans,res); 69 } 70 printf("%d\n",ans); 71 } 72 return 0; 73 }