UVA11183 Teen Girl Squad —— 最小树形图

题目链接:https://vjudge.net/problem/UVA-11183

 

You are part of a group of n teenage 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 calls so that the news spreads from you to all n-1 other members of the group.

 

Input

The first line of input gives the number of cases, N (N < 150). N test cases follow. Each one starts with two lines containing n (0 ≤ n ≤ 1000) and m (0 ≤ m ≤ 40, 000). Girls are numbered from 0 to n-1, and you are girl 0. The next m lines will each contain 3 integers, u, v and w, meaning that a call from girl u to girl v costs w cents (0 ≤ w ≤ 1000). No other calls 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

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

 

Sample Output

Case #1: 10

Case #2: Possums!

Case #3: 40

Case #4: 130

 

 

题解:

最小树形图,即有向图的最小生成树。因为题目要求可以有重边,所以对于两个点,如果之间有多条边(有向边,u-->v),选取权值最小的那条边。

 

代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 using namespace std;
  7 typedef long long LL;
  8 const double EPS = 1e-6;
  9 const int INF = 2e9;
 10 const LL LNF = 9e18;
 11 const int MOD = 1e9+7;
 12 const int MAXN = 1e3+10;
 13 
 14 struct Edge
 15 {
 16     int u, v, w;
 17 }edge[40010];
 18 
 19 int pre[MAXN], id[MAXN], vis[MAXN], in[MAXN];
 20 
 21 int zhuliu(int root, int n, int m)
 22 {
 23     int res = 0;
 24     while(1)
 25     {
 26         for(int i = 0; i<n; i++)    //初始化
 27             in[i] = INF;
 28         for(int i = 0; i<m; i++)   //为每个结点选择一条最小入边, 并记录它的上一个点。
 29         if(edge[i].u!=edge[i].v && edge[i].w<in[edge[i].v]) //第一个判断防止自环
 30         {
 31             pre[edge[i].v] = edge[i].u;
 32             in[edge[i].v] = edge[i].w;
 33         }
 34 
 35         for(int i = 0; i<n; i++)    //如果非根结点找不到至少一条入边,则建树失败
 36             if(i!=root && in[i]==INF)
 37                 return -1;
 38 
 39         int tn = 0; //tn为重建图后的结点个数,过程中为结点重新编号
 40         memset(id, -1, sizeof(id));
 41         memset(vis, -1, sizeof(vis));
 42         in[root] = 0;   //根节点的入度必须为0
 43         for(int i = 0; i<n; i++)    //将环缩成点
 44         {
 45             res += in[i];
 46             int v = i;
 47             //当vis[v]==i时,找到了环中第一个被访问的结点
 48             while(vis[v]!=i && id[v]==-1 && v!=root)
 49             {
 50                 vis[v] = i;
 51                 v = pre[v];
 52             }
 53             if(v!=root && id[v]==-1)
 54             {
 55                 for(int u = pre[v]; u!=v; u = pre[u])   //为整个换编上号
 56                     id[u] = tn;
 57                 id[v] = tn++;
 58             }
 59         }
 60         if(tn==0) break;    //如果不存在环, 则建树成功
 61         for(int i = 0; i<n; i++)    //为不在环内的结点编号
 62             if(id[i]==-1)
 63                 id[i] = tn++;
 64 
 65         for(int i = 0;  i<m; )  //重新建图
 66         {
 67             int v = edge[i].v;
 68             edge[i].u = id[edge[i].u];
 69             edge[i].v = id[edge[i].v];
 70             if(edge[i].u!=edge[i].v)
 71                 edge[i++].w -= in[v];
 72             else
 73                 swap(edge[i], edge[--m]);
 74         }
 75         n = tn; //更新结点个数及根节点
 76         root = id[root];
 77     }
 78     return res;
 79 }
 80 
 81 int g[MAXN][MAXN];
 82 int main()
 83 {
 84     int T, n, m;
 85     scanf("%d", &T);
 86     for(int kase = 1; kase<=T; kase++)
 87     {
 88         scanf("%d%d",&n,&m);
 89         for(int i = 0; i<n; i++)
 90         for(int j = 0; j<n; j++)
 91             g[i][j] = INF;
 92 
 93         for(int i = 0; i<m; i++)
 94         {
 95             int u, v, w;
 96             scanf("%d%d%d", &u, &v, &w);
 97             g[u][v] = min(g[u][v], w);
 98         }
 99 
100         int tot = 0;
101         for(int i = 0; i<n; i++)
102         for(int j = 0; j<n; j++)
103         {
104             if(i!=j && g[i][j]!=INF)
105                 edge[tot].u = i, edge[tot].v = j, edge[tot++].w = g[i][j];
106         }
107 
108         int ans = zhuliu(0, n, tot);
109         if(ans<0) printf("Case #%d: Possums!\n", kase);
110         else printf("Case #%d: %d\n", kase, ans);
111     }
112 }
View Code

 

posted on 2017-10-31 16:47  h_z_cong  阅读(352)  评论(0编辑  收藏  举报

导航