POJ3255(次短路大法好,dij大法好)
2017-03-27 11:53
302 查看
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
Sample Output
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
对于次短路来说每次更新最短路时保存次短路就可以,同样的思路还可以求出次3短次4短。。。。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int N=100000+10;
const int INF=100000000;
typedef pair <int ,int > P;
int n,r;
int dist
, dist2
;
struct Edge
{
int to,cost;
Edge(int To,int Cost):to(To),cost(Cost){}
};vector<Edge> G
;
void addedge (int u,int v,int w)
{
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
void solve()
{
priority_queue< P , vector<P> , greater<P> > que;
fill (dist ,dist+n,INF);
fill (dist2,dist2+n,INF);
dist[0]=0;
que.push(P(0,0));
while(!que.empty())
{
P u=que.top();que.pop();
int v=u.second,d=u.first;
if(dist2[v]<d) continue;
for(int i=0;i<G[v].size();i++)
{
Edge &e=G[v][i];
int d2=d+e.cost;
if(dist[e.to]>d2)
{
swap(dist[e.to],d2);
que.push(P(dist[e.to],e.to));
}
if(dist2[e.to]>d2&&dist[e.to]<d2)
{
dist2[e.to]=d2;
que.push(P(dist2[e.to],e.to));
}
}
}
printf("%d\n",dist2[n-1]);
}
int main()
{
cin >> n >> r;
for(int i=0;i<r;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
u--;
v--;
addedge(u,v,cost);
}
solve();
return 0;
}
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)
对于次短路来说每次更新最短路时保存次短路就可以,同样的思路还可以求出次3短次4短。。。。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int N=100000+10;
const int INF=100000000;
typedef pair <int ,int > P;
int n,r;
int dist
, dist2
;
struct Edge
{
int to,cost;
Edge(int To,int Cost):to(To),cost(Cost){}
};vector<Edge> G
;
void addedge (int u,int v,int w)
{
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
void solve()
{
priority_queue< P , vector<P> , greater<P> > que;
fill (dist ,dist+n,INF);
fill (dist2,dist2+n,INF);
dist[0]=0;
que.push(P(0,0));
while(!que.empty())
{
P u=que.top();que.pop();
int v=u.second,d=u.first;
if(dist2[v]<d) continue;
for(int i=0;i<G[v].size();i++)
{
Edge &e=G[v][i];
int d2=d+e.cost;
if(dist[e.to]>d2)
{
swap(dist[e.to],d2);
que.push(P(dist[e.to],e.to));
}
if(dist2[e.to]>d2&&dist[e.to]<d2)
{
dist2[e.to]=d2;
que.push(P(dist2[e.to],e.to));
}
}
}
printf("%d\n",dist2[n-1]);
}
int main()
{
cin >> n >> r;
for(int i=0;i<r;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
u--;
v--;
addedge(u,v,cost);
}
solve();
return 0;
}
相关文章推荐
- POJ3255 Roadblocks dij求次短路 [模板]
- POJ 3159 最短路dij优先队列模板 差分约束
- POJ2457 dij最短路也可以做
- ZOJ 2750 Idiomatic Phrases Game(最短路,dij)
- 1088: 最短路(SPFA算法 &dij)
- poj3255 次短路裸题
- dij最短路优先队列堆的时候,加边
- POJ2457 dij最短路也可以做
- CSU 1808 地铁【思维建图+最短路Dij+Heap】
- POJ3255 Roadblocks , 次短路
- POJ1062->最短路Dij
- hdu3832-Earth Hour-最短路dij
- poj3255 dijsktra求次短路经
- poj3255 Roadblocks【次短路】
- 最短路裸题hdu-2112-DIJ
- hdu 3873 Invade the Mars(dij变体,带限制最短路)
- Dij二级最短路
- 蒟蒻的图论总结(2):用Floyd、Dij.和SPFA解决最短路
- hdu2544最短路dij+stl 实现heap
- 愚者指名自己的辩护人_纪中2046_最短路_维包一生推,gosick大法好!