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(最小费用流)的更多相关文章

  1. Codeforces 937A - Olympiad

    A. Olympiad 题目链接:http://codeforces.com/problemset/problem/937/A time limit per test 1 second memory ...

  2. Codeforces 730I [费用流]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...

  3. CodeForces 222D - Olympiad

    第一行给出两个个数字k和n,第二三行分别有k个数字,求将第二.三行之间的数字相互组合,求最多有多少个组合的和不小于n 纯粹暴力 #include <iostream> #include & ...

  4. Codeforces 最大流 费用流

    这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...

  5. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror) in codeforces(codeforces730)

    A.Toda 2 思路:可以有二分来得到最后的数值,然后每次排序去掉最大的两个,或者3个(奇数时). /************************************************ ...

  6. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest

    A. Toda 2 按题意模拟即可. #include <bits/stdc++.h> using namespace std ; typedef pair < int , int ...

  7. 模拟费用流 & 可撤销贪心

    1. CF730I Olympiad in Programming and Sports 大意: $n$个人, 第$i$个人编程能力$a_i$, 运动能力$b_i$, 要选出$p$个组成编程队, $s ...

  8. codeforces675D

    Tree Construction CodeForces - 675D During the programming classes Vasya was assigned a difficult pr ...

  9. OUC_Summer Training_ DIV2_#16 725

    今天做了这两道题真的好高兴啊!!我一直知道自己很渣,又贪玩不像别人那样用功,又没有别人有天赋.所以感觉在ACM也没有学到什么东西,没有多少进步.但是今天的B题告诉我,进步虽然不明显,但是只要坚持努力的 ...

随机推荐

  1. Android blueZ HCI(一个):hciconfig实施和经常使用

    关键词:hciconfighcitool  hcidump笔者:xubin341719(欢迎转载,请明确说明,请尊重版权,谢谢.)欢迎指正错误,共同学习.共同进步! . Android blueZ H ...

  2. boost库交叉编译(Linux生成ARM的库)

    1. 环境: Linux系统:Ubuntu 14.04 编译工具:arm-fsl-linux-gnueabi-gcc 2.下载boost源码: 地址:https://sourceforge.net/p ...

  3. WPF VisualTreeHelper的使用

    <Window x:Class="MyWpf.MainWindow"        xmlns="http://schemas.microsoft.com/winf ...

  4. jquery 显示和隐藏的三种方式

     <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    & ...

  5. window 10 64bit Tortoise SVN 图标状态显示不正常

    https://www.cnblogs.com/lzpong/p/6187366.html 根据以上帖子处理 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows ...

  6. Win8Metro(C#)数字图像处理--2.8图像线性变换

    原文:Win8Metro(C#)数字图像处理--2.8图像线性变换  2.8图像线性变换 [函数名称] 图像线性变换函数LinearTransformProcess(WriteableBitmap ...

  7. asp.net文件流下载的代码摘要

    try { var workbook = new XLWorkbook(); if (Workbook != null) { workbook = Workbook; } if (this.Expor ...

  8. VCL的通用属性,方法和事件 good

    Action 一些组件标识与组件相连接的活动对象Align 一些组件确定组件在父类组件区内的对齐方式Anchors 大部分组件说明与组件连接的窗体的位置点AutoSize 一些组件说明组件是否可根据内 ...

  9. 12 寸 Retina MacBook 的大秘密: 可用移动电源充电

    苹果新款12寸Retina MacBook虽然只有一个USB-C接口,但这个接口的能力却十分强大.它不仅可以进行数据传输和视频输出,还能接收和输入电源.这也就是说,你可以使用移动电源对其进行充电,如果 ...

  10. Delphi中动态调用TXMLDocument的经历

    var  vXMLDocument: TXMLDocument;begin  vXMLDocument := TXMLDocument.Create('c:/temp/temp.xml');  Cap ...