zoukankan      html  css  js  c++  java
  • HDU 6126.Give out candies 最小割

    Give out candies

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
    Total Submission(s): 218    Accepted Submission(s): 66


    Problem Description
    There are n children numbered 1 to n, and HazelFan's task is to give out candies to the children. Each children must be given at least 1 and at most m candies. The children raised k pieces of requirements, and every piece includes three integers x,y,z, means that the number of candies given to the child numbered x, subtract the number of candies given to the child number y, the result should not be more than z. Besides, if the child numbered i is given j candies, he or she will get wi,jsatisfaction score, now you need to help HazelFan maximize the sum of the satisfaction scores of these children.
     
    Input
    The first line contains a positive integer T(1T5), denoting the number of test cases.
    For each test case:
    The first line contain three positive integers n,m,k(1n,m50,1k150).
    The next n lines, the ith line contains m positive integers, the jth integer denotes wi,j.
    The next k lines, each line contains three integers x,y,z(1x,yn,z∣<233), denoting a piece of requirement.
     
    Output
    For each test case:
    A single line contains a nonnegative integer, denoting the answer. Particularly, if there is no way to give out candies, print -1.
     
    Sample Input
    2
    2 1 1
    1
    1
    1 2 1
    3 3 2
    1 2 3
    2 3 1
    3 1 2
    1 2 0
    2 3 0
     
    Sample Output
    2
    7

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6126

    题意:有n个人,每个人i获得j个糖果会有一个满意度wij,现在有一些约数条件xi,yi,zi,即xi获得的糖果数量-yi获得的糖果数量不超过zi,求最大满意度。

    思路:最小割。应为求的是最大满意度,说以先要化为最小。约数条件就建立inf边,这样就不会进行穿过inf边的分割,约数条件也就成立了。具体建图看代码。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> P;
    #define PI acos(-1.0)
    const int maxn=1e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e18+7;
    struct edge
    {
        int from,to,cap,flow;
    };
    vector<edge>es;
    vector<int>G[maxn];
    bool vis[maxn];
    int dist[maxn];
    int iter[maxn];
    void init(int n)
    {
        for(int i=0; i<=n+10; i++) G[i].clear();
        es.clear();
    }
    void addedge(int from,int to,int cap)
    {
        es.push_back((edge)
        {
            from,to,cap,0
        });
        es.push_back((edge)
        {
            to,from,0,0
        });
        int x=es.size();
        G[from].push_back(x-2);
        G[to].push_back(x-1);
    }
    bool BFS(int s,int t)
    {
        memset(vis,0,sizeof(vis));
        queue <int> Q;
        vis[s]=1;
        dist[s]=0;
        Q.push(s);
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            for (int i=0; i<G[u].size(); i++)
            {
                edge &e=es[G[u][i]];
                if (!vis[e.to]&&e.cap>e.flow)
                {
                    vis[e.to]=1;
                    dist[e.to]=dist[u]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int u,int t,int f)
    {
        if(u==t||f==0) return f;
        int flow=0,d;
        for(int &i=iter[u]; i<G[u].size(); i++)
        {
            edge &e=es[G[u][i]];
            if(dist[u]+1==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>0)
            {
                e.flow+=d;
                es[G[u][i]^1].flow-=d;
                flow+=d;
                f-=d;
                if (f==0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t)
    {
        int flow=0;
        while(BFS(s,t))
        {
            memset(iter,0,sizeof(iter));
            int d=0;
            while(d=DFS(s,t,inf)) flow+=d;
        }
        return flow;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n,m,k;
            scanf("%d%d%d",&n,&m,&k);
            int s=0,t=n*m+1;
            for(int i=1; i<=n; i++)
            {
                addedge(s,(i-1)*m+1,inf);
                for(int j=1; j<=m; j++)
                {
                    int x;
                    scanf("%d",&x);
                    if(j<m) addedge((i-1)*m+j,(i-1)*m+j+1,10000-x);
                    else addedge((i-1)*m+j,t,10000-x);
                }
            }
            for(int i=1; i<=k; i++)
            {
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                for(int i=1; i<=m; i++)
                {
                    for(int j=1; j<=m; j++)
                    {
                        if(i-j>z)
                        {
                            if(j<m) addedge((x-1)*m+i,(y-1)*m+j+1,inf);
                            else addedge((x-1)*m+i,t,inf);
                        }
                    }
                }
            }
            int ans=Maxflow(s,t);
            if(ans>=inf) printf("-1
    ");
            else printf("%d
    ",10000*n-ans);
            init(n*m);
        }
        return 0;
    }
    最小割
  • 相关阅读:
    教研室课题卫星通信系统
    html5学习笔记03. Canvas简介,Canvas的使用方法
    ARCGIS RUNTIME FOR IOS总结(一)
    ARCGIS RUNTIME FOR IOS总结(三)
    html5学习笔记05.JavaScript 中的面向对象,继承和封装
    JAVA排序算法之 选择排序
    ARCGIS RUNTIME FOR IOS总结(六)
    ASP upload
    问题一百三十:字符矩阵排序
    美妙的微机原理2013/5/1
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7383977.html
Copyright © 2011-2022 走看看