网络流(费用流)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条街 ...
随机推荐
- Java 文件下载
public HttpServletResponse download(String path, HttpServletResponse response) { try { // path是指欲下载的 ...
- .NET3.5中的高性能 Socket API
转载:http://www.cnblogs.com/TianFang/archive/2007/11/09/954730.html 在 .NET Framework 2.0 版本中,System.Ne ...
- 微信热修复tinker及tinker server快速接入
博客: 安卓之家 掘金: jp1017 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 当前热修复方案很多,今天研究了下微信的tinker,使用效果还是不错的,配合tinker serv ...
- asp IIS部署An error occurred on the server when processing the URL错误提示解决
An error occurred on the server when processing the URL. Please contact the system administrator.If ...
- MvvmCross[翻译] 使用Xamarin与MvvmCross完成一个跨平台App
总览 原文:https://github.com/MvvmCross/MvvmCross/wiki/Tip-Calc-A-first-app 我们所做的第一个Model-View-ViewModel( ...
- LVS单机测试不负载
LVS单机测试不负载 1.困惑 当我们在个人PC上搭建虚拟机(Vmware)做LVS负载实验的时候,我们不论是在个人浏览器或者其他虚拟机上访问LVS的VIP都会出现上时间刷新都出现同一个页面的情况. ...
- C#中class的访问级别
中午吃饭前,同事问了一个问题:class 前面不加public访问修饰符时的默认访问级别是什么? 当时脑海自然而然的闪过了private 级别,但是细想感觉不对,class 是在namespace之下 ...
- Git版本控制工具使用:Error pulling origin: error: Your local changes to the following files would be overwritten by merge
摘自: CSDN 逆觞 git在pull时,出现这种错误的时候,可能很多人进进行stash,相关stash的请看:Error pulling origin: error: Your local cha ...
- 帝国cms7.0设置标题图片(缺失状态下)
有时候因为我们没有设置标题图片,程序就会是使用自己的标题图片,这就是问题所在,现在有2个办法解决这个问题, [1]直接替换调程序的标签图片,但是这样的方法虽然简单,但是图片大小固定,要是每个模版的图片 ...
- mysql的字段类型范围必须重视起来
在MySQL数据类型中,例如INT,FLOAT,DOUBLE,CHAR,DECIMAL等,它们都有各自的作用,下面我们就主要来介绍一下MySQL数据类型中的DECIMAL类型的作用和用法. 一般赋予浮 ...