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 - ...
随机推荐
- [ASP.NET MVC]笔记(四 UnobtruSive AJAX和客户端验证
UnobtruSive AJAX和客户端验证 ASP.NET MVC 已经默认开启非侵入试js和客户端验证,在web.config可以看到如下配置: <configuration> < ...
- 《深入浅出MySQL》之SQL基础
SQL是Structure Query language(结构化查询语言)的缩写,它是使用关系模型的数据库应用语言.在众多开源数据中,MySQL正式其中最杰出的代表,MySQL是由三个瑞典人于20世纪 ...
- C# 引用类型之特例string
在C#编程的时候经常会使用字符串(string)类型,它也是引用类型,但是处处都不作为引用的用法来使用,实属特例,下来我一一罗列出来,供自己记忆方便: 1)字符串的直接赋值:本身字符串就是引用类型,应 ...
- 【Beta】阶段 第三次Daily Scrum Meeting
每日任务 ·1.本次会议为第三次 Meeting 会议 ·2.本次会议在周三上午9:40召开,会议时间为10分钟 一.今日站立式会议照片 二.每个人的工作(有work item的ID) 三.工作中遇到 ...
- 201521123073 《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1. finally 题目4-2 1.1 截图你的提交结果( ...
- 201521123051《Java程序设计》第十二周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 一 JAVA语言中主要通过流来完成IO操作. 流:计算机的输入输出之间流动的数据序列,也是类的对象.java中 ...
- Eclipse rap 富客户端开发总结(11) : rcp/rap与spring ibatis集成
1. rcp/rap 与 spring 集成 Activator 是rcp/rap 启动时需要加载的类, 只需要加载一遍,所以与spring 集成的时候一般是在这个类里面加载spring 的Appli ...
- 使用 Python & Flask 实现 RESTful Web API
环境安装: sudo pip install flask Flask 是一个Python的微服务的框架,基于Werkzeug, 一个 WSGI 类库. Flask 优点: Written in Pyt ...
- scrapy爬虫框架
downloader:负责下载html页面 spider:负责爬取页面内容,我们需要自己写爬取规则 srapy提供了selector,获取的方式有xpath,css,正则,extract item容 ...
- 关于Java中数组的常用操作方法
1. 声明一个数组 String[] arr1 = new String[5]; String[] arr2 = {"a","b","c", ...