zoukankan      html  css  js  c++  java
  • hdu 1595 find the longest of the shortest(dijkstra)

    Problem Description
    Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
    Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
    Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
    Input
    Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1, and Marica in city N.
    In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
     
    Output
    In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
     
    Sample Input
    5 6
    1 2 4
    1 3 3
    2 3 1
    2 4 4
    2 5 7
    4 5 1
    
    6 7
    1 2 1
    2 3 4
    3 4 4
    4 6 4
    1 5 5
    2 5 2
    5 6 5
    
    5 7
    1 2 8
    1 4 10
    2 3 9
    2 4 10
    2 5 1
    3 4 7
    3 5 10
     
    Sample Output
    11 
    13 
    27
     
    Source

    题意:城市内有n条路,其中有某条路在修,因为这条路有很多情况,问各种情况下的最短路中最长的是哪条

    思路:直接枚举最短路的所有边,将边标记为inf,再跑dijkstra算出最大值即可

      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<math.h>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<set>
     10 #include<bitset>
     11 #include<map>
     12 #include<vector>
     13 #include<stdlib.h>
     14 #include <stack>
     15 using namespace std;
     16 int dirx[]={0,0,-1,1};
     17 int diry[]={-1,1,0,0};
     18 #define PI acos(-1.0)
     19 #define max(a,b) (a) > (b) ? (a) : (b)  
     20 #define min(a,b) (a) < (b) ? (a) : (b)
     21 #define ll long long
     22 #define eps 1e-10
     23 #define MOD 1000000007
     24 #define N 1006
     25 #define inf 1<<26
     26 int n,m;
     27 int mp[N][N];
     28 int fa[N];
     29 void init(){
     30     for(int i=1;i<=n;i++){
     31         fa[i]=-1;
     32         for(int j=1;j<=n;j++){
     33             if(i==j){
     34                 mp[i][j]=0;
     35             }
     36             else{
     37                 mp[i][j]=inf;
     38             }
     39         }
     40     }
     41 }
     42 int dijkstra(int flag){
     43     int vis[N];
     44     int dis[N];
     45     for(int i=0;i<=n;i++){
     46         vis[i]=0;
     47         dis[i]=inf;
     48     }
     49     vis[1]=1;
     50     dis[1]=0;
     51     int x=n;
     52     int st=1;
     53     while(x--){
     54         for(int i=1;i<=n;i++){
     55             
     56                 if(dis[i]>dis[st]+mp[st][i]){
     57                     dis[i]=dis[st]+mp[st][i];
     58                     if(flag){
     59                         fa[i]=st;
     60                     }    
     61                 }
     62             
     63         }
     64         int minn=inf;
     65         for(int i=1;i<=n;i++){
     66             if(!vis[i] && dis[i]<minn){
     67                 minn=dis[i];
     68                 st=i;
     69             }
     70         }
     71         vis[st]=1;
     72         
     73     }
     74     
     75     return dis[n];
     76 }
     77 int main()
     78 {
     79     while(scanf("%d%d",&n,&m)==2){
     80         init();
     81         for(int i=0;i<m;i++){
     82             int a,b,c;
     83             scanf("%d%d%d",&a,&b,&c);
     84             if(mp[a][b]>c){
     85                 mp[a][b]=mp[b][a]=c;
     86             }
     87         }
     88         int ans=dijkstra(1);
     89         
     90         for(int i=n;i!=1;i=fa[i]){
     91             int num=mp[i][fa[i]];
     92             mp[i][fa[i]]=mp[fa[i]][i]=inf;
     93             int cnt=dijkstra(0);
     94             
     95             ans=max(ans,cnt);
     96             mp[i][fa[i]]=mp[fa[i]][i]=num;
     97         }
     98         printf("%d
    ",ans);
     99         
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    POJ 1321:棋盘问题
    POJ 2251:Dungeon Master
    POJ 3438:Look and Say
    POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
    杭电1285--确定比赛名次(拓扑排序)
    南阳67--三角形面积
    南阳38--布线问题
    杭电1050--Moving Tables(区间覆盖)
    杭电1217--Arbitrage(Spfa)
    杭电1719--Friend(找规律)
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4833241.html
Copyright © 2011-2022 走看看