zoukankan      html  css  js  c++  java
  • USACO 2006 November Contest Problem. Road Blocks SPFA

    题目

    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).

    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).

    Line 1: Two space-separated integers: N and R

    Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

    Line 1: The length of the second shortest path between node 1 and node N

    4 4
    1 2 100
    2 4 200
    2 3 250
    3 4 100
    450

    分析

    这道题的题目非常容易理解:求图的第二短路径。

    因为只有最短路径算法,那么我们要思考的问题就转化为:如何将最短路径算法的结果转化为第二短路径?

    可以想到,让这条路径必须经过某个不在最短路径上的点,或者说,经过某个不在最短路径上的边,第二段路径就是:

    这条边的起点到整个图的起点的最短路径+边权+这条边的终点到整个图的终点的最短路径。

    最后用程序来实现上面的方法。只需要用单元最短路分别从起点和终点出发,正反SPFA,把结果放在两个不同的数组当中,最后用一个循环找每一个边,如果用上面公式计算出来的长度比最短路径长度要大,那么就把这个长度与已经存储的第二短路径长度进行比较,就可以得出答案了。

    想通以后这个程序写起来就非常的开心了,数组开的大小要稍微注意一下(据说有人翻车了233),我一遍写完就在洛谷上通过了(好开心233)。

    程序

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int MAXM = 200000 + 1;
     5 const int MAXN = 5000 + 1;
     6 int EdgeCount = 0, Head[MAXN], dis[MAXN], dis2[MAXN], n, r;
     7 bool vis[MAXN];
     8 queue<int> Q;
     9 struct node
    10 {
    11     int From, Next, Aim, Weight;
    12 }Edge[MAXM];
    13 
    14 void Insert(int u, int v, int w)
    15 {
    16     Edge[++EdgeCount] = (node){u,Head[u],v,w};
    17     Head[u] = EdgeCount;
    18 }
    19 void SPFA(int s, int t, int *dis)
    20 {
    21     while(!Q.empty())
    22         Q.pop();
    23     Q.push(s);
    24     dis[s] = 0;
    25     memset(vis,false,sizeof(vis));
    26     vis[s] = true;
    27     while(!Q.empty())
    28     {
    29         int p = Q.front();
    30         Q.pop();
    31         vis[p] = false;
    32         for (int i = Head[p]; i; i = Edge[i].Next)
    33         {
    34             int m = Edge[i].Aim;
    35             if (dis[p]+Edge[i].Weight < dis[m])
    36             {
    37                 dis[m] = dis[p] + Edge[i].Weight;
    38                 if (!vis[m])
    39                 {
    40                     Q.push(m);
    41                     vis[m] = 1;
    42                 }
    43             }
    44         }
    45     }
    46 }
    47 
    48 int main()
    49 {
    50     cin >> n >> r;
    51     int u, v, w;
    52     for (int i = 1; i <= r; i++)
    53     {
    54         cin >> u >> v >> w;
    55         Insert(u,v,w);
    56         Insert(v,u,w);
    57     }
    58     memset(dis,0x3F,sizeof(dis));
    59     SPFA(1,n,dis);
    60     int ShortestPathLength = dis[n];
    61     int SecondShortest = 2147483647;
    62     memset(dis2,0x3F,sizeof(dis2));
    63     SPFA(n,1,dis2);
    64     for(int i = 1; i <= EdgeCount; i++)
    65         if(dis[Edge[i].From]+dis2[Edge[i].Aim]+Edge[i].Weight > ShortestPathLength)
    66             SecondShortest = min(dis[Edge[i].From]+dis2[Edge[i].Aim]+Edge[i].Weight, SecondShortest);
    67     cout << SecondShortest << endl;
    68 }
  • 相关阅读:
    C# 获取 存储过程 返回值
    C# System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
    C# oracle 日期型字段,使用参数传值时,遇到ORA-01810: format code appears twice错误
    C# oracle to_date 日期型 参数传值
    windows2003两台服务器,局域网之间不能互相访问
    windows2003服务器,时间每隔1小时自动同步一次
    sqlserver 服务器主体 无法在当前安全上下文下访问数据库
    电商设计V1(一):软件工程设计
    Maven使用教程三:maven的生命周期及插件机制详解
    JVM 学习笔记二 :JVM内存区域
  • 原文地址:https://www.cnblogs.com/OIerPrime/p/8312969.html
Copyright © 2011-2022 走看看