zoukankan      html  css  js  c++  java
  • Uva 11183

    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
    ", res);
    93         else printf("Possums!
    ");
    94     }
    95     return 0;
    96 }
     

  • 相关阅读:
    Multi ingress controller
    Obtain file system information
    What is the difference between “inode size” and “Bytes per inode”
    Build and install fluent-bit on CentOS7
    Failed to initialize NVML: Driver/library version mismatch.
    利用multiprocessing.managers开发跨进程生产者消费者模型
    [算法] 举一反三之n重复数组中找唯一m重复异类数
    HBase Thrift过滤语法
    kubernetes pod infra container网络原理
    CentOS7上手动部署入门级kubernetes
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3218927.html
Copyright © 2011-2022 走看看