UVa11183 Teen Girl Squad, minimum tree structure, zhu liu algorithm, uva11183 Tree Structure

Source: Internet
Author: User

UVa11183 Teen Girl Squad, minimum tree structure, zhu liu algorithm, uva11183 Tree Structure

Teen Girl Squad
Input:
Standard Input

Output:Standard Output

You are part of a groupNTeenage girls armed with cellphones. you have some news you want to tell everyone in the group. the problem is that no two of you are in the same room, and you must communicate using only cellphones. what's worse is that due to excessive usage, your parents have refused to pay your cellphone bills, so you must distribute the news by calling each other in the cheapest possible way. you will call several of your friends, they will call some of their friends, and so on until everyone in the group hears the news.

Each of you is using a different phone service provider, and you know the price of girl A calling girl B for all possible A and B. not all of your friends like each other, and some of them will never call people they don't like. your job is to find the cheapest possible sequence of CILS so that the news spreads from you to allN-1 other members of the group.

Input

The first line of input gives the number of cases,N(N <1, 150 ).NTest cases follow. Each one starts with two lines containingN (0 <= N<= 1000) AndM (0 <= M<= 40,000) . Girls are numbered from 0 N-1 , And you are girl 0. The nextMLines will each contain 3 integers,U,VAndW, Meaning that a call from girlUTo girlVCostsWCents (0 <= W<= 1000) . No other CILS are possible because of grudges, rivalries and because they are, like, lame. The input file size is around 1200 KB.

Output

For each test case, output one line containing "Case #X: "Followed by the cost of the cheapest method of distributing the news. If there is no solution, print" Possums! "Instead.

Sample Input Sample Output

4
2
1
0 1 10
2
1
1 0 10
4
4
0 1 10
0 2 10
1 3 20
2 3 30
4
4
0 1 10
1 2 20
2 0 30
2 3 100
Case #1: 10
Case #2: Possums!
Case #3: 40
Case #4: 130
                  


Minimum Tree Structure

The minimum spanning tree of a directed graph and specifies the starting point.
Solution: 1. First, dfs determines that the starting point can reach any other point. Otherwise, no tree chart exists.
2. Find the smallest inbound edge for each vertex. If there is no ring, these edges constitute the smallest tree structure and are transferred to 4; otherwise, they are transferred to 3.
3. add the edge weight of each edge in the ring to ans and form a new vertex new. If there is an edge for multiple vertices in the ring <j, i> The Edge Weight of <j, new> is equal to all <j, I>-<pre [I], i> the smallest in (because after the point is reduced, the second graph must remove an edge from the ring <pre [I], I> then add a smallest edge <x, I>, in this way, we can ensure the correctness of the answer, which is clever and clear when we change the picture.), the edge weight of <new, j> = the minimum value of all <I, j>, the point is scaled down and switched to 2.
4. Cut n vertices and n-1 edges without loops. Now it is a connected tree. ans + = the edge weights of the n-1 edges have the answer;



The above is the "zhu liu algorithm" invented by Chinese people, and the complexity of the adjacent matrix (n ^ 3) the complexity of the critical table (VE ).

# Include <cmath> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <set> # include <map> # include <stack> # include <queue> # include <vector> # include <string> # define for0 (, b) for (a = 0; a <B; ++ a) # define for1 (a, B) for (a = 1; a <= B; ++) # define foru (I, a, B) for (I = a; I <= B; ++ I) # define ford (I, a, B) for (I = a; I> = B; -- I) using namespace std; typedef long ll; const int maxn = 1000 + 5; const I Nt maxm = 40000 + 5; const int INF = 1e9; struct Edge {int u, v, cost ;}; Edge edge [maxm]; int pre [maxn], id [maxn], vis [maxn], in [maxn]; int zhuliu (int root, int n, int m, Edge edge []) {int res = 0, u, v; int I, j; while (1) {for0 (I, n) in [I] = INF; for0 (I, m) if (edge [I]. u! = Edge [I]. v & edge [I]. cost <in [edge [I]. v]) {pre [edge [I]. v] = edge [I]. u; in [edge [I]. v] = edge [I]. cost;} for0 (I, n) if (I! = Root & in [I] = INF) return-1; // The minimum tree structure int tn = 0 cannot exist; memset (id,-1, sizeof id ); memset (vis,-1, sizeof vis); in [root] = 0; for0 (I, n) {res + = in [I]; 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] = tn; id [v] = tn ++;} if (tn = 0) break; // No directed ring for0 (I, n) if (id [I] =-1) id [I] = tn ++; for (I = 0; I <m;) {v = edge [I]. v; edge [I]. u = id [edge [I]. u]; edge [I]. v = id [edge [I]. v]; if (edge [I]. u! = Edge [I]. v) edge [I ++]. cost-= in [v]; else swap (edge [I], edge [-- m]);} n = tn; root = id [root];} return res ;} int g [maxn] [maxn]; int main () {# ifndef ONLINE_JUDGE freopen ("in. cpp "," r ", stdin); freopen (" out. cpp "," w ", stdout); # endif // ONLINE_JUDGE int n, m; int T, I, j; scanf (" % d ", & T ); for (int cas = 1; cas <= T; ++ cas) {scanf ("% d", & n, & m); for0 (I, n) for0 (j, n) g [I] [j] = INF; int u, v, c; while (m --) {s Canf ("% d", & u, & v, & c); if (u = v) continue; g [u] [v] = min (g [u] [v], c);} int e = 0; for0 (I, n) for0 (j, n) if (g [I] [j] <INF) {edge [e]. u = I; edge [e]. v = j; edge [e ++]. cost = g [I] [j];} int ans = zhuliu (0, n, e, edge); printf ("Case # % d:", cas ); if (ans =-1) printf ("Possums! \ N "); else printf (" % d \ n ", ans);} return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.