• 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('
      ');
       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 }
  • 相关阅读:
    普通平衡树(AVL树)
    正态分布与二项分布
    grahql操作
    skywalking定时删除在大并发下引起的性能问题
    Apache SkyWalking SQL注入漏洞复现分析 (CVE-2020-9483)
    skywalking远程调试Apache SkyWalking SQL注入
    sourceTree详细使用教程
    git 分支的相关操作、创建分支、查看分支、切换分支、删除分支
    [Git高级教程 (一)] 通过Tag标签回退版本修复bug
    idea 版本2018.3.6版本的安装
  • 原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9461696.html
Copyright © 2020-2023  润新知