• poj 3255 Roadblocks 次短路(两次dijksta)


    Roadblocks

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 15   Accepted Submission(s) : 6
    Problem 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: <i>N</i> and <i>R</i> <br>Lines 2..<i>R</i>+1: Each line contains three space-separated integers: <i>A</i>, <i>B</i>, and <i>D</i> that describe a road that connects intersections <i>A</i> and <i>B</i> and has length <i>D</i> (1 ≤ <i>D</i> ≤ 5000)
     
    Output
    Line 1: The length of the second shortest path between node 1 and node <i>N</i>
     
    Sample Input
    4 4 1 2 100 2 4 200 2 3 250 3 4 100
     
    Sample Output
    450
     
    有n个路口r条马路,马路可以重复走,问从1号路口到n号路口的次短路
     

    次短路问题,实际上可以这么理解:

    在知道最短路的情况下,不走最短路,绕一段路,而且只能绕一段路,否则会不满足次短。

    所以就先找到最短路并记录下路径,然后枚举最短路上的每一个点a,从这个点再绕一个点b,然后再加上点b到n的最短路。

    所以我们需要知道从1到每个点的最短路,还需要知道从每个点到n的最短路,从每个点到n的最短路就是从n到每个点的最短路

    所以两次dijkstra 然后枚举次短路就好啦

     

    邻接矩阵居然超内存

     1 #include <cstring>  
     2 #include <cstdio>  
     3 #include <iostream>  
     4 #include <vector> 
     5 #include <algorithm>
     6 #include <string>
     7 #define inf 0x3f3f3f3f 
     8 const int maxn = 5010;
     9 using namespace std;
    10 int n, m;
    11 int d1[maxn];
    12 int d2[maxn];
    13 bool book[maxn];
    14 struct edge
    15 {
    16     int to, c;
    17 };
    18 vector<edge> e[maxn];
    19 //用邻接矩阵会超内存
    20 void dijkstra(int s, int *d)
    21 {
    22     memset(d,inf, maxn * sizeof(int));
    23     memset(book, 0, sizeof(book));
    24     int i;
    25     //for (i = 0; i < e[s].size(); i++) d[e[s][i].to] = e[s][i].c;
    26     //book[s] = 1;
    27     //不能直接赋值,也要进行比较,典型样例
    28     //2 2
    29     //1 2 100
    30     //1 2 200
    31     d[s] = 0;
    32     while (1)
    33     {
    34         int k = -1;
    35 
    36         int min = inf;
    37         for ( i = 1; i<= n;i++)
    38         {
    39             if (!book[i] && d[i] < min)
    40             {
    41                 min = d[i];
    42                 k = i;
    43             }
    44         }
    45         if (k == -1) break;
    46 
    47         else
    48         {
    49             book[k] = 1;
    50             for (i=0;i<e[k].size();i++)
    51             {
    52                 if (d[e[k][i].to] > d[k] + e[k][i].c)
    53                 {
    54                     d[e[k][i].to] = d[k] + e[k][i].c;
    55                 }
    56 
    57             }
    58         }
    59     }
    60 }
    61 
    62 int main()
    63 {
    64     int i;
    65     cin >> n >> m;
    66     for (i = 1; i <= m; i++)
    67     {
    68         edge t, t1;
    69         int k;
    70         cin >> k >> t.to >> t.c;
    71         t1.to = k;
    72         t1.c = t.c;
    73         e[k].push_back(t);//双向存。存一个点出发的多个目的地从k出发,目的地是t.to,花费t.c
    74         e[t.to].push_back(t1);
    75     }
    76 
    77     dijkstra(1, d1);
    78     dijkstra(n, d2);//某个点到n的最短路就是n到某个点的最短路
    79     int k = n;
    80     int ans = inf;
    81     int minn = d1[n];
    82 
    83     for (k=1;k<=n;k++)
    84     {
    85         for (i = 0; i<e[k].size(); i++)
    86         {
    87             int ee = d1[k] + e[k][i].c + d2[e[k][i].to];
    88             if (ans>ee&&ee>minn)
    89             {
    90                 ans = ee;
    91             }
    92         }
    93     }
    94     cout << ans << endl;
    95     return 0;
    96 }

    超内存代码且错误代码

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <cstdio>
     6 #define inf 0x3f3f3f3f
     7 using namespace std;
     8 int e[5005][5005];
     9 int d1[1005];
    10 int d2[1005];
    11 int m, n;
    12 void dijkstra(int s, int *d)
    13 {
    14     int book[1005];
    15     memset(book, 0, sizeof(book));
    16     int i;
    17     for (i = 1; i <= n; i++)
    18     {
    19         d[i] = e[s][i];
    20     }
    21     book[1] = 1;
    22     while (1)
    23     {
    24         int min = inf;
    25         int k = -1;
    26         for (i = 1; i <= n; i++)
    27         {
    28             if (d[i] < min&&book[i]==0)
    29             {
    30                 min = d[i];
    31                 k = i;
    32             }
    33         }
    34         if (k == -1) break;
    35         book[k] = 1;
    36         for (i = 1; i <= n; i++)
    37         {
    38             if (book[i] == 0 && d[i] > d[k] + e[k][i])
    39             {
    40                 d[i] = d[k] + e[k][i];
    41             }
    42         }
    43     }
    44 }
    45 int main()
    46 {
    47     int i, j;
    48     scanf("%d %d", &m, &n);
    49     for (i = 1; i <= n; i++)
    50     {
    51         for (j = 1; j <= n; j++)
    52         {
    53             if (i == j) e[i][j] = 0;
    54             else
    55                 e[i][j] = inf;
    56         }
    57     }
    58     for (i = 1; i <= n; i++)
    59     {
    60         int x, y, z;
    61         scanf("%d %d %d", &x, &y,&z);
    62         if (e[x][y] > z)
    63         {
    64             e[x][y] = z;
    65             e[y][x] = z;
    66         }
    67     }
    68     dijkstra(1, d1);
    69     dijkstra(n, d2);
    70     int minn = d1[n];
    71     int ans = inf;
    72     for (i = 1; i <= n; i++)
    73     {
    74         for (j = 1; j <= n; j++)
    75         {
    76             if (i == j) continue;
    77             if (e[i][j] == inf) continue;
    78             if (d1[i] + e[i][j] + d2[j] > minn)
    79             {
    80                 ans = min(ans, d1[i] + e[i][j] + d2[j]);
    81             }
    82         }
    83     }
    84     printf("%d
    ", ans);
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    回滚事件只是让原数据看起来不变,但是id还是会自增吗?
    什么是Java中的原子操作( atomic operations)
    Java并发编程:volatile关键字解析
    对Java CAS的一些了解(正在整理学习中)
    问号和冒号----条件运算符, 问号冒号表达式
    对Java ConcurrentHashMap的一些了解
    Java HashMap的工作原理
    Java hashCode() 和 equals()
    Java 对比Hashtable、Hashmap、Treemap有什么不同?
    Java TreeMap详细介绍和使用示例
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271245.html
Copyright © 2020-2023  润新知