zoukankan      html  css  js  c++  java
  • POJ 3255

    Roadblocks
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6605   Accepted: 2458

    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)

    Source

     
    次短路
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 #include <utility>
     7 
     8 using namespace std;
     9 
    10 typedef pair<int,int> pii;
    11 
    12 const int INF = 1e9;
    13 const int MAX_V = 5005;
    14 const int MAX_E = 1000005;
    15 int N,R;
    16 int first[MAX_V],next[2 * MAX_E],v[2 * MAX_E],w[2 * MAX_E];
    17 int dist[MAX_V],dist2[MAX_V];
    18 
    19 
    20 void addedge(int a,int b,int id) {
    21         int e = first[a];
    22         next[id] = e;
    23         first[a] = id;
    24 }
    25 
    26 void solve() {
    27         fill(dist + 1,dist + N + 1,INF);
    28         fill(dist2 + 1,dist2 + N + 1,INF);
    29 
    30         dist[1] = 0;
    31         priority_queue<pii,vector<pii>,greater<pii> > q;
    32         q.push(pii(0,1));
    33 
    34         while(!q.empty()) {
    35                 pii x = q.top(); q.pop();
    36                 int d = x.first,u = x.second;
    37                 for (int e = first[u]; e != -1; e = next[e]) {
    38                         int d2 = d + w[e];
    39                         if(dist[v[e]] > d + w[e]) {
    40                                 dist[v[e]] = d + w[e];
    41                                 q.push(pii(dist[ v[e] ],v[e]));
    42                         }
    43 
    44                         if(d2 < dist2[ v[e] ] && d2 > dist[ v[e] ]) {
    45                                 dist2[ v[e] ] = d2;
    46                                 q.push(pii(d2,v[e]));
    47                         }
    48 
    49                 }
    50         }
    51 
    52         printf("%d
    ",dist2[N]);
    53 }
    54 int main()
    55 {
    56     //freopen("sw.in","r",stdin);
    57 
    58     scanf("%d%d",&N,&R);
    59     for (int i = 1; i <= N; ++i) first[i] = -1;
    60     for (int i = 0; i < 2 * R; i += 2) {
    61             int u;
    62             scanf("%d%d%d",&u,&v[i],&w[i]);
    63             v[i + 1] = u;
    64             w[i + 1] = w[i];
    65             addedge(u,v[i],i);
    66             addedge(v[i],u,i + 1);
    67     }
    68 
    69     solve();
    70 
    71 
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    document.querySelector和querySelectorAll方法
    Hello World!
    tomcat8总结默认端口默认项目,以及图片上传和展示的路径问题.
    理解java移位运算符
    tomcat8的apr模式配置SLL证书
    SpringData
    JPQL
    JPA多对多
    JPA一对一双向
    JPA一对多单向
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3669807.html
Copyright © 2011-2022 走看看