zoukankan      html  css  js  c++  java
  • POJ 3723

    Time Limit: 1000MS Memory Limit: 65536K

    Description

    Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

    Input

    The first line of input is the number of test case.
    The first line of each test case contains three integers, NM and R.
    Then R lines followed, each contains three integers xiyi and di.
    There is a blank line before each test case.

    1 ≤ NM ≤ 10000
    0 ≤ R ≤ 50,000
    0 ≤ xi < N
    0 ≤ yi < M
    0 < di < 10000

    Output

    For each test case output the answer in a single line.

    Sample Input

    2
    
    5 5 8
    4 3 6831
    1 3 4583
    0 0 6592
    0 1 3063
    3 3 4975
    1 3 2049
    4 2 2104
    2 2 781
    
    5 5 10
    2 4 9820
    3 2 6236
    3 1 8864
    2 4 8326
    2 0 5156
    2 0 1463
    4 1 2439
    0 4 4373
    3 4 8889
    2 4 3133
    

    Sample Output

    71071
    54223

    运用kruskal算法的最大生成树的基础题,直接1A。

    具体请参考:http://www.cnblogs.com/liangrx06/p/5083763.html

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int n,m,r;
     5 struct Node{
     6     int x,y,d;
     7 }node[50000+5];
     8 int par[20000+5];
     9 bool cmp(Node x,Node y){return x.d>y.d;}
    10 void init()
    11 {
    12     sort(node+1,node+r+1,cmp);//按d从大到小排列,以满足最大生成树的要求
    13     for(int i=1;i<=n+m;i++) par[i]=i;//初始化并查集
    14 }
    15 int find(int x){return( par[x]==x ? x : par[x]=find(par[x]) );}
    16 int kruskal()
    17 {
    18     int d_sum=0;
    19     for(int i=1;i<=r;i++)
    20     {
    21         int x=find(node[i].x),y=find(node[i].y);
    22         if(x != y)//找到一条属于最大生成树的新边
    23         {
    24             par[y]=x;//合并
    25             d_sum+=node[i].d;//这条边属于最大生成树,即对应的题目中要使用的“关系”( ‵▽′)ψ
    26         }
    27     }
    28     return d_sum;
    29 }
    30 int main()
    31 {
    32     int t;
    33     scanf("%d",&t);
    34     while(t--)
    35     {
    36         scanf("
    
    %d%d%d",&n,&m,&r);// n个girl , m个boy , r句话 
    37         for(int i=1;i<=r;i++)
    38         {
    39             int x,y,d;
    40             scanf("
    %d%d%d",&x,&y,&d); //girl - x ; boy - y 
    41             node[i].x=x+1,node[i].y=n+1+y;//这样可以确保男女孩的编号必然是不同的
    42             node[i].d=d;
    43         }
    44         init();
    45         int d_sum=kruskal();
    46         printf("%d
    ",(n+m)*10000 - d_sum);
    47     }
    48 }
  • 相关阅读:
    apache 问题 You don't have permission to access /test.php on this server 解决方法
    setTimeout和setInterval实现定时器的区别
    视图Ext.Viewport和窗口Ext.Window用法
    JavaScript设置Cookie
    布局Layout
    html中select标签刷新后不回到默认值而是保持之前选择值
    设置session失效的几种方法
    面板Ext.Panel使用
    树TreePanel
    让html元素随浏览器的大小自适应垂直居中
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804149.html
Copyright © 2011-2022 走看看