题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5855

Less Time, More profit

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> 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
> 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 1 1≤L,ti≤1000000000
> 1≤payi,proi≤30000
#### 输出
> 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,并且要ti天才能建完。

有m个商店,每个商店能盈利proi,并且需要若干个指定的工厂给它提供原料(缺一不可)。

现在问如何在最短的时间内收益最大。

题解

最大权闭合子图的裸题。(百度文库里面的算法合集之最小割的论文里面有详细介绍最大权闭合子图)

我们二分时间t,然后问题转化成了对满足条件的工厂与商店之间的图求一个最大权闭合子图的问题。我们按照最大权闭合子图的建图方式连边就行了。

建图:

增设源点0,汇点n+m+1。

源点向每个能够满足条件的商店(如果商店的一个需求工厂在时间上不满足条件,那么这个商店就不满足条件,直接忽视它)连边,容量为商店的利润。

商店向需求工厂建边,容量为无穷大。

工厂向汇点建边,容量为工厂的花费。

然后跑最大流,判断在二分的时间下,是否能创造大于等于L的利润。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
//#define lson (o<<1)
//#define rson ((o<<1)|1)
//#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++) typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=444; struct Edge {
int u,v,cap,flow;
Edge(int u,int v,int c,int f):u(u),v(v),cap(c),flow(f) {}
}; struct Dinic {
int n,m,s,t;
vector<Edge> egs;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void init(int n) {
this->n=n;
rep(i,0,n+1) G[i].clear();
egs.clear();
m=0;
} void addEdge(int u,int v,int cap) {
egs.pb(Edge(u,v,cap,0));
egs.pb(Edge(v,u,0,0));
m=egs.sz();
G[u].pb(m-2);
G[v].pb(m-1);
} bool BFS() {
clr(vis,0);
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()) {
int x=Q.front();
Q.pop();
rep(i,0,G[x].sz()) {
Edge& e=egs[G[x][i]];
if(!vis[e.v]&&e.cap>e.flow) {
vis[e.v]=1;
d[e.v]=d[x]+1;
Q.push(e.v);
}
}
}
return vis[t];
} int DFS(int x,int a) {
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x]; i<G[x].size(); i++) {
Edge& e=egs[G[x][i]];
if(d[x]+1==d[e.v]&&(f=DFS(e.v,min(a,e.cap-e.flow)))>0) {
e.flow+=f;
egs[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
} int Maxflow(int s,int t) {
this->s=s;
this->t=t;
int flow=0;
while(BFS()) {
clr(cur,0);
flow+=DFS(s,INF);
}
return flow;
}
} dinic; PII plants[maxn];
int pi[maxn],vis[maxn];
vector<int> G[maxn]; int n,m,L; int isok(int mid) {
dinic.init(n+m+2);
clr(vis,0);
for(int i=1; i<=m; i++) {
rep(j,0,G[i].size()) {
int v=G[i][j];
if(plants[v].Y>mid) {
vis[i]=1;
break;
}
}
}
int sumv=0;
for(int i=1; i<=m; i++) {
if(vis[i]) continue;
sumv+=pi[i];
dinic.addEdge(0,i,pi[i]);
rep(j,0,G[i].size()) {
int v=G[i][j];
dinic.addEdge(i,v+m,INF);
}
}
rep(i,1,n+1) {
if(plants[i].Y>mid) continue;
dinic.addEdge(i+m,m+n+1,plants[i].X);
}
return sumv-dinic.Maxflow(0,n+m+1);
} void init() {
rep(i,0,maxn) G[i].clear();
} int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--) {
scanf("%d%d%d",&n,&m,&L);
init();
rep(i,1,n+1) {
scanf("%d%d",&plants[i].X,&plants[i].Y);
}
rep(i,1,m+1) {
scanf("%d",&pi[i]);
int cnt;
scanf("%d",&cnt);
while(cnt--) {
int x;
scanf("%d",&x);
G[i].push_back(x);
}
}
printf("Case #%d: ",++kase);
int l=0,r=1000000000;
if(isok(r)<L) {
puts("impossible");
continue;
}
while(l+1<r) {
int mid=l+(r-l)/2;
if(isok(mid)>=L) r=mid;
else l=mid;
}
printf("%d %d\n",r,isok(r));
}
return 0;
} //end-----------------------------------------------------------------------
/*
5 3 50
1 1
2 2
3 3
4 4
5 5
100 2 1 4
1 0
1 0
*/

