Description gier is the wife of an antique store. As she operates properly, the store is booming. Yesterday, gier accidentally got the treasure of the moon, which has been scattered for centuries. Gier knows the value of the eye of the moon is even greater: it is made up of many pearls. craftsmen use golden wires to connect the pearls. Each golden wire connects two pearls; at the same time, two colors were added to each gold wire, one half of which were dyed silver-white and the other half were dyed Dai black. Because gier is familiar with ancient books, he still knows the mysterious legend of "Eyes of the Moon": "Eyes of the Moon" was originally a treasure of an ancient temple, originally hanging on a pillar of the Buddhist temple, the entire treasure is vertically hung, and all the pearls are lined up and embedded in the pillar, and every golden wire is tight, and the silver and white ends of the Golden line are always above the black ends of Dai; however, on a full moon night, the eyes of the Moon suddenly fly out of the column and fall down, the treasures themselves intact, it's just that monks can no longer embed the "eye of the Moon" in the pillar as they are. Gier looked at this mysterious treasure and recalled the legends he had read in his childhood. It was an impulse to restore the eyes of the moon, but it was still unsuccessful after several days. Now, please help gier fulfill this mission. You need to design a program to carefully analyze the given "Eyes of the Moon", and then give the arrangement of the treasures embedded on the pillars of the Buddhist temple hundreds of years ago. The given "Eye of the Moon" has n pearls and P gold wires. All pearls have a sequence number: 1, 2... , N. The input data contains a feature description of the "Eye of the Moon": the first line of the file contains two integers N and P, where n indicates the number of pearls in the treasure, and P indicates the number of golden wires in the treasure; the following P lines describe the pearl connection: the object line I + 1 has three integers, Ri1, ri2, Li. Here, Ri1 indicates the pearl serial number connected to the silver and white ends of the I root gold line; ri2 indicates the pearl serial number connected to the Black ends of the I root gold line; Li indicates the length of the I root gold line. Because the size of the pearl is small, several pearls can be embedded in one position at the same time. Your output data describes the position of each pearl on the pillar of the moon eye. The output file contains N rows: line I, an integer S, it indicates the distance of the pearl labeled as I from the highest position on the pillar. NOTE: If there is no solution, only one line is output, containing an integer "-1 ". Sample input9 9 1 2 3 2 5 2 7 1 4 5 5 6 1 5 9 1 6 6 7 1 7 8 3 9 8 4 sample output2 5 5 10 0 4 5 6 9 5 hint
See the code for http://blog.csdn.net/kongming_acm/article/details/5721338133. Here is my understanding.
# Include <iostream> using namespace STD; int X [501], Y [501], L [501], V [501], H [501], N, P, flag = 0; void DFS (int K) {int I, HH, O; If (FLAG) return; V [k] = 1; // Mark K as read for (I = 1; I <= P; I ++) // check whether the header is K or the end is K if (X [I] = k) | (Y [I] = k )) {o = x [I] + Y [I]-K; // calculate another HH = (X [I] = K? H [k] + L [I]: H [k]-l [I]); // calculate the height if (V [O]) & (H [O]! = HH) // if another vertex has been marked, it indicates that there is a circle. If H [O] is not correct, it is a ghost {flag = 1; return ;} H [O] = HH; Calculate the height of another vertex if (! V [O]) DFS (o); recursion} int main () {CIN> N> P; int I, min = 0; for (I = 1; I <= P; I ++) CIN> X [I]> Y [I]> L [I]; H [1] = 0; DFS (1); // check if (FLAG) cout <-1 <Endl; else {for (I = 1; I <= N; I ++) if (H [I] <min) min = H [I]; for (I = 1; I <= N; I ++) cout <H [I]-min <Endl;} return 0 ;}