Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11335 | Accepted: 3999 |
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
本题目的意思就是要求次短路。我用两种方法求解:
(一)利用dijkstra算法进行适当修改,到某个顶点v的次短路:(1)其他某个顶点u的最短路加上u->v的边长;(2)其他某个顶点u的次短路加上u->v的边长。所以我们要求出到所有
顶点的最短路和次短路。因此对每个顶点,同时记录最短距离和次短距离(开两个数组记录,用dijkstra算法不断更新)。
此解法为《挑战程序设计竞赛》的例题
#include <cstdio> #include <vector> #include <queue> #include <algorithm> #include <cmath> #include <cstring> using namespace std; const int maxn = 200000 + 5; const int INF = 1000000000; typedef pair<int, int> P; int N, R; struct edge { int to; int cost; edge(int to, int cost){ this -> to = to; this -> cost = cost; } }; vector<edge> G[maxn]; int dis[5005]; //最短距离 int disc[5005]; //次短距离 void dijkstra() { fill(dis + 1, dis + N + 1, INF); fill(disc + 1, disc + N + 1, INF); priority_queue<P, vector<P>, greater<P> > que; dis[1] = 0; que.push(P(0, 1)); while (!que.empty()){ P p = que.top(); que.pop(); int v = p.second, dd = p.first; if (disc[v] < dd) continue; for (int i = 0; i < G[v].size(); i++){ edge& e = G[v][i]; int d = dd + e.cost; if (dis[e.to] > d){ swap(dis[e.to], d); que.push(P(dis[e.to], e.to)); } if (disc[e.to] > d && dis[e.to] < d){ disc[e.to] = d; que.push(P(disc[e.to], e.to)); } } } printf("%d\n", disc[N]); } int main() { int a, b, c; 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; }