zoukankan      html  css  js  c++  java
  • HDU 1595 find the longest of the shortest

    题意:从1到n去点一条边,最坏的情况下的最短路是多少

    题解:先跑一遍最短路,记录途径的每条路,然后再遍历去点最短路中的每条路,再继续跑最短路(因为不是关键路径的路去掉了对最短路没影响)。

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int inf=0x3f3f3f3f;
     4 int mm[1010][1010];
     5 int vis[1010],dis[1010],link[1010];
     6 int n,m;
     7 bool flag=0;
     8 void dijkstra(int s)
     9 {
    10     memset(vis,0,sizeof(vis));
    11     memset(dis,0x3f,sizeof(dis));
    12     int minn,pos;
    13     dis[s]=0;
    14     for(int i=0;i<=n;i++)
    15     {
    16         minn=inf;
    17         for(int j=1;j<=n;j++)
    18         {
    19             if(dis[j]<minn&&!vis[j])
    20             {
    21                 minn=dis[j];
    22                 pos=j;
    23             }
    24         }
    25         vis[pos]=1;
    26         for(int j=1;j<=n;j++)
    27         {
    28             if(dis[pos]+mm[pos][j]<dis[j])
    29             {
    30                 dis[j]=dis[pos]+mm[pos][j];
    31                 if(flag)
    32                     link[j]=pos;
    33             }
    34         }
    35     }
    36 }
    37 int main()
    38 {
    39     while(~scanf("%d%d",&n,&m))
    40     {
    41         memset(mm,0x3f,sizeof(mm));
    42         memset(link,0,sizeof(link));
    43         while(m--)
    44         {
    45             int a,b,w;
    46             scanf("%d%d%d",&a,&b,&w);
    47             mm[a][b]=mm[b][a]=w;
    48         }
    49         flag=!flag;
    50         dijkstra(1);
    51         flag=!flag;
    52         int ans=dis[n];
    53         for(int i=n;i!=1;i=link[i])
    54         {
    55             int temp=mm[i][link[i]];
    56             mm[i][link[i]]=mm[link[i]][i]=inf;
    57             dijkstra(1);
    58             ans=max(ans,dis[n]);
    59             mm[i][link[i]]=mm[link[i]][i]=temp;
    60         }
    61         printf("%d
    ",ans);
    62     }
    63 }
  • 相关阅读:
    TestNG:org.openqa.selenium.firefox.NotConnectedException: Unable to connect
    Python 程序员经常犯的 10 个错误
    python爬虫框架scrapy实例详解
    使用python进行汉语分词
    Python监控日志程序
    用Python读取excel中的数据
    Python 文件处理
    10 款最好的 Python IDE
    自动化测试代码架构浅谈
    Robotium如何向下拖动屏幕
  • 原文地址:https://www.cnblogs.com/kearon/p/7638253.html
Copyright © 2011-2022 走看看