poj-3255-Roadblocks-路径可重复次短路

题目:

Roadblocks
Time Limit: 2000MS        Memory Limit: 65536K
Total Submissions: 7075        Accepted: 2629
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)

题目

题意:无向图。 起点为1, 终点为 n, 输出仅次于 最短路的次短路, 路径可重复走。

思路:正反求两遍最短路,得出1 到所有点的最短距离, 和 n到所有点的最短距离。因为路径可以重复走,所以两点之间只有一条边的可以重复经过。接着将每条边视作单项边,枚举每条单项边e(u, v),Dist = dist(1, u) + dist(n, v) + dist(u,v).这样只会重复一条最小边。所求得的 大于 dist(1, n)的最小的Dist,就是题目的解。(理解不了的画一下图就明白了)

AC代码:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<queue>
  4 #include<vector>
  5 #include<algorithm>
  6 #include<cstring>
  7 #include<climits>
  8 using namespace std;
  9 #define maxn 100009
 10 #define INF INT_MAX-100000
 11 struct Edge
 12 {
 13    int from, to, dist;
 14 };
 15
 16 struct Heapnode
 17 {
 18    int d, u;
 19    bool operator < (const Heapnode& that) const {
 20       return d > that.d;
 21    }
 22 };
 23
 24 int dist1[maxn], dist2[maxn];
 25 struct Dijkstra
 26 {
 27    int n, m;
 28    vector<Edge> edges;
 29    vector<int> G[maxn];
 30    bool done[maxn];
 31    int d[maxn];
 32    int p[maxn];
 33
 34    void init(int n) {
 35       this->n = n;
 36       for(int i = 0; i < n; i++) G[i].clear();
 37       edges.clear();
 38    }
 39
 40    void AddEdge(int from, int to , int dist) {
 41       edges.push_back((Edge) {from, to , dist});
 42       m = edges.size();
 43       G[from].push_back(m-1);
 44    }
 45
 46    void dijkstra(int s, int* dx) {
 47       priority_queue<Heapnode> Q;
 48       for(int i = 0; i < n; i++) dx[i] = d[i] = INF;
 49       d[s] = dx[s] = 0;
 50       memset(done, 0, sizeof(done));
 51       Q.push((Heapnode) {0, s});
 52       while(!Q.empty()) {
 53          Heapnode x = Q.top(); Q.pop();
 54          int u = x.u;
 55          if(done[u]) continue;
 56          done[u] = true;
 57          int size = G[u].size();
 58          for(int i = 0; i < size; i++) {
 59             Edge& e = edges[G[u][i]];
 60             if(d[e.to] > d[u] + e.dist) {
 61                dx[e.to] = d[e.to] = d[u] + e.dist;
 62                p[e.to] = G[u][i];
 63                Q.push ((Heapnode) {d[e.to], e.to});
 64             }
 65          }
 66       }
 67    }
 68 }dij;
 69
 70 void work(int n, int r)
 71 {
 72    dij.init(n+1);
 73    for(int i = 0; i < r; i++){
 74       int a, b, c; scanf("%d%d%d", &a, &b, &c);
 75       dij.AddEdge(a, b, c);
 76       dij.AddEdge(b, a, c);
 77    }
 78    dij.dijkstra(1, dist1);
 79    dij.dijkstra(n, dist2);
 80    int Dist, sta = dist1[n], res = INF;
 81    for(int i = 1; i <= n; i++){
 82       int ss = dij.G[i].size();
 83       for(int j = 0; j < ss; j++){
 84          int m = dij.G[i][j];
 85          Edge ee = dij.edges[m];
 86          Dist = dist1[ee.from] + dist2[ee.to] + ee.dist;
 87          if(Dist < res && Dist > dist1[n]) res = Dist;
 88          //cout<<Dist<<" "<<res<<endl;
 89       }
 90    }
 91    printf("%d\n", res);
 92 }
 93 int main()
 94 {
 95    int n, r;
 96    while(scanf("%d%d", &n, &r) != EOF){
 97       work(n, r);
 98    }
 99    return 0;
100 }

poj-3255-Roadblocks-路径可重复次短路

时间: 2024-09-18 09:02:20

poj-3255-Roadblocks-路径可重复次短路的相关文章

POJ 3255 Roadblocks (次短路问题)

解法有很多奇葩的地方,比如可以到达终点再跳回去再跳回来(比如有两个点)....反正就是不能有最短路,不过没关系,算法都能给出正确结果 思想:和求最短路上的点套路一样,spfa先正着求一次,再反着求一次最短路,然后枚举每条边<i,j>找dist_zheng[i] + len<i,j> + dist_fan[j]的第二小值即可!注意不能用邻接矩阵,那样会MLE,应该用邻接表 /* poj 3255 3808K 266MS */ #include<cstdio> #inclu

poj 3255 Roadblocks【次短路】

题目:poj 3255 Roadblocks 题意:给出一个无向图,然后求1到n点的次短路 分析:两种做法,第一种,Astat+最短路求k短路的方法. 第二种是比较暴力的方法. 先求1点到所有点的最短路dis1 然后求n点到所有点的最短路dis2 然后枚举所有边,则次短路为dis1[from] + dis2[to] + w[i]中大于最短路的最短的. AC代码: #include <cstdio> #include <string> #include <cstring>

POJ - 3255 Roadblocks (次短路)

POJ - 3255 Roadblocks Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status 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

POJ 3255 &amp;&amp; HDU 1688 &amp;&amp; HDU 3191 次短路问题

POJ 3255 Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7627   Accepted: 2798 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 h

POJ 3255 Roadblocks (次短路模板)

Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       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 quick

POJ 3255 Roadblocks (次短路径 + Dijkstra算法)

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7982   Accepted: 2921 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 q

POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 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

POJ 3255 Roadblocks

Roadblocks Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 325564-bit integer IO format: %lld      Java class name: Main Bessie has moved to a small farm and sometimes enjoys returning to visit one of her bes

POJ 3255 Roadblocks Dijkstra 算法变形

#include <cstdio> #include <iostream> #include <queue> using namespace std; const int INF = 1000000; const int maxn = 5005; struct edge{ int y,w; edge(int cy,int ww){ y = cy; w = ww; } }; vector<edge> vec[maxn]; int n,m; struct num