HDU 2121 Ice_cream's world II
Ice_cream's world IITime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3231 Accepted Submission (s): 761
Problem Description After 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.
Input Every 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.
Output If 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
Author Wiskey
Source HDU 2007-10 Programming Contest_WarmUp
Minimum tree structure .... The comments of all the codes below are comments of others .. So far, I still don't understand it very well. Post a post and then attach your thinking.
# Include
Using namespace std; # include
# Include
# Define MAXN 1005 # define INF 0x7f7f7f7ftypedef _ int64 type; struct node // edge Weight and vertex {int u, v; type w;} edge [MAXN * MAXN]; int pre [MAXN], id [MAXN], vis [MAXN], n, m, pos; type in [MAXN]; // Save the minimum inbound edge weight, pre [v] is the start point of the edge type Directed_MST (int root, int V, int E) {type ret = 0; // The total weight of the minimum tree weight while (true) {int I; // 1. find the minimum inbound edge of each node for (I = 0; I <V; I ++) in [I] = INF; // initialize to infinity for (I = 0; I <E; I ++) // traverse each edge {int u = edge [I]. u; Int v = edge [I]. v; if (edge [I]. w <in [v] & u! = V) // indicates that vertex v has an inbound edge record with a smaller weight {pre [v] = u; // node u points to v in [v] = edge [I]. w; // minimum inbound edge if (u = root) // This is the actual starting point pos = I ;}} for (I = 0; I <V; I ++) // determine whether there is a minimal Tree Structure {if (I = root) continue; if (in [I] = INF) return-1; // if there is no inbound edge except the root, the root cannot reach it, indicating that it is an independent vertex and cannot constitute a tree structure} // 2. int cnt = 0; // record the number of rings memset (id,-1, sizeof (id); memset (vis,-1, sizeof (vis )); in [root] = 0; for (I = 0; I <V; I ++) // mark each ring {ret + = in [I]; // record weight 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] = cnt; // mark the node u as the ring id [v] = cnt ++ ;}} if (cnt = 0) break; // if there is no ring, break for (I = 0; I <V; I ++) if (id [I] =-1) id [I] = cnt ++; // 3. create a new thumbnail and remark for (I = 0; I <E; I ++) {int u = edge [I]. u; int v = edge [I]. v; edge [I]. u = id [u]; edge [I]. v = id [v]; if (id [u]! = Id [v]) edge [I]. w-= in [v];} V = cnt; root = id [root];} return ret;} int main () {int I; while (scanf ("% d", & n, & m )! = EOF) {type sum = 0; for (I = 0; I <m; I ++) {scanf ("% d % I64d ", & edge [I]. u, & edge [I]. v, & edge [I]. w); edge [I]. u ++; edge [I]. v ++; sum + = edge [I]. w;} sum ++; for (I = m; I <m + n; I ++) // Add the Supernode 0, the edge weight from node 0 to other nodes is the same (the edge weight in this question must be greater than the total edge Weight of the source image) {edge [I]. u = 0; edge [I]. v = I-m + 1; edge [I]. w = sum;} type ans = Directed_MST (0, n + 1, m + n); // n + 1 is the sum point, m + n indicates the total number of edges. // ans indicates the total weight of the smallest tree worker with the root of the super node 0. // subtract ans from sum. If the difference is less than sum, it indicates that the outbound degree of node 0 is only 1, indicating that the source image is a connected graph. // if the difference value is greater than = sum, it indicates that the outbound degree of node 0 is more than 1, it indicates that the source image is not a connected graph if (ans =-1 | ans-sum> = sum) puts ("impossible"); else printf ("% I64d % d \ n ", ans-sum, pos-m); puts ("");} return 0 ;}