TJU 2248. minimum tree structure of Channel Design, tju Tree Structure

Source: Internet
Author: User

TJU 2248. minimum tree structure of Channel Design, tju Tree Structure


Minimum tree structure, test template ....


2248. Channel DesignTime Limit: 1.0 Seconds Memory Limit: 65536 K
Total Runs: 2199 Accepted Runs: 740

We need irrigate our farms, but there is only one source of water nearby. So we need build some water channels with minimum cost.

In Figure (A),V1 indicates the source of water. OtherN-1 nodes in the Figure indicate the farms we need to irrigate. an edge represents you can build a channel between the two nodes, to irrigate the target. the integers indicate the cost of a channel between two nodes.

Figure (B) Represents a design of channels with minimum cost.

Input

There are multiple cases, the first line of each case contains two integersNAndM(2 ≤N≤ 100; 1 ≤M≤ 10000 ),NShows the number of nodes. The followingMLines, each line contains three integersI j cIj, Means we can build a channel from nodeVITo nodeVJ, Which costCIj. (1 ≤I,JN;I=J; 1 ≤CIj≤ 100)

The source of water is alwaysV1.
The input is terminatedN=M= 0.

Output

For each case, output a single line contains an integer represents the minimum cost.

If no design can irrigate all the farms, output "impossible" instead.

Sample Input
5 81 2 31 3 52 4 23 1 53 2 53 4 43 5 75 4 33 31 2 31 3 53 2 10 0

Sample Output
176

Problem setter:Hill



Source:  TJU Contest August 2006 Submit List Runs Forum Statistics



/*************************************** * ******** Author: CKbossCreated Time:, Saturday, July 04, 2015 File Name: TJU2248.cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> using namespace std; const int INF = 0x3f3 F3f3f; const int maxn = 110; int n, m; struct Edge {int u, v, cost;}; Edge edge [maxn * maxn]; int pre [maxn], id [maxn], vis [maxn], in [maxn]; int zhuliu (int root, int n, int m, Edge edge []) {int res = 0, u, v; while (true) {for (int I = 0; I <n; I ++) in [I] = INF; for (int I = 0; I <m; I ++) {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 ;}}for (int I = 0; I <n; I ++) if (I! = Root & in [I] = INF) return-1; int tn = 0; memset (id,-1, sizeof (id); memset (vis, -1, sizeof (vis); in [root] = 0; for (int I = 0; I <n; I ++) {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; for (int I = 0; I <n; I ++) if (id [I] =-1) id [I] = tn ++; for (int 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]; elseswap (edge [I], edge [-- m]);} n = tn; root = id [root];} return res ;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout ); while (scanf ("% d", & n, & m )! = EOF) {if (n = 0 & m = 0) break; for (int I = 0; I <m; I ++) {int u, v, w; scanf ("% d", & u, & v, & w); u --; v --; edge [I]. u = u; edge [I]. v = v; edge [I]. cost = w;} int ans = zhuliu (0, n, m, edge); if (ans =-1) puts ("impossible "); else printf ("% d \ n", ans);} return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.