ZOJ3508 The War 贪心,最大流
A war had broken out because a sheep from your kingdom ate some grasses which belong to your neighboring kingdom. The counselor of your kingdom had to get prepared for this war. There are N (1 <= N <= 2500) unarmed soldier in your kingdom and there are M (1 <= M <= 40000) weapons in your arsenal. Each weapon has a weight W (1 <= W <= 1000), and for soldier i, he can only arm the weapon whose weight is between minWi and maxWi ( 1 <= minWi <= maxWi <= 1000). More armed soldier means higher success rate of this war, so the counselor wants to know the maximal armed soldier he can get, can you help him to win this war?
There multiple test cases. The first line of each case are two integers N, M. Then the following N lines, each line contain two integers minWi, maxWi for each soldier. Next M lines, each line contain one integer W represents the weight of each weapon.
3 3
1 5
3 7
5 10
4
8
9
2 2
5 10
10 20
4
21
2
0
常规解法是贪心,但是在复习最大流的写法,因此用sap来写的。思路是很好想的
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<memory.h>
#include<cmath>
using namespace std;
const int M=;
int s,t,cnt;;
int a[],b[],num[];;
const int inf=;
int vd[],dis[],ans,m,n;
int Laxt[M],Next[M],Val[M],To[M];
void _update()
{
memset(vd,,sizeof(vd));
memset(dis,,sizeof(dis));
memset(num,,sizeof(num));
memset(Laxt,,sizeof(Laxt));
ans=; cnt=;
s=; t=+n+;
}
void _add(int u,int v,int w)
{
Next[cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Val[cnt++]=w; Next[cnt]=Laxt[v];
Laxt[v]=cnt;
To[cnt]=u;
Val[cnt++]=;
}
int dfs(int u,int flow)
{
int temp,delta;
if(u==t)return flow;
delta=;
for(int i=Laxt[u];i>;i=Next[i])
if(Val[i]> && dis[u]==dis[To[i]]+)
{
temp=dfs(To[i],min(flow-delta,Val[i]));
Val[i]-=temp;
Val[i^]+=temp; delta+=temp;
if(delta==flow||dis[s]>t) return delta;
}
vd[dis[u]]--;
if(vd[dis[u]]==)dis[s]=t+;
dis[u]++;
vd[dis[u]]++;
return delta;
}
void _work()
{
int i,j,x,y;
for(i=;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
for(i=;i<=m;i++) {
scanf("%d",&x);
num[x]++;
}
for(i=;i<=;i++)
if(num[i]>)
_add(s,i,num[i]);
for(i=;i<=;i++)
for(j=;j<=n;j++){
if(i>=a[j]&&i<=b[j])
_add(i,+j,);
}
for(j=;j<=n;j++)
_add(+j,t,);
int temp=;
while(dis[s]<t+)
{
int flow=dfs(s,inf);
ans+=flow;
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
_update();
_work();
}
return ;
}
ZOJ3508 The War 贪心,最大流的更多相关文章
- ZOJ-2364 Data Transmission 分层图阻塞流 Dinic+贪心预流
题意:给定一个分层图,即只能够在相邻层次之间流动,给定了各个顶点的层次.要求输出一个阻塞流. 分析:该题直接Dinic求最大流TLE了,网上说采用Isap也TLE,而最大流中的最高标号预流推进(HLP ...
- UVa 11729 - Commando War(贪心)
"Waiting for orders we held in the wood, word from the front never came By evening the sound of ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- 【P2774】方格取数问题(贪心+最大流,洛谷)
首先,我们要读懂这道题,否则你会和我一开始产生一样的疑问,把所有的数都取走剩下一个最小的不就可以了么???然后我们发现样例完全不是这么回事.题目中所说的使相邻的两个数没有公共边,是指你去走的数,也就是 ...
- 洛谷P2765 魔术球问题(贪心 最大流)
题意 已经很简洁了吧. 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...
- bzoj 1707: [Usaco2007 Nov]tanning分配防晒霜【贪心||最大流(?)】
洛谷上能过的最大流bzoj上T了--但是贪心做法明明在洛谷上比最大流要慢啊--如果是最大流的话就是裸题了吧 说一下贪心,就按照防晒霜排序,然后对每一个防晒霜选一头可以使用的且r最小的牛 就,没了. 贪 ...
- luogu P5470 [NOI2019]序列 dp 贪心 费用流 模拟费用流
LINK:序列 考虑前20分 容易想到爆搜. 考虑dp 容易设\(f_{i,j,k,l}\)表示前i个位置 选了j对 且此时A选择了k个 B选择了l个的最大值.期望得分28. code //#incl ...
- POJ2699:The Maximum Number of Strong Kings(枚举+贪心+最大流)
The Maximum Number of Strong Kings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2488 ...
- CodeForces - 884F :Anti-Palindromize(贪心&费用流)
A string a of length m is called antipalindromic iff m is even, and for each i (1 ≤ i ≤ m) ai ≠ am - ...
随机推荐
- java开发3轮技术面+hr面 面经(MT)
一直没打理博客园 发现博客园阅读量好大,就把前段时间写的一个面经也搬过来咯,大家一起加油.... 作者:小仇Eleven 链接:https://www.nowcoder.com/discuss/37 ...
- NHibernate教程(14)--使用视图
本节内容 引入 1.持久化类 2.映射文件 3.测试 结语 引入 在数据库操作中,我们除了对表操作,还有视图.存储过程等操作,这一篇和下篇来学习这些内容.这篇我们来学习如何在NHibernate中使用 ...
- 软件工程(GZSD2015)第二次作业小结
第二次作业,从4月7号开始,陆续开始提交作业.根据同学们提交的作业报告,相比第一次作业,已经有了巨大改变,大家开始有了完整的实践,对那些抽象的名词也开始有了直观的感受,这很好.然后有一些普遍存在的问题 ...
- GUI(GroupLayout 分组布局)
组:一些组件的集合 连续组:一个接着一个摆放 并行组:一个组在另一个组的顶部 ...
- 201521123088《Java程序设计》第七周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析1.1 解释ArrayList的contains源代码源代码: //contain ...
- 201521123057 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过? ...
- python学习笔记1.1
- 201521123016《Java程序设计》第10周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? 只有执 ...
- 201521123031 《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 1.将Student对象(属性:int id, String name,int age,dou ...
- 多线程:head first Thread.join()
不使用Thread.join() 测试线程 先上代码: /** * Created by Zero on 2017/8/23. */ public class TestJoin implements ...