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比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...
随机推荐
- Eclipse 打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素(转)
打开 Eclipse 时,如图所示: 解决办法: 把有问题的 devices.xml 文件删除,再把 sdk 里面 tools\lib 下的这个文件拷贝到你删除的那个文件夹里,重启 eclipse 就 ...
- What and where are the stack and heap?
The stack is the memory set aside as scratch space for a thread of execution. When a function is cal ...
- basicAnimation移动图形
目的:采用CABasicAnimation 点击屏幕上的点来是实现图像的位置移动 并且位置能够不反弹 难点:1 通过动画的KeyPath找到layer的属性 2 通过NSValue将点包装成对象 ...
- POJ 2391.Ombrophobic Bovines (最大流)
实际上是求最短的避雨时间. 首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数. floyd求任意两点互相到达的最短时间,二分最长时间, ...
- jQuery慢慢啃之属性(三)
1.attr(name|properties|key,value|fn)设置或返回被选元素的属性值. $("img").attr("src");//获取属性 $ ...
- linux之uniq
Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...
- TestNG Listener
常用接口 IExecutionListener 监听TestNG运行的启动和停止. IAnnotationTransformer 注解转换器,用于TestNG测试类中的注解. ISuiteList ...
- 关于使用iframe标签自适应高度的使用
在ifrome内设定最小高度,(此方法只适用于页面内切换高度不一.但是会保留最大高度,返回后保持最大高度不再回到最初页面的高度) <iframe id="one4" widt ...
- Asp.Net MVC5 格式化输出时间日期
刚好用到这个,网上找的全部是输出文本框内容的格式化日期时间 而我需要只是在一个表格中的单元个中输出单纯的文字 最后在MSDN上找到 HtmlHelper.FormatValue 方法 public s ...
- An Attempt to Understand Boosting Algorithm(s)
An Attempt to Understand Boosting Algorithm(s) WELCOME! Here you will find daily news and tutorials ...