Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7149 | Accepted: 2663 |
Description
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 intersectionN.
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).
Input
Output
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
Sample Output
450
Hint
Source
#include<queue> #include<vector> #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int INF=1000000000; const int max_e=200000+5; typedef pair<int,int>P; struct edge{ int to,dis; edge(int to,int dis){ this -> to = to; this -> dis = dis; } }; int N,R; int a,b,c; int dis[5005]; //记录最短路径 int disc[5005]; //记录次短路径 vector<edge>G[max_e]; void dijkstra(){ fill(dis+1,dis+N+1,INF); fill(disc+1,disc+N+1,INF); priority_queue<P,vector<P>,greater<P> >q; dis[1]=0; q.push(P(0,1)); while(q.size()){ P p=q.top(); q.pop(); int dd=p.first; int v=p.second; if(disc[v]<dd) continue; for(int i=0;i<G[v].size();i++){ edge& e=G[v][i]; int d=dd+e.dis; if(dis[e.to]>d){ swap(dis[e.to],d); q.push(P(dis[e.to],e.to)); } if(disc[e.to]>d&&dis[e.to]<d){ disc[e.to]=d; q.push(P(disc[e.to],e.to)); } } } cout<<disc[N]<<endl; } int main() { scanf("%d%d",&N,&R); for(int i=1;i<=R;i++){ scanf("%d%d%d",&a,&b,&c); G[a].push_back(edge(b,c)); G[b].push_back(edge(a,c)); } dijkstra(); return 0; }
#include<queue> #include<vector> #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int INF=1000000000; const int max_e=200000+5; typedef pair<int,int>P; struct edge{ int to,dis; edge(int to,int dis){ this -> to = to; this -> dis = dis; } }; struct dd{ int x,y; int sum; }an[200005]; int N,R; int a,b,c; int d1[5005]; int d2[5005]; vector<edge>G[max_e]; void dijkstra(int dis[],int s){ fill(dis+1,dis+N+1,INF); priority_queue<P,vector<P>,greater<P> >q; dis[s]=0; q.push(P(0,s)); while(q.size()){ P p=q.top(); q.pop(); int v=p.second; if(dis[v]<p.first) continue; for(int i=0;i<G[v].size();i++){ edge& e=G[v][i]; if(dis[e.to]>dis[v]+e.dis){ dis[e.to]=dis[v]+e.dis; q.push(P(dis[e.to],e.to)); } } } } int main() { int K=0; scanf("%d%d",&N,&R); for(int i=1;i<=R;i++){ scanf("%d%d%d",&a,&b,&c); G[a].push_back(edge(b,c)); G[b].push_back(edge(a,c)); an[K].x=a; an[K].y=b; an[K].sum=c; K++; an[K].x=b; an[K].y=a; an[K].sum=c; K++; } dijkstra(d1,1); dijkstra(d2,N); int ans=INF; //cout<<ans<<" "<<d1[N]<<endl; for(int i=0;i<K;i++){ int aa=d1[an[i].x]+d2[an[i].y]+an[i].sum; if(aa>d1[N]){ ans=min(ans,aa); } }cout<<ans<<endl; return 0; }