Minimum tree diagram, test template ....
2248. Channel Designtime limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 2199 Accepted Runs: 740
We need irrigate our farms, but there are 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. Other N-1 nodes in the indicate of the farms we need to irrigate. An edge represents you can build a channel between the nodes, to irrigate the target. The integers indicate the cost of a channel between and the nodes.
Figure (b) represents a design of channels with minimum cost.
Input
there is multiple cases, the first line of all case contains integers N and m (2≤ N ≤100; 1≤ m ≤ 10000), N shows the number of nodes. The following M lines, each line contains three integers i j cij , means we can build a channel from node vi to node vj , which cost c IJ . (1≤ i , J ≤ N ; i ≠ j ; 1≤ cij ≤100)
The source of water is always V1.
The input is terminated by N = 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:2015 July 04 Saturday 23:35 05 seconds 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=0x3f3f3f3f;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%d", &n,&m)!=eof {if (n==0&&m==0) break;for (int i=0;i<m;i++) {int u,v,w;scanf ("%d%d%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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Tju 2248. Channel Design Minimum tree diagram