zoukankan      html  css  js  c++  java
  • POJ3723 Conscription

    Conscription

    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, N, M and R.
    Then R lines followed, each contains three integers xi, yi and di.
    There is a blank line before each test case.

    1 ≤ N, M ≤ 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
    

    翻译一下就是n个女孩,m个男孩,全部要招募,但是有些女孩x和男孩y有关系,如果已经招募了其中一个,另一个就只需要10000- d了,求最小花费。
    开始是想最小生成树,求招募m+n个人最小花费,预处理时用10000先减去d,再求最小生成树,但是图可能不是连通图,所以对每个联通子图求最小生成树,再加上树的个数 num*10000,当然,一个人也是一棵树,可就是一直wa。
    但是,如果反过来,求n+m个人能优惠的钱数,最后用总钱数再减去它就行了,也不用预处理。

    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    char buf[100000],*L=buf,*R=buf;
    #define gc() L==R&&(R=(L=buf)+fread(buf,1,100000,stdin),L==R)?EOF:*L++;
    template<typename T>
    inline void read(T&x) {
        char ch=gc();
        x=0;
        while (ch<'0'||ch>'9')
            ch=gc();
        while (ch>='0'&&ch<='9')
            x=(x<<3)+(x<<1)+ch-48,ch=gc();
    }
    
    const int MAXN=5e4+20;
    int fa[MAXN],Rank[MAXN];
    
    inline int find(int x) {
        while(fa[x]!=x)
            x=fa[x]=fa[fa[x]];
        return x;
    }
    inline void merge(int x,int y) {
        int xx=find(x),yy=find(y);
        if(xx==yy)return;
    
        if(Rank[xx]<Rank[yy])fa[xx]=yy;
    
        else {
            fa[yy]=xx;
            if(Rank[xx]==Rank[yy])++Rank[xx];
        }
    }
    inline void init() {
        for(int i=0; i<MAXN; ++i){
            fa[i]=i,Rank[i]=0;
        }
    }
    struct Node {
        int x,y,v;
        bool operator<(const Node&b)const {
            return v>b.v;
        }
    } p[MAXN];
    int main() {
        freopen("in.txt","r",stdin);
        int t;
        read(t);
        while(t--) {
            int n,m,r;
            read(n),read(m),read(r);
            init();
            for(int i=0; i<r; ++i) {
                read(p[i].x),read(p[i].y),read(p[i].v);
                p[i].y+=n;//女生: [0,n-1],男生: [n,m+n];
            }
            int ans=0;//优惠多少钱
            sort(p,p+r);
            for(int i=0; i<r; ++i) {
                if(find(p[i].x)==find(p[i].y))
                    continue;
                merge(p[i].x,p[i].y);
                ans+=p[i].v;
                //加个边数判断也行
            }
            printf("%d
    ",(m+n)*10000-ans);//总钱数减去优惠的钱数
        }
        return 0;
    }
    
    //正着算,居然过了
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    char buf[100000],*L=buf,*R=buf;
    #define gc() L==R&&(R=(L=buf)+fread(buf,1,100000,stdin),L==R)?EOF:*L++;
    template<typename T>
    inline void read(T&x) {
        char ch=gc();
        x=0;
        while (ch<'0'||ch>'9')
            ch=gc();
        while (ch>='0'&&ch<='9')
            x=(x<<3)+(x<<1)+ch-48,ch=gc();
    }
    
    const int MAXN=5e4+20;
    int fa[MAXN],Rank[MAXN];
    
    inline int find(int x) {
        while(fa[x]!=x)
            x=fa[x]=fa[fa[x]];
        return x;
    }
    inline void merge(int x,int y) {
        int xx=find(x),yy=find(y);
        if(xx==yy)return;
    
        if(Rank[xx]<Rank[yy])fa[xx]=yy;
    
        else {
            fa[yy]=xx;
            if(Rank[xx]==Rank[yy])++Rank[xx];
        }
    }
    inline void init() {
        for(int i=0; i<MAXN; ++i){
            fa[i]=i,Rank[i]=0;
        }
    }
    struct Node {
        int x,y,v;
        bool operator<(const Node&b)const {
            return v<b.v;
        }
    } p[MAXN];
    int main() {
        freopen("in.txt","r",stdin);
        int t;
        read(t);
        while(t--) {
            int n,m,r;
            read(n),read(m),read(r);
            init();
            for(int i=0; i<r; ++i) {
                read(p[i].x),read(p[i].y),read(p[i].v);
                p[i].y+=n;
                p[i].v=10000-p[i].v;
            }
            int ans=0;
            sort(p,p+r);
            for(int i=0; i<r; ++i) {
                if(find(p[i].x)==find(p[i].y))
                    continue;
                merge(p[i].x,p[i].y);
                ans+=p[i].v;
            }
            int num=0;
            for(int i=0;i<m+n;++i)
                if(fa[i]==i)++num;
            printf("%d
    ",ans+num*10000);
        }
        return 0;
    }
    
  • 相关阅读:
    JS和PYTHON中数据类型比较
    http状态码
    ffmpeg architecture(上)
    降低数值精度以提高深度学习性能
    IaaS、PaaS 和 SaaS:云服务模型概述
    英特尔Intel® Arria® 10 FPGA加速器设计
    基于至强® 平台的内存数据库解决方案
    MLPerf结果证实至强® 可有效助力深度学习训练
    如何从数据角度为人工智能部署做好数据准备
    英特尔内存革新助平安云 Redis 云服务降本增效
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14144860.html
Copyright © 2011-2022 走看看