E. Routing. eachroadconnectsapairofdistinctcitiesandisbidireal.
E. president's Path time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Good old Berland has n cities and m roads. each road connects a pair of distinct cities and is bidirectional.
E. President's Path
Time limit per test
4 seconds
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
Good old Berland hasNCities andMRoads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.
We also know that the President will soon ride along the Berland roads from citySTo cityT. Naturally, he will choose one of the shortest paths fromSToT, But nobody can say for sure which path he will choose.
The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. that is the reason he is planning to repair the roads in the possible President's path.
Making the budget for such an event is not an easy task. For all possible distinct pairsS,?T(S? T) Find the number of roads that lie on at least one shortest path fromSToT.
Input
The first line of the input contains integersN,?M(2? ≤?N? ≤? 500, 0? ≤?M? ≤?N·(N? -? 1 )? /? 2)-the number of cities and roads, correspondingly. ThenMLines follow, containing the road descriptions, one description per line. Each description contains three integersXI,?YI,?LI(1? ≤?XI,?YI? ≤?N,?XI? ?YI,? 1? ≤?LI? ≤? 106), whereXI,?YIAre the numbers of the cities connected byI-Th road andLIIs its length.
Output
Print the sequence of integersC12 ,?C13 ,?...,?C1N,?C23 ,?C24 ,?...,?C2N,?...,?CN? -? 1 ,?N, WhereCStIs the number of roads that can lie on the shortest path fromSToT. Print the elements of sequenceCIn the described order. If the pair of citiesSAndTDon't have a path between them, thenCSt? =? 0.
Sample test (s)
Input
5 61 2 12 3 13 4 14 1 12 4 24 5 4
Output
1 4 1 2 1 5 6 1 2 1
Question:
The following figure shows the total number of routes for the shortest distance between each vertex.
Solution:
Because we need to find the total number of routes with the shortest distance between each point, we can split the problem into: 1. The shortest distance; 2. The total number of routes with the shortest distance.
For question 1, you can use the floyd algorithm to calculate it;
For question 2, the key point of this question is that the total number of outlets should not be repeated. we can calculate the number of paths in the shortest path around a vertex. because one side cannot be counted twice. (For details, refer to the Code ).
Code:
# Include
# Include
# Include
# Include using namespace std; const int lim = 500000001; class TMain {private: int n, m, f [505] [505], dis [505] [505], ans [505] [505], now [505]; public: int run () {scanf ("% d", & n, & m ); for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) if (I = j) f [I] [j] = dis [I] [j] = 0; else f [I] [j] = dis [I] [j] = lim; for (int I = 1; I <= m; I ++) {int a, B, c; scanf ("% d", &, & B, & c); dis [a] [B] = di S [B] [a] = f [a] [B] = f [B] [a] = min (f [a] [B], c );} for (int k = 1; k <= n; k ++) for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) f [I] [j] = min (f [I] [j], f [I] [k] + f [k] [j]); // calculates the shortest path between each vertex for (int I = 1; I <= n; I ++) {// from for (int j = 1; j <= n; j ++) now [j] = 0; for (int j = 1; j <= n; j ++) for (int k = 1; k <= n; k ++) if (j! = K & f [I] [j] + dis [j] [k] = f [I] [k]) // determine whether the edge connected to the k point is in the shortest path. now [k] ++; // If the edge is in, then, the total number of Route entries of the k-node increases by one // because it is the shortest path, there is no edge that can be repeated. For example, f [I] [j] is an edge, now [I] And now [j] can only accumulate for (int j = 1; j <= n; j ++) for (int k = 1; k <= n; k ++) if (f [I] [j] + f [j] [k] = f [I] [k]) // If the shortest path needs to pass through j, it can be noted that the shortest path of j must go through ans [I] [k] + = now [j]; // accumulate} for (int I = 1; I <n; I ++) for (int j = I + 1; j <= n; j ++) if (f [I] [j] = lim) printf ("0"); else printf ("% d", ans [I] [j]); printf ("\ n"); return 0 ;}} Main; int main () {return Main. run ();}