POJ 3255(次短路+SPFA)
2017-08-08 09:29
459 查看
Roadblocks
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
Sample Output
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Source
USACO 2006 November Gold
题意:给出N个点和R条边,求1到N的次短路。
分析:首先,最短路很容易求,只要用SPFA跑一遍就可以了,然后至于次短路,肯定有一条边是比最短路中的边要长的,然后我们就是要枚举不在最短路中的最小边,这个最小边必定是出现在这R条边之内。从1开始跑一遍SPFA,再从N开始跑一边SPFA,枚举R条边的起始点,求出比最短路长的最小值。
代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15411 | Accepted: 5427 |
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
USACO 2006 November Gold
题意:给出N个点和R条边,求1到N的次短路。
分析:首先,最短路很容易求,只要用SPFA跑一遍就可以了,然后至于次短路,肯定有一条边是比最短路中的边要长的,然后我们就是要枚举不在最短路中的最小边,这个最小边必定是出现在这R条边之内。从1开始跑一遍SPFA,再从N开始跑一边SPFA,枚举R条边的起始点,求出比最短路长的最小值。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<queue> using namespace std; int n,m; int d1[5005]; int d2[5005]; struct node{ int x,y,w; }; struct Node{ int v,w; }; vector<Node> vec[5005]; void spfa(int flag,int cur){ queue<int> q; q.push(cur); int temp[5005]; memset(temp,0x3f3f3f3f,sizeof(temp)); int id[5005]; memset(id,0,sizeof(id)); temp[cur]=0; id[cur]=1; while(q.empty()==0){ int u=q.front(); int len=vec[u].size(); q.pop(); id[u]--; for(int i=0;i<len;i++){ int v=vec[u][i].v; if(temp[v]>temp[u]+vec[u][i].w){ temp[v]=temp[u]+vec[u][i].w; if(id[v]==0){ id[v]++; q.push(v); } } } } if(flag==1){ for(int i=1;i<=n;i++){ d1[i]=temp[i]; } } else if(flag==2){ for(int i=1;i<=n;i++){ d2[i]=temp[i]; } } } int main(){ //freopen("test.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ int u,v,w;; node a[100005]; for(int i=1;i<=m;i++){ Node temp; scanf("%d%d%d",&u,&v,&w); temp.v=v; temp.w=w; vec[u].push_back(temp); temp.v=u; vec[v].push_back(temp); a[i].x=u; a[i].y=v; a[i].w=w; } memset(d1,0x3f3f3f3f,sizeof(d1)); memset(d2,0x3f3f3f3f,sizeof(d2)); spfa(1,1); spfa(2,n); int ans=0x3f3f3f3f; //printf("%d\n",d2[1]); for(int i=1;i<=m;i++){ int u=a[i].x; int v=a[i].y; int w=a[i].w; //printf("%d %d %d\n",u,v,w); if(d1[u]+d2[v]+w>d1 ){ ans=min(ans,d1[u]+d2[v]+w); } if(d2[u]+d1[v]+w>d1 ){ ans=min(ans,d2[u]+d1[v]+w); } } printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ3255 - Roadblocks - 次短路(spfa+LLL优化)
- POJ 3255 Roadblock(SPFA寻找次短路)
- poj 3255 Roadblocks(无向图次短路 SPFA)
- poj 3255 Roadblocks 次短路 spfa 解题报告
- poj 3255 spfa(求次短路)
- POJ 3255 Roadblocks 次短路 SPFA
- poj 3255 次短路(第k短路) A* + spfa 或 dijkstra
- 【POJ】3255 Roadblocks(次短路+spfa)
- poj 2449 求第k最短路 A* + SPFA
- Light OJ 1099 && poj 3255(次最短路)
- poj 3072 Robot 最短路spfa
- poj 3255(次短路)
- POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)
- poj 3255 Roadblocks (SPFA)
- POJ 1511(邻接表和最短路 SPFA)
- POJ 3255 Wormholes(最短路最负环)
- POJ 3259 Wormholes (最短路 SPFA 判断负环)
- poj2449 Remmarguts' Date --- k短路模板(SPFA+A*)
- poj3255-Roadblocks-次短路
- POJ - 1511 Invitation Cards (SPFA最短路)