zoukankan      html  css  js  c++  java
  • 【UVA 11183】 Teen Girl Squad (定根MDST)

    【题意】

      输入三元组(X,Y,C),有向图,定根0,输出MDST。

    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

    【分析】

      裸的MDST。

      哇,自己打一下真是各种bug orz。。。

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 using namespace std;
     8 #define Maxn 1010
     9 #define Maxm 40010
    10 #define INF 0xfffffff
    11 
    12 struct node
    13 {
    14     int x,y,c;
    15 }t[Maxm];
    16 
    17 int in[Maxn],vis[Maxn],id[Maxn],pre[Maxn];
    18 
    19 int n,m,rt;
    20 
    21 int MDST()
    22 {
    23     int ans=0;
    24     rt=1;
    25     while(1)
    26     {
    27         for(int i=1;i<=n;i++) in[i]=INF;
    28         for(int i=1;i<=m;i++)
    29         {
    30             int x=t[i].x,y=t[i].y;
    31             if(t[i].c<in[y]&&x!=y)
    32             {
    33                 in[y]=t[i].c;
    34                 pre[y]=x;
    35             }
    36         }
    37         for(int i=1;i<=n;i++) if(i!=rt&&in[i]==INF) return -1;
    38         memset(vis,-1,sizeof(vis));
    39         memset(id,-1,sizeof(id));
    40         int cnt=0;
    41         for(int i=1;i<=n;i++) if(i!=rt)
    42         {
    43             ans+=in[i];
    44             int now=i;
    45             while(vis[now]!=i&&id[now]==-1&&now!=rt)
    46             {
    47                 vis[now]=i;
    48                 now=pre[now];
    49             }
    50             if(now!=rt&&id[now]==-1)
    51             {
    52                 cnt++;
    53                 for(int j=pre[now];j!=now;j=pre[j])
    54                     id[j]=cnt;
    55                 id[now]=cnt;
    56             }
    57         }
    58         if(cnt==0) break;
    59         for(int i=1;i<=n;i++) if(id[i]==-1) id[i]=++cnt;
    60         for(int i=1;i<=m;i++)
    61         {
    62             int x=t[i].x,y=t[i].y;
    63             t[i].x=id[x];t[i].y=id[y];
    64             if(t[i].x!=t[i].y) t[i].c-=in[y];
    65         }
    66         rt=id[rt];
    67         n=cnt;
    68         
    69     }
    70     return ans;
    71 }
    72 
    73 int main()
    74 {
    75     int T,kase=0;
    76     scanf("%d",&T);
    77     while(T--)
    78     {
    79         scanf("%d%d",&n,&m);
    80         for(int i=1;i<=m;i++)
    81         {
    82             scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].c);
    83             t[i].x++;t[i].y++;
    84         }
    85         printf("Case #%d: ",++kase);
    86         int x=MDST();
    87         if(x==-1) printf("Possums!
    ");
    88         else printf("%d
    ",x);
    89     }
    90     return 0; 
    91 }
    View Code

    2016-11-01 14:42:51

  • 相关阅读:
    python学习之路-day1-python基础1
    JSON.NET基本使用
    tortoiseSVN 设置ignore
    一个简单的身份证校验
    一个HttpWebRequest工具类
    linq to NHibernate
    python-plot and networkx绘制网络关系图
    DDoS攻击及防御措施
    白帽子原则
    认识特洛伊木马
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6019370.html
Copyright © 2011-2022 走看看