Uva 11183 - Teen Girl Squad (最小树形图)

Problem I
Teen Girl Squad 
Input: Standard Input Output: Standard Output 

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<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, uv 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.


Problem setter: Igor Naverniouk
 
解题思路:最小树形图 模板题
 
 1 #include<cstdio>
 2 #include<cstring>
 3 #define SIZE 1002
 4 #define MAXN 40004
 5 
 6 using namespace std;
 7 
 8 const int INF = 1<<30;
 9 int nv, ne, N;
10 int vis[SIZE], pre[SIZE], num[SIZE];
11 int inLength[SIZE];
12 
13 struct Edge{
14     int u, v, w;
15 }edge[MAXN];
16 
17 bool Traverse(int& res)
18 {
19     int root = 0;
20     while(true)
21     {
22         for(int i=0; i<nv; ++i) inLength[i] = INF;
23         for(int i=0; i<ne; ++i)
24         {
25             int& u = edge[i].u;
26             int& v = edge[i].v; 
27             if(edge[i].w < inLength[v] && u != v)
28             {
29                 inLength[v] = edge[i].w;
30                 pre[v] = u;
31             }
32         }
33         for(int i=0; i<nv; ++i)
34         if(i != root && inLength[i] == INF) return false;
35         int newnum = 0;
36         inLength[root] = 0;
37         memset(vis, -1, sizeof(vis));
38         memset(num, -1, sizeof(num));
39         for(int i=0; i<nv; ++i)
40         {
41             res += inLength[i];
42             int v = i;
43             while(vis[v] != i && num[v] == -1 && v != root)
44             {
45                 vis[v] = i;
46                 v = pre[v];
47             }
48             if(vis[v] == i)
49             {
50                 for(int u=pre[v]; u != v; u = pre[u])
51                     num[u] = newnum;
52                 num[v] = newnum++;
53             }
54         }    
55         if(newnum == 0) return true;
56         for(int i=0; i<nv; ++i)
57         if(num[i] == -1) num[i] = newnum++;
58         for(int i=0; i<ne; ++i)
59         {
60             int u = edge[i].u;
61             int v = edge[i].v;
62             edge[i].u = num[u];
63             edge[i].v = num[v];
64             if(edge[i].u != edge[i].v)
65                 edge[i].w -= inLength[v];
66         }
67         nv = newnum;
68         root = num[root];
69     }
70 
71 }
72 
73 int main()
74 {
75     #ifndef ONLINE_JUDGE
76     freopen("input.txt", "r", stdin);
77     #endif
78     scanf("%d", &N);
79     for(int t = 1; t <= N; ++t)
80     {
81         int u, v, w;
82         scanf("%d%d", &nv, &ne);
83         for(int i=0; i<ne; ++i)
84         {
85             scanf("%d%d%d", &u, &v, &w);
86             edge[i].u = u;
87             edge[i].v = v;
88             edge[i].w = u == v ? INF+1 : w;
89         }
90         printf("Case #%d: ", t);
91         int res = 0;
92         if(Traverse(res)) printf("%d\n", res);
93         else printf("Possums!\n");
94     }
95     return 0;
96 }

 

 

转载于:https://www.cnblogs.com/liaoguifa/p/3218927.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值