Codeforces 730I:Olympiad in Programming and Sports(最小费用流)
http://codeforces.com/problemset/problem/730/I
题意:有n个人参加两种比赛,其中每个人有两个参加比赛的属性,如果参加了其中的一个比赛,那么不能参加另一个比赛,每种比赛有一个参加的限制人数,求让两种比赛的属性值最大的方案。
思路:如果往网络流方面想,就挺容易想到最小费用流的。
学习了一个技巧:把费用设为负,最后再反过来,就可以求最大费用流了。
将S和每个人相连,容量为1, 费用为0,再把每个人和T1点相连(代表第一个属性),容量为1,费用为-a[i],每个人和T2点相连(代表第二个属性),容量为1,费用为-b[i],再把T1和T相连,容量为p,T2和T相连,容量为s。
因为一些粗心调了N久。
#include <bits/stdc++.h>
using namespace std;
#define N 3010
#define INF 0x3f3f3f3f
struct Edge {
int u, v, cap, cost, nxt;
} edge[N*];
int head[N], a[N], b[N], tot, S, T, vis[N], dis[N], ans, pre[N]; void Add(int u, int v, int cap, int cost) {
edge[tot] = (Edge) {u, v, cap, cost, head[u]}; head[u] = tot++;
edge[tot] = (Edge) {v, u, , -cost, head[v]}; head[v] = tot++;
} bool SPFA() {
queue<int> que;
que.push(S);
memset(dis, INF, sizeof(dis));
memset(vis, , sizeof(vis));
int flow = INF;
dis[S] = ; vis[S] = ; pre[S] = -;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = ;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].cost;
if(edge[i].cap && dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pre[v] = i;
flow = min(flow, edge[i].cap);
if(!vis[v]) { vis[v] = ; que.push(v); }
}
}
}
if(dis[T] == INF) return false;
ans -= flow * dis[T];
int u = T;
while(u != S) {
edge[pre[u]].cap -= flow;
edge[pre[u]^].cap += flow;
u = edge[pre[u]].u;
}
return true;
} int main() {
int n, q, p;
scanf("%d%d%d", &n, &p, &q);
for(int i = ; i <= n; i++) scanf("%d", a + i);
for(int i = ; i <= n; i++) scanf("%d", b + i);
S = , T = n + ; int T1 = n + , T2 = n + , c1 = , c2 = ;
memset(head, -, sizeof(head)); tot = ;
for(int i = ; i <= n; i++) {
Add(S, i, , ); Add(i, T1, , -a[i]); Add(i, T2, , -b[i]);
}
Add(T1, T, p, ); Add(T2, T, q, );
ans = ;
while(SPFA()) ;
for(int u = ; u <= n; u++) {
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(edge[i].cap == ) {
if(v == T1) a[++c1] = u;
if(v == T2) b[++c2] = u;
}
}
}
printf("%d\n", ans);
for(int i = ; i <= c1; i++) printf("%d ", a[i]); puts("");
for(int i = ; i <= c2; i++) printf("%d ", b[i]); puts("");
return ;
}
Codeforces 730I:Olympiad in Programming and Sports(最小费用流)的更多相关文章
- Codeforces 937A - Olympiad
A. Olympiad 题目链接:http://codeforces.com/problemset/problem/937/A time limit per test 1 second memory ...
- Codeforces 730I [费用流]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...
- CodeForces 222D - Olympiad
第一行给出两个个数字k和n,第二三行分别有k个数字,求将第二.三行之间的数字相互组合,求最多有多少个组合的和不小于n 纯粹暴力 #include <iostream> #include & ...
- Codeforces 最大流 费用流
这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror) in codeforces(codeforces730)
A.Toda 2 思路:可以有二分来得到最后的数值,然后每次排序去掉最大的两个,或者3个(奇数时). /************************************************ ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest
A. Toda 2 按题意模拟即可. #include <bits/stdc++.h> using namespace std ; typedef pair < int , int ...
- 模拟费用流 & 可撤销贪心
1. CF730I Olympiad in Programming and Sports 大意: $n$个人, 第$i$个人编程能力$a_i$, 运动能力$b_i$, 要选出$p$个组成编程队, $s ...
- codeforces675D
Tree Construction CodeForces - 675D During the programming classes Vasya was assigned a difficult pr ...
- OUC_Summer Training_ DIV2_#16 725
今天做了这两道题真的好高兴啊!!我一直知道自己很渣,又贪玩不像别人那样用功,又没有别人有天赋.所以感觉在ACM也没有学到什么东西,没有多少进步.但是今天的B题告诉我,进步虽然不明显,但是只要坚持努力的 ...
随机推荐
- HDU 1027 以数列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- redis 从0到1 linux下的安装使用 数据类型 以及操作指令 一
安装 redis 到 /usr/目录下 我这里安装的是redis-3.2.9.tar.gz tar zxvf redis-3.2.9.tar.gz -C /usr 然后进行 执行编译命令 mak ...
- Android Camera2 拍照(二)——使用TextureView
原文:Android Camera2 拍照(二)--使用TextureView 上一篇博文简单介绍了使用Camera2 API拍摄照片,并使用SurfaceView作为预览界面.实际上,相对于Surf ...
- byte[] 左移和右移
public static class ex { public static byte[] RightShift(this byte[] ba, int n) { ) { return ba.Left ...
- Delphi I/O Errors(几百种不同的错误)
The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...
- SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)
原文:SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容(C ...
- Java HashMap实现原理 源码剖析
HashMap是基于哈希表的Map接口实现,提供了所有可选的映射操作,并允许使用null值和null建,不同步且不保证映射顺序.下面记录一下研究HashMap实现原理. HashMap内部存储 在Ha ...
- Tobject 类解析
TObject = class //创建 constructor Create; //释放 procedure Free; //初始化实列 class functi ...
- Android应用开机自启动问题
本文主要介绍Android应用如何实现开机自启动.自启动失败的原因以及通过ADB命令模拟发送BOOT_COMPLETED开机广播. 1.Android应用如何实现开机自启动 (1) 实现一个广播类,接 ...
- tftp的安装及配置
1.安装tftp服务客户端sudo apt-get install tftp 2.安装tftp服务器端sudo apt-get install tftpd 3.安装xinetd注意同类似的还有open ...