Codeforces Round #270 (using the prim algorithm ),
D. Design Tutorial: Inverse the Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. the hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example.
Now let's create a task this way. we will use the task: you are given a tree, please calculate the distance between any pair of its nodes. yes, it is very easy, but the inverse version is a bit harder: you are givenNLimit × limitNDistance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers ).
Input
The first line contains an integerN(1 digit ≤ DigitNLimit ≤ limit 2000)-the number of nodes in that graph.
Then nextNLines each containsNIntegersDI, Bytes,J(0 bytes ≤ bytesDI, Bytes,JLimit ≤ limit 109)-the distance between nodeIAnd nodeJ.
Output
If there exists such a tree, output "YES", otherwise output "NO ".
Sample test (s) input
30 2 72 0 97 9 0
Output
YES
Input
31 2 72 0 97 9 0
Output
NO
Input
30 2 27 0 97 9 0
Output
NO
Input
30 1 11 0 11 1 0
Output
NO
Input
20 00 0
Output
NO
Note
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.
In the second example, it is impossible becauseD1, rows 1 shoshould be 0, but it is 1.
In the third example, it is impossible becauseD1, rule 2 shoshould equalD2, period 1.
The distance between the two points is given. If you want to create a tree composed of these points, we first construct a minimum spanning tree, then compare whether the distance between each point is equal to the distance given by the question. You can use dfs to search for the distance between each two points in the entire graph. the following example uses an array f [] [] to record the distance between two points x and y. When calculating the distance, you can find the distance through the front of the current vertex, that is to say, an array record precursor is required, so that you don't need to use dfs. after reading the code, you will understand .....
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 2010; const int INF = 0x3f3f3f; int graph [maxn] [maxn]; int prior [maxn]; int visit [maxn]; int dis [maxn]; int f [maxn] [maxn]; int n; bool check () {for (int I = 0; I <n; I ++) {dis [I] = INF; if (graph [I] [I]! = 0) return false; for (int j = I + 1; j <n; j ++) {if (graph [I] [j]! = Graph [j] [I] | graph [I] [j] = 0) return false;} memset (visit, 0, sizeof (visit )); memset (prior,-1, sizeof (prior); memset (f, 0, sizeof (f); int cent = n; dis [0] = 0; while (cent --) // loop n times because you want to initialize {int min =-1; for (int I = 0; I <n; I ++) {if (! Visit [I] & (min =-1 | dis [I] <dis [min]) {min = I ;}} for (int I = 0; I <n; I ++) // Add the Content Calculation distance in this layer of loop to the prim algorithm {if (visit [I]) // it must be an accessed point, to calculate the distance {f [I] [min] = f [min] [I] = f [I] [prior [min] + dis [min];} visit [min] = true; for (int I = 0; I <n; I ++) {if (dis [I]> graph [min] [I]) {dis [I] = graph [min] [I]; prior [I] = min; // record precursor }}for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (f [I] [J]! = Graph [I] [j]) {return false ;}} return true ;}int main () {# ifdef xxz freopen ("in", "r ", stdin); # endif // xxz while (cin> n) {for (int I = 0; I <n; I ++) for (int j = 0; j <n; j ++) {cin> graph [I] [j];} if (check () cout <"YES" <endl; else cout <"NO" <endl;} return 0 ;}