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 - ...
随机推荐
- Fast Paxos 和 Paxos的区别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt373 自从Lamport在1998年发表Paxos算法后,对Paxos的各种 ...
- iBatis的一个问题
写了一段查询语句,条件中有一条alarmtype<>'1004'这样的条件,原来是这样写的 <![CATA[ and alarmtype<>'1004']]> 然后 ...
- C# yield return用法
本文实例讲述了C#中yield return用法,并且对比了使用yield return与不使用yield return的情况,以便读者更好的进行理解.具体如下: yield关键字用于遍历循环中,yi ...
- 玩转PS路径,轻松画logo!
轻松画图标!教你一分钟玩转PS路径描边 推荐: cyRotel 2014/11/24 in PS 教程 & 设计文章 @Micu设计 :Photoshop的路径工具和钢笔工具 ...
- 结对作业(1)----基于GUI的四则运算
小伙伴:201421123031 余洋 201421123044 潘志坚 题目要求: 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是W ...
- 团队作业——Alpha冲刺之事后诸葛亮
小组成员: 武健男:201421123091 林俊鹏:201421123076 何跃斌:201421123082 陈鑫龙:201421123078 潘益靖:201421123086 黄睿:201421 ...
- 201521123060 《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1 ...
- Java 第五周总结
1. 本周学习总结 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 不能. ...
- 201521123093 java 第十三周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...
- 201521123017 《Java程序设计》第12周学习总结
1. 本周学习总结 2. 书面作业 Q1.字符流与文本文件:使用 PrintWriter(写),BufferedReader(读) 1.1 生成的三个学生对象,使用PrintWriter的printl ...