Topic links: https://vjudge.net/problem/POJ-3268
Meaning of the questions: start line at point X, cattle other points to point X to the party, and then back to their point from point X, path is one-way, all cattle must take the shortest path,
Find the shortest road to go all the cattle in the longest.
(Direct look at the code it, is a change it, and other board dijkstra almost)
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <string> 6 using namespace std; 7 8 typedef long long LL; 9 #define inf (1LL << 30) - 1 10 #define rep(i,j,k) for(int i = (j); i <= (k); i++) 11 #define rep__(i,j,k) for(int i = (j); i < (k); i++) 12 #define per(i,j,k) for(int i = (j); i >= (k); i--) 13 #define per__(i,j,k) for(int i = (j); i > (k); i--) 14 15 const int N = 1010; 16 int G[N][N]; 17 bool vis[N]; 18 int dis1[N]; 19 int dis2[N]; 20 int u,v,w; 21 int n,m,X; 22 23 void init(){ 24 25 rep(i,1,n) vis[i] = false; 26 rep(i,1,n) rep(j,1,n){ 27 if(i == j) G[i][j] = 0; 28 else G[i][j] = inf; 29 } 30 } 31 32 void input(){ 33 34 rep(i,1,m){ 35 cin >> u >> v >> w; 36 if(G[u][v] > w) G[u][v] = w; 37 } 38 } 39 40 void dijkstra(){ 41 42 ////////////////////////////////////////////////////////// 43 //(1)第一部分 44 REP (I, . 1 , n-) DIS1 [I] G = [I] [X]; // DIS [] is stored to the shortest distance X city point 45 VIS [X] = to true ; 46 is 47 REP (I , 2 , n-) { 48 49 int X = - . 1 ; 50 int W = INF; 51 is 52 is REP (J, . 1 , n-) { 53 is IF ! (VIS [J] && W> DIS1 [J]) W = DIS1 [X = J]; 54 is } 55 IF (X == - . 1 ) Continue ; 56 is 57 is VIS [X] =to true ; 58 REP (k, . 1 , n-) { 59 // if the distance from point k to point X k is greater than the distance from the point x to the point x to point X plus distance DIS then update [] array 60 IF (VIS! [K] && DIS1 [K]> DIS1 [X] + G [K] [X]) { 61 is DIS1 [K] = DIS1 [X] + G [K] [X]; 62 is } 63 is } 64 } 65 66 // REP (I,. 1, n-) COUT << DIS1 [I] << endl; 67 68 /////////////////////////// ///////////////////////////////// 69 // (2) the second part is exactly the same dijkstra board 70 REP ( I, . 1 , n-) VIS [I] =false; 71 rep(i,1,n) dis2[i] = G[X][i]; 72 vis[X] = true; 73 74 rep(i,2,n){ 75 int x = -1; 76 int w = inf; 77 78 rep(j,1,n){ 79 if(!vis[j] && w > dis2[j]) w = dis2[x = j]; 80 } 81 82 if(x == -1) continue; 83 84 VIS [X] = to true ; 85 REP (K, . 1 , n-) { 86 IF (VIS [K] && DIS2 [K]> DIS2 [X] +! {G [X] [K]) 87 DIS2 [K] DIS2 = [X] + G [X] [K]; 88 } 89 } 90 } 91 is int ANS = 0 ; 92 93 // the line back and forth together, select the longest round, is the answer 94 REP ( I, . 1 , n-) IF ! (= X-I) = max ANS (ANS, DIS1 [I] + DIS2 [I]); 95 96 COUT ANS << << endl; 97 } 98 99 int main(){ 100 101 ios::sync_with_stdio(false); 102 cin.tie(0); 103 104 cin >> n >> m >> X; 105 106 init(); 107 input(); 108 dijkstra(); 109 110 getchar();getchar(); 111 return 0; 112 }