码迷,mamicode.com
首页 > 数据库 > 详细

Roadblocks(POJ 3255)

时间:2018-08-12 01:34:47      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:The   flag   图结构   void   BMI   clu   for   wap   more   

  • 原题如下:
    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 }

     

Roadblocks(POJ 3255)

标签:The   flag   图结构   void   BMI   clu   for   wap   more   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!