HDU 5855 Less Time, More profit(网络流)

Problem Description
The city planners plan to build N plants in the city which has M shops.

Each shop needs products from some plants to make profit of proi units.

Building ith plant needs investment of payi units and it takes ti days.

Two or more plants can be built simultaneously, so that the time for building multiple plants is maximum of their periods(ti).

You should make a plan to make profit of at least L units in the shortest period.


Input
First line contains T, a number of test cases.

For each test case, there are three integers N, M, L described above.

And there are N lines and each line contains two integers payi, ti(1<= i <= N).

Last there are M lines and for each line, first integer is proi, and there is an integer k and next k integers are index of plants which can produce material to make profit for the shop.

1 <= T <= 30
1 <= N, M <= 200
1≤L,ti≤1000000000
1≤payi,proi≤30000


Output
For each test case, first line contains a line “Case #x: t p”, x is the number of the case, t is the shortest period and p is maximum profit in t hours. You should minimize t first and then maximize p.

If this plan is impossible, you should print “Case #x: impossible”


Sample Input
2

1 1 2
1 5
3 1 1

1 1 3
1 5
3 1 1


Sample Output
Case #1: 5 2

Case #2: impossible


题意:N个工厂,给出每个工厂建成需要的时间及花费payi(所有工厂可以同时开建)。M个商店,每个商店开张可以获利proi,(获利是一次性的),每个商店必须在给定的几个工厂建好的前提下才能开张。求获利L最少需要的天数T,以及T天的最大获利P。

思路:二分时间。对于每个时间t,源点到所有t时间内能开张的商店都连一条边,容量为proi,并记录sum(proi)。商店到他所依赖的工厂连一条边,容量无限大。t时间内能建好的工厂到汇点连一条边,容量为payi。则t时间的最大获利为sum(proi) - 最小割。


#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;

typedef long long LL;

LL ttt[2005];

LL n,m,l;

struct factory
{
    LL ti,p;
}gc[2005];

LL shop[2005];
LL sum=0;
vector<LL> v[2005];

const LL MAX_V = 2005;
const LL INFF = 0x7FFFFFFF;


LL min(LL a,LL b)
{
    if(a<b) return a;
    return b;
}

LL max(LL a,LL b)
{
    if(a>b) return a;
    return b;
}

#define inf 0x3f3f3f3f
#define INF 10000000

struct edge
{
    LL to;   //终点
    LL cap;    //容量
    LL rev;   //反向边
    edge() {}
    edge(LL t, LL c, LL r)
    {
        to = t;
        cap = c;
        rev = r;
    }
};

vector<edge> G[MAX_V];
int level[MAX_V];
bool vis[MAX_V];
int iter[MAX_V];
void add_edge(LL from, LL to, LL cap)
{
    //cout<<from<<"!"<<to<<"!"<<cap<<endl;
    G[from].push_back(edge(to, cap, (LL)G[to].size()));
    G[to].push_back(edge(from, 0, (LL)G[from].size() - 1));
}
LL dfs(LL u,LL t,LL f)
{
    if(u==t) return f;
    for(int &i=iter[u];i<G[u].size();i++)
    {
        edge &e=G[u][i];
        if(e.cap>0&&level[e.to]>level[u])
        {
            LL d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
void bfs(LL s)
{
    memset(level,inf,sizeof(level));
    memset(vis,0,sizeof(vis));
    queue<LL> q;
    q.push(s);
    level[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        LL u=q.front();
        vis[u]=0;
        q.pop();
        for(int i=0;i<G[u].size();i++)
        {
            edge &e=G[u][i];
            if(e.cap>0&&level[e.to]>level[u]+1)
            {
                level[e.to]=level[u]+1;
                if(!vis[e.to])
                {
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
    }
}
LL max_flow(LL s,LL t)
{
    LL res=0;
    while(1)
    {
        bfs(s);
        if(level[t]==inf) return res;
        memset(iter,0,sizeof(iter));
        LL f;
        while((f=dfs(s,t,INF))>0)
            res+=f;
    }
}

void built(LL t)
{
    sum=0;
    for(int i=0;i<=n+m+1;i++)
    {
        G[i].clear();
    }
    for(LL i=1;i<=m;i++)
    {
        int flag=0;
        for(int j=0;j<v[i].size();j++)
        {
            if(t<gc[v[i][j]].ti)
            {
                flag=1;
                break;
            }
            add_edge(i,v[i][j]+m,INFF);
        }
        if(flag==0)
        {
            add_edge(0,i,shop[i]);
            sum+=shop[i];
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(t>=gc[i].ti)
        {
            add_edge(i+m, n+m+1, gc[i].p);
        }
    }
}

bool check(LL t)
{
    if((sum-max_flow(0, n+m+1))>=l) return true;
    return false;
}
           
LL erfen(LL l, LL r)
{
    if(l == r)
    {
        built(l);
        if(check(l)) return l;
        return -1;
    }
    if(l+1 ==r)
    {
        built(l);
        if(check(l)) return l;
        return erfen(r,r);
    }
    LL mid = (l+r)/2;
    built(mid);
    if(check(mid)) r = mid;
    else l = mid+1;
    return erfen(l, r);
}


int main()
{
    LL t;
    LL cas=1;
    scanf("%I64d",&t);
    //scanf("%lld",&t);
    while(t--)
    {
        cin>>n>>m>>l;
        for(int i=0;i<=n+m+1;i++)
        {
            G[i].clear();
            v[i].clear();
        }
        LL maxt=0;
        for(LL i=1;i<=n;i++)
        {
            scanf("%I64d%I64d",&gc[i].p,&gc[i].ti);
            //scanf("%lld%lld",&gc[i].p,&gc[i].ti);
            maxt=max(maxt,gc[i].ti);
        }
        for(LL i=1;i<=m;i++)
        {
            scanf("%I64d",&shop[i]);
            //scanf("%lld",&shop[i]);
            sum+=shop[i];
            LL k;
            scanf("%I64d",&k);
            //scanf("%lld",&k);
            for(int j=0;j<k;j++)
            {
                LL to;
                scanf("%I64d",&to);
                //scanf("%lld",&to);
                v[i].push_back(to);
            }
        }
        LL anst=erfen(0,maxt);
        printf("Case #%I64d: ",cas++);
        //printf("Case #%lld: ",cas++);
        if(anst==-1) printf("impossible\n");
        else
        {
            built(anst);
            LL res=sum-max_flow(0, n+m+1);
            printf("%I64d %I64d\n",anst,res);
            //printf("%lld %lld\n",anst,res);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值