Roadblocks(POJ 3255)

  • 原题如下:

    Roadblocks

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 19314   Accepted: 6777

    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: AB, 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)

  • 题解:这题要求的是次短路,根据Dijkstra算法的思路,依次确定尚未确定的顶点中距离最小的顶点可求得最短路,在这个基础上做少许修改,即可求得次短路,到某个顶点v的次短路无非两种情况:①到其它顶点u的最短路再加上u→v的边,②到u的次短路再加上u→v的边。因此,只要求出到所有顶点的最短路和次短路就好了。对于每个顶点,我们记录最短路和次短路,类似Dijkstra算法那样,不断更新这两个距离。(注:这道题中男女之间的二分图结构是无用的陷阱条件,许多问题中,如果有特殊的结构,往往会考虑如何利用这个结构,但这题不是的)
  • 代码:

      1 #include <cstdio>
      2 #include <queue>
      3 #include <vector>
      4 #include <functional>
      5 #include <cctype>
      6 #include <algorithm>
      7 #define num s-‘0‘
      8
      9 using namespace std;
     10 typedef pair<int, int> P;
     11
     12 struct edge
     13 {
     14     int to;
     15     int cost;
     16 };
     17
     18 const int MAX_V=5000;
     19 const int INF=0x3f3f3f3f;
     20 int V,E,n;
     21 vector<edge> G[MAX_V];
     22 int dist[MAX_V];
     23 int dist2[MAX_V];
     24
     25 void solve();
     26
     27 void swap(int &x, int &y)
     28 {
     29     int t=x;
     30     x=y;y=t;
     31 }
     32
     33 void read(int &x){
     34     char s;
     35     x=0;
     36     bool flag=0;
     37     while(!isdigit(s=getchar()))
     38         (s==‘-‘)&&(flag=true);
     39     for(x=num;isdigit(s=getchar());x=x*10+num);
     40     (flag)&&(x=-x);
     41 }
     42
     43 void write(int x)
     44 {
     45     if(x<0)
     46     {
     47         putchar(‘-‘);
     48         x=-x;
     49     }
     50     if(x>9)
     51         write(x/10);
     52     putchar(x%10+‘0‘);
     53 }
     54
     55 int main()
     56 {
     57     read(V);read(E);
     58     for (int i=0; i<E; i++)
     59     {
     60         edge e;
     61         int s,t,c;
     62         read(s);read(t);read(c);
     63         e.to=t-1;e.cost=c;
     64         G[s-1].push_back(e);
     65         e.to=s-1;e.cost=c;
     66         G[t-1].push_back(e);
     67     }
     68     solve();
     69     write(dist2[V-1]);
     70     putchar(‘\n‘);
     71 }
     72
     73 void solve()
     74 {
     75     priority_queue<P, vector<P>, greater<P> > que;
     76     fill(dist, dist+V, INF);
     77     fill(dist2, dist2+V, INF);
     78     dist[0]=0;
     79     que.push(P(0,0));
     80     while (!que.empty())
     81     {
     82         P p=que.top(); que.pop();
     83         int v=p.second, d=p.first;
     84         if (dist2[v]<d) continue;
     85         for (int i=0; i<G[v].size(); i++)
     86         {
     87             edge &e=G[v][i];
     88             int d2=d+e.cost;
     89             if (dist[e.to]>d2)
     90             {
     91                 swap(dist[e.to],d2);
     92                 que.push(P(dist[e.to],e.to));
     93             }
     94             if (dist2[e.to]>d2 && dist[e.to]<d2)
     95             {
     96                 dist2[e.to]=d2;
     97                 que.push(P(dist2[e.to],e.to));
     98             }
     99         }
    100     }
    101 }

原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9461696.html

时间: 2024-08-10 22:37:53

Roadblocks(POJ 3255)的相关文章

Roadblocks POJ 3255(次短路)

原题 题目链接 题目分析 给无向图,求次短路.相对于第k短路而言次短路还是好求的,只需要在跑dijkstra的过程中顺便记录次短路就行了. 代码 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <utility> 6 #include <ctime> 7 #include <cmath

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 题解

题目大意 给你一个 \(n\) 个点,\(m\) 条边的无向图,求出这个无向图中从1到n的次短路.其中\(n \le 5000\),\(m \le 100000\). 题目传送门 POJ 3255 思路 其实求次长路是很简单的一个问题,但是网上却有很多算法都过于复杂了.首先我们先求出从1到每个结点的最短路长度(用Dijkstra或者是SPFA都可以),存入数组 \(dis1\) 中,然后再求出从结点 \(n\) 到任意结点的最短路的长度,存入数组 \(dis2\) 中. 然后我们枚举这个图中的所

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

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算法)

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