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. WPF中的Application类。

    原文:WPF中的Application类. Application对象用的名称空间是system.windows 1.手动创建Application对象步骤. 1.1).把项目中的App.Xaml文件 ...

  2. WPF中样式和行为和触发器

    原文:WPF中样式和行为和触发器 样式简介:样式(style)是组织和重用格式化选项的重要工具,不是使用重复的标记填充XAML,以便设置外边距.内边距.颜色以及字体等细节.而是创建一系列封装所有这些细 ...

  3. Angular基本概念理解

    一些符号的概念 #nzTable 模块变量 [] 输入(绑定值) () 输出(绑定事件) 补充说明: []是控件监控外部变化 ()是监听事件,交给外部变化内部值的权利 二者都是"监听&quo ...

  4. WPF控件的一些特殊应用

    1 checkbox.IsChecked 返回的是bool?类型,需要用bool强转,或者直接和bool类型比较,将发生隐形转换 2 RadioButton有分组属性GroupName

  5. WPF 验证错误模板

    <Window x:Class="BindingExam.MainWindow"        xmlns="http://schemas.microsoft.co ...

  6. 【C#】WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的

    原文:[C#]WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的 初学WPF,知道一些控件可以通过定义Style的Trigger改变要显示的样式,但是 ...

  7. centos7安装 lamp

    1.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd.service #启动apache systemctl sto ...

  8. UWP中String类型如何转换为Windows.UI.Color

    原文:UWP中String类型如何转换为Windows.UI.Color 我在学习过程中遇到的,我保存主题色为string,但在我想让StatusBar随着主题色变化时发现没法使用. ThemeCol ...

  9. Visual Studio查找中文的正则表达式

    原文: Visual Studio查找中文的正则表达式 经常有这样的需求:项目代码中有一些输出信息是中文写的,不过现在要做国际化,代码""中写的中文都要改成英文.这样就需要将代码中 ...

  10. linq中不能准确按拼音排序

    在LinqToObject中,利用OrderBy/OrderByDescending, ThenBy/ThenByDescending这4个方法排序时,发现不能正确的按拼音排序,所以在排序时增加编码支 ...