HDU 5855 Less Time, More profit 最大权闭合子图的更多相关文章

  1. Less Time, More profit 最大权闭合子图(最大流最小割)

    The city planners plan to build N plants in the city which has M shops. Each shop needs products fro ...

  2. HDU5855 Less Time, More profit(最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...

  3. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

  4. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  5. hdu 3917 Road constructions 最大权闭合子图

    样例说明: n(城市数目)   m(工程队数目) 每个工程队上交的税收 val[i] k(k个工程) xi   yi  ci  costi , 工程队ci承包由xi到yi,政府的补贴为costi 注意 ...

  6. 2018.11.06 NOIP训练 最大获利(profit)(01分数规划+最大权闭合子图)

    传送门 好题啊. ∑i<jpi,jK∗(200−K)>X\frac{\sum_{i<j}p_{i,j}}{K*(200-K)}>XK∗(200−K)∑i<j​pi,j​​ ...

  7. HDU 3879 Base Station(最大权闭合子图)

    将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...

  8. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  9. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...

随机推荐

  1. chkconfig设置开机自启动的原理

    开机自启动服务的原理$ sshd on #手动设置3级别的开机自启动 [leiyf@leiyangfeng ~] #手动设置3级别的开机自启动,实质是在对应运行级别的目录rc3.d下创建一个sshd的 ...

  2. STM32 HAL库学习系列第6篇---定时器TIM 级联配置

    应用情景 使用定时器配置编码器模式,发现STM32只有两个定时器是32位,16位的测量值不够用,发现是可以使用两个16位定时器级联为32位的. 我是在使用编码器计数电机转速时使用,但是最终实现的效果不 ...

  3. 树莓3B+_apt-get update && apt-get upgrade

    在Windows下安装软件,我们只需要有EXE文件,然后双击,下一步直接OK就可以了.但在LINUX下,不是这样的.每个LINUX的发行版,都会维护一个自己的软件仓库,我们常用的几乎所有软件都在这里面 ...

  4. 快速认识LinkIt 7697开发板

    LinkIt 7697是一款多功能且价格亲民的开发板,可用来连接网络或你的各项装置,同时提供Wi-Fi及蓝芽两种联机功能.此开发板采用MediaTek MT7697芯片,比起其他类似的Wi-Fi/蓝芽 ...

  5. 准确率(accuracy),精确率(Precision),召回率(Recall)和综合评价指标(F1-Measure )----转

    原文:http://blog.csdn.net/t710smgtwoshima/article/details/8215037   Recall(召回率);Precision(准确率);F1-Meat ...

  6. POJ3006-Dirichlet's Theorem on Arithmetic Progressions

    题意: 设一个等差数列,首元素为a,公差为d 现在要求输入a,d,n ,要求找出属于该等差数列中的第n个素数并输出 思路:空间换时间是个主旋律.素数表的生成用素数筛选法.方法是从2开始,对每个目前还标 ...

  7. 鼠标移动在屏幕上显示温度Tip提示功能-CToolTipCtrl类的使用

    初学VC++,太多知识不懂,需要不断的查找资料,想通过记录让自己有所积累,主要是怕以后会很快忘记.最近在做一个在屏幕上显示鼠标移动位置的温度值,我利用先缓存一帧图像的温度值,然后,通过鼠标移动消息相应 ...

  8. Lingo安装

    Lingo安装 Lingo简介        LINGO是Linear Interactive and General Optimizer的缩写,即"交互式的线性和通用优化求解器" ...

  9. python2.7入门---JSON

        这次我们来看如何使用 Python 语言来编码和解码 JSON 对象.首先,我们得了解,JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读 ...

  10. 防360TAB页面的样式页面

    今天给朋友做了一个仿照360新tab页面的效果,主要就是一些样式和JQUERY的应用,超级简单,现在把源码放出来 源码下载