hdu 4322 最大费用流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std; const int maxe = ;
const int maxn = ;
const int INF = 0x3f3f3f; struct Edge{
int u,v,flow,cap,cost;
int next;
Edge(int u=,int v=,int flow=,int cap=,int cost=,int next=):
u(u), v(v), flow(flow), cap(cap), cost(cost), next(next) {}
}; struct MCMF{
int d[maxn];
bool inq[maxn];
Edge edges[maxe];
int head[maxn],cnt;
int pa[maxn]; //用于回溯找增广路。
int res[maxn]; void init(){
memset(head,-,sizeof(head));
cnt = ;
} void addedge(int u,int v,int cap,int cost){
edges[cnt] = Edge(u,v,,cap,cost,head[u]);
head[u] = cnt++;
edges[cnt] = Edge(v,u,,,-cost,head[v]);
head[v] = cnt++;
} bool SPFA(int s,int t,int& flow,int& cost){
memset(inq,,sizeof(inq));
memset(d,-0x3f,sizeof(d)); //这个才是WA无数次的原因,看别人的代码才知道。
queue<int> Q;
Q.push(s);
d[s] = ; inq[s] = true;
res[s] = INF; res[t] = ;
while(!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = false;
for(int i=head[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(e.cap > e.flow && d[e.v] < d[u] + e.cost){
d[e.v] = d[u] + e.cost;
pa[e.v] = i;
res[e.v] = min(res[u],e.cap-e.flow);
if(!inq[e.v]){
Q.push(e.v); inq[e.v] = true;
}
}
} }
if(res[t] == ) return false;
flow += res[t];
cost += res[t]*d[t];
for(int i=t;i!=s;i=edges[pa[i]].u){
edges[pa[i]].flow += res[t];
edges[pa[i]^].flow -= res[t];
}
return true;
} bool MaxCost(int s,int t,int N,int sumB){
int flow = ,cost = ;
while(SPFA(s,t,flow,cost)) {} return N - flow >= sumB - cost;
}
}solver; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int cas=;cas<=T;cas++){
solver.init();
int N,M,K;
cin>>N>>M>>K;
int B[maxn];
int sumB = ;
for(int i=;i<=M;i++) { cin>>B[i]; sumB += B[i]; }
int s = , t = N+M+;
for(int i=;i<=N;i++) solver.addedge(s,i,,); int like[maxn][maxn];
for(int i=; i<=M; i++)
for(int j=; j<=N; j++){
scanf("%d",&like[i][j]);
if(like[i][j])
solver.addedge(j,i+N,,);
} /**
for(int i=1;i<=M;i++)
for(int j=1;j<=N;j++){
int flag;
scanf("%d",&flag);
if(flag) solver.addedge(j,i+N,1,0);
}
这个代码与上面的那段功能相同,但我不知道为啥会错。我怀疑是oj的问题。
**/
for(int i=;i<=M;i++){
solver.addedge(N+i,t,B[i]/K,K);
if(B[i]%K > )
solver.addedge(N+i,t,,B[i]%K); }
if(solver.MaxCost(s,t,N,sumB)) printf("Case #%d: YES\n",cas);
else printf("Case #%d: NO\n",cas);
}
}
hdu 4322 最大费用流的更多相关文章
- hdu 4322(最大费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 思路:建图真的是太巧妙了!直接copy大牛的了: 由于只要得到糖就肯定有1个快乐度,在这一点上糖 ...
- Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)
Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Coding Contest HDU - 5988(费用流)
题意: 有n个区域和m条路,每个区域有a[i]个人和b[i]个食物,然后是m条路连接两个区域,这条路容量为cap,这条路断掉的概率为p,第一个经过的时候一定不会断,后面的人有概率p会断,现在需要所有人 ...
- HDU 4862 Jump 费用流
又是一个看了题解以后还坑了一天的题…… 结果最后发现是抄代码的时候少写了一个负号. 题意: 有一个n*m的网格,其中每个格子上都有0~9的数字.现在你可以玩K次游戏. 一次游戏是这样定义的: 你可以选 ...
- HDU 4862(费用流)
Problem Jump (HDU4862) 题目大意 给定一个n*m的矩形(n,m≤10),每个矩形中有一个0~9的数字. 一共可以进行k次游戏,每次游戏可以任意选取一个没有经过的格子为起点,并且跳 ...
- Going Home HDU - 1533 费用流
http://acm.hdu.edu.cn/showproblem.php?pid=1533 给一个网格图,每两个点之间的匹配花费为其曼哈顿距离,问给每个的"$m$"匹配到一个&q ...
- Tour HDU - 3488 有向环最小权值覆盖 费用流
http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...
- HDU 5988 Coding Contest(费用流+浮点数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...
- HDU 5988 Coding Contest(浮点数费用流)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...
随机推荐
- centos 6.x 安装redis
1.yum 安装 yum install redis 如果提示找不到包的话 可以yum install epel-release 先安装epel第三方库 2.源码安装 https://redis ...
- 关于JavaScript的类的继承
其实最一开始学JS的时候就看过继承的实现.当时只是去试着理解从书上看来的代码段而已.今天又重新思考了一下,感觉这是一个思维探索演进的结果. 继承,即复用. 如果抛开继承的固有思想,让b复用a的成员,最 ...
- 342. Power of Four
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
- git config找到下载的路径URL
有时想在别的机器上下载自己的上传的源代码,发现git不像svn那么方便一眼就能看到下载地址,这两天在网上查了一下,终于找到方法了. 一般本地的github目录下总会有一个.git目录,点击进入,可 ...
- extern "C" {} 来沟通C和C++
比如说你用C++开发了一个DLL库,为了能够让C语言也能够调用你的DLL输出(Export)的函数,你需要用extern "C"来强制编译器不要修改你的函数名. 通常,在C语言的头 ...
- 数组去重的三种方法及from方法
直接上代码: var str="adbbckddwerivka"; var arr=str.split(""); console.log(arr); //ind ...
- SVN版本回滚~
如果你在svn上对文件进行编辑作了修改,想撤销,那么有两种方法可以还原:1) svn revert <yourfile>2) 手动删除该文件,重新执行svn up(rm <yourf ...
- 使用date命令,进行时间戳和日期时间的互转
首先是知道时间转成时间戳 date -d "2014-01-16 12:30:11" +%s - :: - :: 其次是知道时间戳,想要知道当时的时间 date -d '1970- ...
- LINUX单网卡绑定多个IP
在linux下,我们有时候需要给单网卡设置不同的IP地址,这样就涉及到单网卡绑定多个IP地址的情况.使用本方法可以方便的为单网卡绑定多个IP地址.笔者使用的环境是centos5.6,应该在fedora ...