Roadblocks
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21304 | Accepted: 7463 |
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
Line 1: Two space-separated integers: N and R
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
Line 1: The length of the second shortest path between node 1 and node N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Source
这道题目要求是给出一个无向图,求出从起始点1到终点N的次短路(就是比最短路短一点的那一条),如果是要求最短路就容易得多了,毕竟最短路的算法有那么多,随便拿来一个套一下就行了,但是这道次短路问题稍稍有一些绕,那么一条到点v的次短路怎么求呢?我们是不是可以通过另一条到u的次短路来加上u->v这条边的长度来得到呢,或者本身u->v这条边就不属于到点v的最短路,通过到u的最短路来加上u->v这条边来得到一条到点v的次短路。
所以我们可以通过同时维护一个最短路数组和一个次短路数组即可。
题目链接:http://poj.org/problem?id=3255
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
using namespace std;
const int MAX=1e5+5;
const int inf=(1<<31)-1;
bool book[5050];
struct node
{
int e,val;
node(int _e=0,int _val=0):e(_e),val(_val){}
};
vector<node> edge[5050];
pair<int,int> p;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
void Dijk(int dis[],int dis2[],int n)
{
for(int i=1;i<=n;i++){
dis[i]=inf;
dis2[i]=inf;
}
dis[1]=0;
q.push(make_pair(0,1));
while(!q.empty()){
p=q.top();
q.pop();
int v=p.second,dv=p.first;
for(int i=0;i<edge[v].size();i++){
int d=dv+edge[v][i].val;
if(dis[edge[v][i].e]>d){
swap(dis[edge[v][i].e],d);
q.push(make_pair(dis[edge[v][i].e],edge[v][i].e)); //将刚刚更新过最短路径的结点入队,以进行下一步的松弛
}
if(dis2[edge[v][i].e]>d&&dis[edge[v][i].e]<d){
dis2[edge[v][i].e]=d;
q.push(make_pair(dis2[edge[v][i].e],edge[v][i].e)); //进行过次短路更新的结果也需要入队,再进行一次松弛
}
}
}
}
int main()
{
int n,r,x,y,v;
int dis[5050],dis2[5050];
scanf("%d %d",&n,&r);
for(int i=1;i<=r;i++){
scanf("%d %d %d",&x,&y,&v);
edge[x].push_back(node(y,v));
edge[y].push_back(node(x,v));
}
Dijk(dis,dis2,n);
cout<<dis2[n]<<endl;
return 0;
}
还有一种传统的模式,不过需要单独对次短路进行一次松弛,才能真正找到次短路。
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
using namespace std;
const int MAX=1e5+5;
const int inf=(1<<31)-1;
bool book[5050];
struct node
{
int e,val;
node(int _e=0,int _val=0):e(_e),val(_val){}
};
vector<node> edge[5050];
void Dijk(int dis[],int dis2[],int n,int sx)
{
for(int i=1;i<=n;i++){
dis[i]=inf;
dis2[i]=inf;
}
dis[sx]=0;
for(int i=1;i<=n;i++){
int mindis=inf,minj=0;
for(int j=1;j<=n;j++){
if(!book[j]&&dis[j]<mindis){
mindis=dis[j];
minj=j;
}
}
book[minj]=true;
for(int j=0;j<edge[minj].size();j++){
int nex=edge[minj][j].e,d=dis[minj]+edge[minj][j].val;
if(!book[nex]&&dis[nex]>d){
swap(dis[nex],d);
}
if(dis2[nex]>d){ //根据最短路径进行次短路更新
dis2[nex]=d;
}
}
}
for(int i=1;i<=n;i++)book[i]=false;
for(int i=1;i<=n;i++){
int mindis=inf,minj=0;
for(int j=1;j<=n;j++){
if(!book[j]&&dis2[j]<mindis){
mindis=dis2[j];
minj=j;
}
}
book[minj]=true;
for(int j=0;j<edge[minj].size();j++){
int nex=edge[minj][j].e,d=dis2[minj]+edge[minj][j].val;
if(!book[nex]&&dis2[nex]>d)
dis2[nex]=d; //根据次短路进行次短路更新
}
}
}
int main()
{
int n,r,x,y,v;
int dis[5050],dis2[5050];
scanf("%d %d",&n,&r);
for(int i=1;i<=r;i++){
scanf("%d %d %d",&x,&y,&v);
edge[x].push_back(node(y,v));
edge[y].push_back(node(x,v));
}
Dijk(dis,dis2,n,1);
cout<<dis2[n]<<endl;
return 0;
}