次短路 poj 3255
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 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).
Input
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
Sample Output
450
Hint
Source
//#include <bits/stdc++.h> #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; #define maxn 100005 #define INF 9999999 typedef pair<int,int> pii; int d[100000+5],d2[100000+5]; vector<pii> e[maxn]; int n,k; void add_edge(int x,int y,int z) { e[x].push_back(make_pair(y,z)); e[y].push_back(make_pair(x,z)); } void init(int n) { for(int i=0;i<=n;i++) e[i].clear(); for(int i=0;i<=n;i++) d[i]=d2[i]=INF; } void dij() { priority_queue<pii,vector<pii>,greater<pii> > q; d[1]=0; q.push(make_pair(0,1)); while(!q.empty()) { pii p=q.top(); q.pop(); int dis=p.first; int now=p.second; if(d2[now]<dis) continue; for(int i=0;i<e[now].size();i++) { int u=e[now][i].first; int v=e[now][i].second; int dis2=dis+v; if(d[u]>dis2) { swap(d[u],dis2); q.push(make_pair(d[u],u)); } if(d[u]<dis2 && dis2<d2[u]) { d2[u]=dis2; q.push(make_pair(d2[u],u)); } } } cout<<d2[n]<<endl; } int main() { cin>>n>>k; init(n); for(int i=0;i<k;i++) { int x,y,z; cin>>x>>y>>z; add_edge(x,y,z); } dij(); return 0; }
poj运行结果:4888K 1594ms
另一种思路,我尝试用spfa做,因为一共n个点,就有n-1条最优的边,那么如果有一条边不是最优的,且这条边是其他中不优边中最短的,那么也形成了次短路,那么只需要对1
和n都spfa一次,然后次短路一定存在于 d1[i]+e[i][j]+d2[j] 和 d1[j]+e[i][j]+d2[i] 中的一个,接着枚举一遍就好了。
//#include <bits/stdc++.h> #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; #define maxn 100005 #define INF 9999999 typedef pair<int,int> pii; int d1[maxn+5],d2[maxn+5],vis[maxn+5]; vector<pii> e[maxn]; int n,k; void add_edge(int x,int y,int z) { e[x].push_back(make_pair(y,z)); e[y].push_back(make_pair(x,z)); } void init(int n) { for(int i=0; i<=n; i++) e[i].clear(); for(int i=0; i<=n; i++) d1[i]=d2[i]=INF; } void spfa(int s,int d[]) { queue<int>q; memset(vis,0,sizeof(vis)); q.push(s); d[s]=0; vis[s]=1; while(!q.empty()) { int now=q.front(); vis[now]=0; q.pop(); for(int i=0; i<e[now].size(); i++) { int v=e[now][i].first; if(d[v]>d[now]+e[now][i].second) { d[v]=d[now]+e[now][i].second; if(!vis[v]) { q.push(v); vis[v]=1; } } } } } int main() { cin>>n>>k; init(n); for(int i=0; i<k; i++) { int x,y,z; cin>>x>>y>>z; add_edge(x,y,z); } spfa(1,d1); spfa(n,d2); int ans=INF; for(int i=1; i<=n; i++) for(int j=0; j<e[i].size(); j++) { int u=e[i][j].first; int v=e[i][j].second; int t=d1[i]+v+d2[u]; if(t>d1[n] && t<ans) { ans=t; } } cout<<ans<<endl; return 0; }
poj运行结果:4888K 1532ms
两个方法的时间复杂度是一样的。。。。。反复多交了几次 第一种大概只比第二种多个6、70ms 果断还是第二种好233333,毕竟万一有负权边就gg了,而且第二种思路也简单一些。。。