题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18020 | Accepted: 6338 |
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
题意:给定n个点,r条双向路径。求1号点到n号点的次短路径。
思路:在dijstra的基础上加一个保存次短路的数组即可。更新条件为:此路径到n点的长度大于当前最短路,且小于当前次路。
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
// #define Reast1nPeace
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;
struct edge{
int to,len;
edge(int to,int len){
this->to = to , this->len = len;
}
};
struct node{
int id,len;
node(int id,int len){
this->id = id , this->len = len;
}
friend bool operator < (node x , node y){
return x.len > y.len;
}
};
int n,r;
vector<edge> G[5010];
int dis[5010];
int sdis[5010];
void dijkstra(){
priority_queue<node> pq;
memset(dis,INF,sizeof(dis));
memset(sdis,INF,sizeof(sdis));
dis[1] = 0;
pq.push(node(1,0));
while(!pq.empty()){
node t = pq.top() ; pq.pop();
int id = t.id ; int len = t.len;
if(sdis[id] < len) continue;
for(int i = 0 ; i<G[id].size() ; i++){
edge e = G[id][i];
int len2 = len+e.len;
if(dis[e.to] > len+e.len){
swap(dis[e.to] , len2);
pq.push(node(e.to , dis[e.to]));
}//更新最短路
if(sdis[e.to]>len2 && dis[e.to]<len2){
sdis[e.to] = len2;
pq.push(node(e.to, sdis[e.to]));
}//更新次短路
}
}
cout<<sdis[n]<<endl;
}
int main(){
#ifdef Reast1nPeace
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin>>n>>r;
for(int i = 1 ; i<=r ; i++){
int a,b,d;
cin>>a>>b>>d;
G[a].push_back(edge(b,d));
G[b].push_back(edge(a,d));
}
dijkstra();
return 0;
}