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?

Input

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.

Output

3 3
1 5
3 7
5 10
4
8
9
2 2
5 10
10 20
4
21

Sample Output

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 贪心,最大流的更多相关文章

  1. ZOJ-2364 Data Transmission 分层图阻塞流 Dinic+贪心预流

    题意:给定一个分层图,即只能够在相邻层次之间流动,给定了各个顶点的层次.要求输出一个阻塞流. 分析:该题直接Dinic求最大流TLE了,网上说采用Isap也TLE,而最大流中的最高标号预流推进(HLP ...

  2. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  3. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

  4. 【P2774】方格取数问题(贪心+最大流,洛谷)

    首先,我们要读懂这道题,否则你会和我一开始产生一样的疑问,把所有的数都取走剩下一个最小的不就可以了么???然后我们发现样例完全不是这么回事.题目中所说的使相邻的两个数没有公共边,是指你去走的数,也就是 ...

  5. 洛谷P2765 魔术球问题(贪心 最大流)

    题意 已经很简洁了吧. 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...

  6. bzoj 1707: [Usaco2007 Nov]tanning分配防晒霜【贪心||最大流(?)】

    洛谷上能过的最大流bzoj上T了--但是贪心做法明明在洛谷上比最大流要慢啊--如果是最大流的话就是裸题了吧 说一下贪心,就按照防晒霜排序,然后对每一个防晒霜选一头可以使用的且r最小的牛 就,没了. 贪 ...

  7. luogu P5470 [NOI2019]序列 dp 贪心 费用流 模拟费用流

    LINK:序列 考虑前20分 容易想到爆搜. 考虑dp 容易设\(f_{i,j,k,l}\)表示前i个位置 选了j对 且此时A选择了k个 B选择了l个的最大值.期望得分28. code //#incl ...

  8. POJ2699:The Maximum Number of Strong Kings(枚举+贪心+最大流)

    The Maximum Number of Strong Kings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2488 ...

  9. 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 - ...

随机推荐

  1. Charles 抓包

    声明:本文为依依Love博主原创文章,未经博主允许不得转载   1. 简介: 2. 安装包下载: 3. 安装并替换破解版的jar包 4.设置mac代理 5.  安装证书: 6.  设置手机抓包     ...

  2. nginx小问题

    配置nginx与ftp图片服务器:安装后,要在/usr/local/nginx/conf/nginx.conf里面的server中(带有localhost的那一块)修改为location \ {roo ...

  3. 将home多余的空间分配到"/"分区下

    一.操作过程 1.df -h 查看分区大小情况; [root@localhost /]# df -h Filesystem            Size  Used Avail Use% Mount ...

  4. 团队作业10——复审和事后分析(Beta版本)

    团队作业10--事后分析(Beta版本) http://www.cnblogs.com/newteam6/p/6953992.html 团队作业10--复审(Beta版本) http://www.cn ...

  5. 201521123029《Java程序设计》第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...

  6. 201521123004《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 接口 接口(interface)就是方法声明和常量值的集合 实现接口的类叫接口的 ...

  7. 201521123013 《Java程序设计》第12周学习总结

    1. 本章学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示. Q ...

  8. logback:logback和slf4j中的:appender、logger、encoder、layout

    (1)appender 1.appender标签是logback配置文件中重要的组件之一.在logback配置文件中使用appender标签进行定义.可 以包含0个或多个appender标签. 2.a ...

  9. 如何使用IntelliJ IDEA的Favorites来管理项目中的常用代码

    http://www.cnblogs.com/deng-cc/p/6530279.html

  10. SQL Server安装【转载】

    http://blog.csdn.net/sangjinchao/article/details/62044021?locationNum=6&fps=1