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之JMX 详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt194 一.JMX简介 JMX是一种JAVA的正式规范,它主要目的是让程序有被 ...
- mysql中int(10)与int(11)有什么区别吗?
先来看下面的图片 声明字段是int类型的那一刻起,int就是占四个字节,一个字节8位,也就是4*8=32,可以表示的数字个数是2的32次方(2^32 = 4 294 967 296个数字). 4 29 ...
- 201521123061 《Java程序设计》第九周学习总结
201521123061 <Java程序设计>第九周学习总结 1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1 ...
- 201521123002《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 1.提交函数实 ...
- 201521123080《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过? ...
- 201521123014 《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 Q1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...
- 201521123069 《Java程序设计》第1周学习总结
1. 本章学习总结 Java语言的发展.特点(面向对象.简单性.平台无关性) 认识并区分JVM(Java虚拟机),JRE(Java执行环境),JDK(Java开发工具包) 安装好JDK,设置Path变 ...
- 201521123032 《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- 多线程面试题系列(7):经典线程同步 互斥量Mutex
前面介绍了关键段CS.事件Event在经典线程同步问题中的使用.本篇介绍用互斥量Mutex来解决这个问题. 互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...
- hadoop运行wordcount实例,hdfs简单操作
1.查看hadoop版本 [hadoop@ltt1 sbin]$ hadoop version Hadoop -cdh5.12.0 Subversion http://github.com/cloud ...