Link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2121
Question:
Ice_cream's World II
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1610 accepted submission (s): 354
Problem descriptionafter awarded lands to acmers, the queen want to choose a city be her capital. this is an important event in ice_cream world, and it also a very difficult problem, because the world have n cities and M roads, every road was directed. wiskey is a chief engineer
In ice_cream world. the Queen asked wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project's cost as less as better. if wiskey can't fulfill the Queen's require, he will be punishing.
Inputevery case have two integers n and M (n <= 1000, m <= 10000), the cities numbered 0... N-1, following M lines, each line contain three integers S, T and C, meaning from S to t have a road will cost C.
Outputif no location satisfy the Queen's require, you must be output "impossible", otherwise, print the minimum cost in this project and suitable City's number. may be exist before suitable cities, choose the minimum number city. after every case print one blank.
Sample Input
3 10 1 14 40 1 100 2 101 3 202 3 30
Sample output
impossible40 0
Authorwiskey sourcehdu 2007-10 programming contest_warmup
Recommend whisky
Analysis and Summary:
The solution to the problem of the minimum tree structure of the infinitus root is to design a "virtual New Root point". This node can be used to reach all points, in addition, the weight value is a large value (greater than the ownership value), and then you can check whether the node has a minimum tree structure.
The method for finding the true and the node is clever, and the relationship between the edge location and the virtual node is used.
Code:
# Include <cstdio> # include <iostream> # include <cstring> # include <cmath> using namespace STD; const int VN = 1005; const int INF = 0x7fffff; int ans_root; template <typename type> class directed_mst {public: void Init (INT _ n) {n = _ n + 1; size = 0; ans = 0;} void insert (int u, int V, Type _ w) {e [size ++]. set (u, v, _ w);} type directed_mst (INT root) {While (true) {// 1. find the smallest front edge (INT I = 1; I <n; ++ I) in [I] = inf; For (INT I = 0; I <size; ++ I) {int u = E [I]. u, v = E [I]. v; If (E [I]. W <in [v] & U! = V) {pre [v] = u; in [v] = E [I]. w; If (u = root) {ans_root = I; // Save the edge position I instead of V }}for (INT I = 1; I <N; ++ I) if (I! = Root) {If (in [I] = inf) Return-1;} // 2. int mxid = 1; in [root] = 0; memset (ID,-1, sizeof (ID); memset (VIS,-1, sizeof (ID )); for (INT I = 1; I <n; ++ I) {ans + = in [I]; int v = I; while (vis [v]! = I & ID [v] =-1 & V! = Root) {vis [v] = I; V = pre [v];} If (V! = Root & ID [v] =-1) {for (INT u = pre [v]; u! = V; u = pre [u]) {ID [u] = mxid;} ID [v] = mxid ++ ;}} if (mxid = 1) break; // For (INT I = 1; I <n; ++ I) if (ID [I] =-1) ID [I] = mxid ++; // 3. shrink point, re-mark for (INT I = 0; I <size; ++ I) {int u = E [I]. u, v = E [I]. v; E [I]. U = ID [u]; E [I]. V = ID [v]; If (E [I]. u! = E [I]. v) E [I]. w-= in [v];} n = mxid; root = ID [root];} return ans;} PRIVATE: struct edge {int U, V; type W; void set (INT _ u, int _ v, Type _ w) {u = _ u, v = _ V, W = _ w ;}} E [VN * VN/2]; Type ans; // type in [VN]; int N; // number of nodes int size; // Number of edges int pre [VN]; // front edge with the smallest weight int ID [VN]; int vis [VN]; // whether it is in or outside the ring}; directed_mst <int> G; int main () {int n, m; while (~ Scanf ("% d", & N, & M) {G. init (n + 1); int max = 0; For (INT I = 0; I <m; ++ I) {int A, B; int C; scanf ("% d", & A, & B, & C); if (a = B) continue; Max ++ = C; G. insert (a + 1, B + 1, C) ;}++ Max; // set n + 1 to a virtual root node for (INT I = m; I <m + n; ++ I) {G. insert (n + 1, I-m + 1, max); // note the relationship between N + 1 and I on the virtual node. After I> m, start from the virtual root node n + 1 // to 1, 2 ,..., n. using this relationship, when I> m, we can use the location to determine the relationship between m + 1 and other nodes. // use this link to output the root node} int ans = G. directed_mst (n + 1); If (ANS =-1 | ANS-max> = max) puts ("impossible "); else printf ("% d \ n", ANS-Max, ans_root-m); puts ("");} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)