网络流(费用流)CodeForces 321B:Ciel and Duel
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
- Choose one of her cards X. This card mustn't be chosen before.
- If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
- If Y's position is Attack, then (X's strength) ≥ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
- If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Sample Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
3000
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
992
2 4
DEF 0
ATK 0
0
0
1
1
1
Hint
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
这道题提示我们可以用INF强化优先级,之后减回来就好。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,M=,INF=;
int n,m,cnt,fir[N],nxt[M],to[M],cap[M],val[M];
int vis[N],dis[N],path[N],q[N],front,back;
void addedge(int a,int b,int c,int v){
nxt[++cnt]=fir[a];to[fir[a]=cnt]=b;cap[cnt]=c;val[cnt]=v;
nxt[++cnt]=fir[b];to[fir[b]=cnt]=a;cap[cnt]=;val[cnt]=-v;
//printf("%d %d %d %d\n",a,b,c,v);
} int BFS(int S,int T){
for(int i=S+;i<=T;i++)dis[i]=*INF;
q[front=back=]=S;vis[S]=true;
while(front<=back){
int x=q[front++];vis[x]=false;
for(int i=fir[x];i;i=nxt[i])
if(cap[i]&&dis[to[i]]>dis[x]+val[i]){
dis[to[i]]=dis[x]+val[i];
if(!vis[to[i]])q[++back]=to[i];
vis[to[i]]=true;path[to[i]]=i;
}
}
return dis[T];
} void Aug(int S,int T){
int p=T;
while(p!=S){
cap[path[p]]-=;
cap[path[p]^]+=;
p=to[path[p]^];
}
} int McMf(int S,int T){
int ret=,d,tmp=;
for(int i=;i<=n;i++){
d=BFS(S,T);
if(d>=INF)return -ret;
tmp+=d;ret=min(ret,tmp);Aug(S,T);
}
for(int i=n+;i<=m;i++){
d=BFS(S,T);
if(d==*INF)return -ret;
tmp+=d-*INF;ret=min(ret,tmp);Aug(S,T);
}
return -ret;
} int A[N],D[N],B[N],ca,cd,S,T,x;
void Init(){
memset(dis,,sizeof(dis));
memset(fir,,sizeof(fir));
front=back=cnt=;
}
char op[];
int main(){
Init();
scanf("%d%d",&n,&m);
S=;T=n+m+;
for(int i=;i<=n;i++){
scanf("%s%d",op,&x);
if(op[]=='A')A[++ca]=x;
if(op[]=='D')D[++cd]=x;
}
for(int i=;i<=n;i++)
addedge(i+m,T,,);
for(int i=;i<=m;i++){
scanf("%d",&B[i]);
addedge(S,i,,);
addedge(i,T,,*INF-B[i]);
for(int j=;j<=ca;j++)
if(A[j]<=B[i])addedge(i,m+j,,-B[i]+A[j]);
for(int j=;j<=cd;j++)
if(D[j]<B[i])addedge(i,m+j+ca,,);
}
printf("%d\n",McMf(S,T));
return ;
}
网络流(费用流)CodeForces 321B:Ciel and Duel的更多相关文章
- CF 321B Ciel and Duel(费用流)
题目链接:http://codeforces.com/problemset/problem/321/B 题意:两个人,分别有n.m张牌.每张牌有两个属性类型和能力,类型为攻击或者防守.B的m张牌的属性 ...
- BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流
https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...
- 【上下界网络流 费用流】bzoj2055: 80人环游世界
EK费用流居然写错了…… Description 想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么 一个80人的团 ...
- 图论--网络流--费用流POJ 2195 Going Home
Description On a grid map there are n little men and n houses. In each unit time, every little man c ...
- 图论--网络流--费用流--POJ 2156 Minimum Cost
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- HDU 5352——MZL's City——————【二分图多重匹配、拆点||网络流||费用流】
MZL's City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 线性规划||网络流(费用流):COGS 288. [NOI2008] 志愿者招募
[NOI2008] 志愿者招募 输入文件:employee.in 输出文件:employee.out 简单对比 时间限制:2 s 内存限制:512 MB [问题描述] 申奥成功后,布布经过 ...
- POJ训练计划3422_Kaka's Matrix Travels(网络流/费用流)
解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次仅仅能向右和向下走,走到一个格子上加上格子的数,能够走k次.问最大的和是多少. 思路: 建图:每一个格子掰成两个点,分别叫" ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...
- 【bzoj1877】[SDOI2009]晨跑 费用流
题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街 ...
随机推荐
- POJ-3278(BFS)
题目: ...
- mysql锁表和解锁语句分享
对于MySQL来说,有三种锁的级别:页级.表级.行级 页级的典型代表引擎为BDB. 表级的典型代表引擎为MyISAM,MEMORY以及很久以前的ISAM. 行级的典型代表引擎为INNODB. ...
- 用bootstrap的tab插件做一个图层切换效果(感觉会误导淫们,大家当乐子看吧)
小伙伴们啊,我JS真的是个渣渣,所以总想要偷懒,于是为了实现效果就把tab插件给改了(各位大神轻拍啊,我是小白,很纯洁滴,小心脏也很脆弱)…… 最近做的项目为了考虑以后的移动设备兼容性,所以用了Boo ...
- 记录Access数据库更新操作大坑一个
对于更新Access数据库的操作,必须保持参数数组与sql语句中参数顺序一致,如下: public bool Update(MyModel model) { StringBuilder strSql ...
- Java:Date、Calendar、Timestamp的使用
一.Java.util.Date 该对象包含了年月日时分秒信息.具体使用如下代码: //String 转换为Date private static void dateDemo() throws Par ...
- MySQL常见问题汇总(原创)
本文记录了使用Mysql时遇到的问题,持续更新中... 1.在windows命令行下登录mysql时报错: C:\Program Files\MySQL\MySQL Server 5.0\bin> ...
- nullptr和NULL 区别
注:本文内容摘自网络,准确性有待验证,现阶段仅供学习参考.尊重作品作者成果,原文链接 :http://www.2cto.com/kf/201302/190008.html 1.为什要有nullptr ...
- C++线程类的封装
简单的C++线程操作的封装,使用了智能指针管理对象的释放. 可运行对象基类 class SimpleRunable:public RefCountedBase { public: SimpleRuna ...
- Android 安全概述
1. 保密: 信息.文档加密.解密 2. 鉴别.认证:就像确认与我们打电话的确实是XXX,而不是骗子 3. 完整性:信息不能被篡改, 4. 不可否认性:确定信息有who产生,并且将来不能否认
- JSON字符串序列化与反序列化浅试
一.添加引用(using Newtonsoft.Json.Linq;) 二. 1.生成json字符串源码 List<string> list = new List<string> ...