zoukankan      html  css  js  c++  java
  • POJ 3723.Conscription 最小生成树

    Conscription
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13438   Accepted: 4699

    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
    题目链接:http://poj.org/problem?id=3723
    题意:招收n个女的和m个男的,每招收一个人付出10000元,现在他(她)们之间有关系,如果x,y之间的关系为d,当招收了x后在招收y,
    则y的招收费用为10000-d;同理,当招收了y之后再招收x,则x的招收费用为10000-d。因为招收顺序不同,招收总费用也会不同,求出招收费用最低。
    思路:关系之间建立一条无向边,利用某些关系招收会形成一个生成树,当要招收总费用最低,那么需要生成树的边权值最大。如果关系图的权值取负数,
    就是求最小生成树。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<set>
    using namespace std;
    typedef pair<int,int> P;
    typedef long long ll;
    const int maxn=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e13+7;
    struct edge
    {
        int from,to;
        int cost;
    };
    edge es[maxn];
    vector<edge>G[maxn];
    int used[maxn];
    priority_queue<P,vector<P>,greater<P> >que;
    void addedge(int i,int u,int v,int w)
    {
        es[i].from=u,es[i].to=v,es[i].cost=w;
        G[u].push_back(es[i]);
    }
    int prim(int s)
    {
        int ans=0;
        que.push(P(0,s));
        while(!que.empty())
        {
            P p=que.top();
            que.pop();
            int u=p.second,d=p.first;
            if(used[u]) continue;
            used[u]=1;
            ans+=d;
            for(int i=0; i<G[u].size(); i++)
            {
                edge e=G[u][i];
                if(used[e.to]) continue;
                que.push(P(e.cost,e.to));
            }
        }
        return ans;
    }
    int main()
    {
        int t;
        int n,m,r;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d",&n,&m,&r);
            for(int i=0;i<n+m;i++)
            {
                G[i].clear();
                used[i]=0;
            }
            for(int i=1; i<=r; i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(i,u,v+n,-w);
                addedge(i,v+n,u,-w);
            }
            int ans=0;
            for(int i=0; i<n+m; i++)
                if(!used[i]) ans+=prim(i);
            printf("%d
    ",(n+m)*10000+ans);
        }
        return 0;
    }
    最小生成树prim算法
    
    
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    查询计划Hash和查询Hash
    执行计划的重用
    执行计划组件、组件、老化
    执行计划的生成
    查询反模式
    T-SQL 公用表表达式(CTE)
    SQL 操作结果集 -并集、差集、交集、结果集排序
    SQL语句
    POJ 1821 单调队列+dp
    区间gcd问题 HDU 5869 离线+树状数组
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7058681.html
Copyright © 2011-2022 走看看