网络流(费用流)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条街 ...
随机推荐
- .Net程序员学习Linux(一)
本次知识点:Linux系统的多终端切换,linux下的用户,linux远程访问工具使用,linux下重要的目录,命令的组成,通配符,linux的路径问题,文件操作的综合运用 为什么学习linux? 1 ...
- 《你不常用的c#之一》:略谈unsafe
转自csdn:http://blog.csdn.net/robingaoxb/article/details/6199508 msdn里讲到: “在 C# 中很少需要使用指针,但仍有一些需要使用的情况 ...
- Photon开发实战(2)——开发框架、第一个Photon程序
Photon基础开发框架 Photon (v4)的基本框架.开发框架主要Photon和游戏逻辑(C#)两个部分,如下图最新的Photon v4支持的4种底层协议,游戏开发逻辑Photon目前主要划分为 ...
- jquery学习之旅
在jQuery中,css()方法的功能是设置或获取元素的某项样式属性. $<"div">.css("font-weight","bold& ...
- window scipy install
http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy whl包,使用pip install xx.whl 安装 1:先安装 numpy+mkl. whl ...
- PinchEvent QML Type
PinchEvent类型在QtQuick 1.1中被添加进来.center, startCenter, previousCenter属性保存了两个触摸点之间的中心位置.scale and previo ...
- QML之TextEdit
TextEdit显示一个可编辑的,有格式的文本框.它也可以显示明文和富文本.例如:TextEdit { width: 240 text: "<b>Hello</ ...
- 通过Servlet的response绘制页面验证码
java部分 package com.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; ...
- 2016021904 - 如何使用Memory Analyzer
如何使用Memory Analyzer呢? 0.有内存溢出的代码code.<深入理解java虚拟机>中代码 package neutron.oom.heap; import java.ut ...
- 【算法】改进的冒泡排序 O(n^2) 稳定的 C语言
改进的冒泡排序 一.算法描述 基于原冒泡排序 每次选取第一个元素作为主元往后进行比较,若遇到比它小的则放到它左边(即进行交换),若遇到比它大的则选取大的作为主元进行后续比较,每趟选取了无序列中最大元素 ...