HDU1595-find the longest of the shortest-dijkstra + Record Path

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.

InputEach 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.OutputIn 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

 

Given time is 5s, does not time out

Meaning of the questions :

All the way in the presence of a given road is being built

Went from 1 n, find the shortest path of maximum length

 

Ideas:

First dijkstra again find the shortest path length, and the recording path

One by one and then delete the shortest path leading to each edge can be replaced by another to reach the end of the road

Calculating the maximum length

 

Difficulties: recording path

 

  . 1 #include <the iostream>
   2 #include < String .h>
   . 3 #include <the cmath>
   . 4 #include <The iomanip>
   . 5  #define INF 0x3f3f3f3f
   . 6  #define iOS () STD :: :: iOS sync_with_stdio (to false)
   . 7  #define CIN () cin.tie (0)
   . 8  #define COUT () cout.tie (0)
   . 9  #define MEM (A) Memset (A, 0, the sizeof (A))
 10  the using  namespace STD;
 . 11  
12 is  int Vertex [ 1001 ]; // record the first shortest path is obtained through the apex 
13 is  int n-, m;// n-total number of vertices, m is the number of edges 
14  int E [ 1001 ] [ 1001 ];
 15  int DIS [ 1001 ], DIS1 [ 1001 ], DIS2 [ 1001 ];
 16  int Book [ 1001 ];
 . 17  int In Flag;
 18 is  
. 19  void the init ()
 20 is  {
 21 is      for ( int I = . 1 ; I <= n-; I ++ )
 22 is      {
 23 is          for ( int J = . 1 ; J <= n-; J ++ )
 24         {
 25             if(i==j)
 26                 e[i][j]=0;
 27             else
 28                 e[i][j]=inf;
 29             }
 30     }
 31 }
 32 
 33 int  dijkstra(int *dis)
 34 {
 35     mem(book);mem(dis1);mem(dis2);
 36     for(int i=1;i<=n;i++)
 37         dis[i]=e[1][i];
 38     book[1 ] = 1 ;
39      you and;
40      for ( you i = 2 ; i <= n; i ++ )
 41      {
 42          're a = inf;
43          for ( you j = 1 ; j <= n; j ++ )
 44          {
 45              ow (book [i] == 0 && dis [i] < a)
 46              {
 47                  and = j;
48                  a = dis [i];
49              }
 50         }
 51 is          Book [U] = . 1 ;
 52 is          for ( int K = . 1 ; K <= n-; K ++ )
 53 is          {
 54 is              IF (E [U] [K] <INF && DIS [U] + E [U] [K] < DIS [K])
 55              {
 56 is                  DIS [K] = DIS [U] + E [U] [K];
 57 is                  IF (in Flag) // If flag = 1, then start recording the shortest path 
58                  {
 59                      Vertex [ K] = U; // record the vertex 
60                  }
 61              }
 62          }
 63     }
 64     return dis[n];
 65 }
 66 
 67 int main()
 68 {
 69     ios();cin();cout();
 70     int x,y,z;
 71     while(cin>>n>>m)
 72     {
 73         mem(dis);mem(vertex);mem(e);
 74         init();
 75         for(int i=0; i<m; i++)
 76         {
 77             cin>>x>>y>>z;
 78             e[x][y]=z;
79              E [Y] [X] = Z;
 80          }
 81  
82          In Flag = . 1 ; MEM (DIS1);
 83          Fill (Vertex, Vertex + 1001 , . 1 );
 84          int Shortest = Dijkstra (DIS1);
 85        //   COUT << << Shortest "**" << endl;
 86  
87  //         for (int I = n-; I> = 2; i--)
 88  //             COUT << Vertex [I] << ""; 
89  
90          In Flag = 0 ; // the control flag to decide whether the recording path 
91 is          int W = n-; //Go back one step from each segment is replaced and becomes infinite, find other edge 
92          int ANS = Shortest;
 93          while (! W = . 1 ) //    through while controlling whether to return back to traversing all edges 
94          {
 95            //   COUT << "***" << endl; 
96              int frontt = Vertex [W];
 97              int ori = E [frontt] [W];
 98              E [frontt] [W] = E [W] [frontt ] = INF; // first go to the last edge change 
99              MEM (DIS2);
 100              int WW = Dijkstra (DIS2);
 101            //   IF (! WW = INF) 
102                  ANS =max (WW, ANS);
 103              E [frontt] [W] = E [W] [frontt] = ori;
 104              W = frontt;
 105             // becomes lower then after an infinite time to find the time that may be required to edges, it is necessary to restore 
106          }
 107          COUT ANS << << endl;
 108      }
 109      return  0 ;
 110 }
View Code

 

Guess you like

Origin www.cnblogs.com/OFSHK/p/11519270.html