1090: MTM (费用流)
1090: MTM
Total Submissions:127 Accepted:19
Description
MTM is not only a good ACMer but also a good teacher. There are n" role="presentation">n
students in MTM’s class. Every student has two skills, each measured as a number:ai" role="presentation">ai – the programming skill andbi" role="presentation">bi
– the math skill.
Both ACM competition and Math
competition will be held soon. So MTM decides to compose two teams to
take part in these competitions. Because of the limitation of the number
of student, MTM has to select p" role="presentation">p
students to take part in the ACM competition ands" role="presentation">s
students to take part in the Math competition. A student can't be a member of both teams.
MTM considers that his expected
result is equal to the sum of two values: the ACM team strength and the
Math team strength. The strength of each team is the sum of skills of
its members in the corresponding area.
Help MTM to compose two teams to maximize his expected result.
Input
The input test file will contain multiple test cases. The first line of each input contains three positive integer numbers n" role="presentation">n
, p" role="presentation">p and s" role="presentation">s (2 ≤ n ≤ 500" role="presentation">2≤n≤500, p + s ≤ n" role="presentation">p+s≤n) --- the number of students, the size of the ACM team and the size of the Math team.
The second line contains n" role="presentation">n positive integers a1, a2, …, an" role="presentation">a1,a2,…,an (1 ≤ ai ≤ 500" role="presentation">1≤ai≤500), where ai" role="presentation">ai is the programming skill of the i" role="presentation">i-th student.
The third line containsn" role="presentation">n positive integersb1, b2, …, bn" role="presentation">b1,b2,…,bn (1 ≤ bi ≤ 500" role="presentation">1≤bi≤500), wherebi" role="presentation">bi is the math skill of thei" role="presentation">i
-th student.
Output
In
the first line, print the maximum strength of MTM’s expected result. In
the second line, print p numbers — the members of the ACM team. In the
third line, print s numbers — the members of the Math team.
The students are numbered from 1" role="presentation">1
ton" role="presentation">n
as they are given in the input. All numbers printed in the second and in the third lines should be distinct and should be printed in ascending order.
Sample Input
5 2 2
1 3 4 5 2
5 3 2 1 4 4 2 2
10 8 8 3
10 7 9 4 5 3 1
5 2 5 1 7
6 3 1 6 3
Sample Output
18
3 4
1 5
31
1 2
3 4
23
1 3 5
4
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x7fffffff
#define mod 10000
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = +;
const int M = ;
set<int>ac,ma;
int n,p,s;
int acm[N],math[N];
struct Edge {
int from, to, cap, flow;
int cost;
};
inline int Min(int aa,int bb)
{
return aa<bb?aa:bb;
}
struct MCMF {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
int inq[N]; // 是否在队列中
int d[N]; // Bellman-Ford
int p[N]; // 上一条弧
int a[N]; // 可改进量
void init(int n) {
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void addedge(int from, int to, int cap, int cost) {
edges.push_back((Edge){from, to, cap, , cost});
edges.push_back((Edge){to, from, , , -cost});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BellmanFord(int s, int t, int& flow,int& cost) {
for(int i = ; i < n; i++) d[i] = inf;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = inf; queue<int> Q;
Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = ;
int l=G[u].size();
for(int i = ; i < l; i++) {
Edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = Min(a[u], e.cap - e.flow);
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
}
}
}
if(d[t] == inf) return false;
cost += d[t]*a[t];
int u = t;
while(u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
// 需要保证初始网络中没有负权圈
void Mincost(int s, int t) {
int cost = ;
int flow=;
while(BellmanFord(s, t,flow, cost));
printf("%d\n",-cost);
}
}g;
int main() {
int k;
while(~scanf("%d%d%d",&n,&p,&s) ) {
g.init(n+);
ac.clear();ma.clear();
for(int i=;i<=n;i++)scanf("%d",&acm[i]);
for(int i=;i<=n;i++)scanf("%d",&math[i]);
for(int i=;i<=n;i++){
g.addedge(,i,,);
g.addedge(i,n+,,-acm[i]);
g.addedge(i,n+,,-math[i]);
}
g.addedge(n+,n+,p,);g.addedge(n+,n+,s,);
int ans=;
g.Mincost(,n+); for(int i=;i<g.m;i++){
Edge temp=g.edges[i];
if(temp.to==n+&&temp.flow==) {
ac.insert(temp.from);
}
else if(temp.to==n+&&temp.flow==) {
ma.insert(temp.from);
}
}
bool f=false;
for(int x:ac){
if (f==false){
printf("%d",x);
f=true;
}
else
printf(" %d",x);
}
printf("\n");
f=false;
for(int x:ma){
if (f==false){
printf("%d",x);
f=true;
}
else
printf(" %d",x);
}
printf("\n");
}
return ;
}
1090: MTM (费用流)的更多相关文章
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 960 Solved: 5 ...
- 洛谷 1004 dp或最大费用流
思路: dp方法: 设dp[i][j][k][l]为两条没有交叉的路径分别走到(i,j)和(k,l)处最大价值. 则转移方程为 dp[i][j][k][l]=max(dp[i-1][j][k-1][l ...
- Codeforces 730I [费用流]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...
- zkw费用流+当前弧优化
zkw费用流+当前弧优化 var o,v:..] of boolean; f,s,d,dis:..] of longint; next,p,c,w:..] of longint; i,j,k,l,y, ...
- 【BZOJ-4213】贪吃蛇 有上下界的费用流
4213: 贪吃蛇 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 58 Solved: 24[Submit][Status][Discuss] Desc ...
- 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广
3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 174 Solved: 9 ...
- [bzoj4514]数字配对[费用流]
今年SDOI的题,看到他们在做,看到过了一百多个人,然后就被虐惨啦... 果然考试的时候还是打不了高端算法,调了...几天 默默地yy了一个费用流构图: 源连所有点,配对的点连啊,所有点连汇... 后 ...
随机推荐
- BJOI2018
BJOI2018 省选挂完,是时候更一篇题解了.对于鬼畜结论题我只放结论不给证明,不要打我-- day1 二进制 试题描述 pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不 ...
- 网络编程:listen函数
listen函数仅由TCP服务器调用,它做两件事: 当socket函数创建一个套接字时,它被假设为一个主动套接字,也就是说,它是一个将调用connect发起连接的客户套接字.listen函数把一个未连 ...
- C&C++——标准库
1.什么是C&C++的标准库? C语言被发明出来时并没有什么库函数,随着C语言的流行,越来越多的厂商或者机构组织开始提供C的编译器,并且同时把经常用到的函数封装成“库”的形式发布:不同的组织发 ...
- jocky1.0.3 (原joc) java混淆器 去除jdk版本限制
昨晚下班回去,研究了下jocky1.0.3的使用,发现编译时提示引用类库版本不对,捣弄了半个小时后终于理解,原来是我的jdk1.7版本过高,这货是06年的版本,到现在都没更新过,支持(限制)的最高版本 ...
- taotao用户登录(及登录成功后的回调url处理)
后台Controller: package com.taotao.sso.controller; import org.springframework.stereotype.Controller; i ...
- 7月17号day9总结
今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest { Configuration configuration; FileSystem ...
- Document base D:\devTools\apache-tomcat-6.0.51\webapps\AppService does not exist or is not a readable directory
tomcat通过eclipse发布项目到webapp后 手动删除在webapp目录下的文件,启动tomcat时,会报出异常找不到那个删除的项目. 解决方法是(1)重新发布项目到webapp (2)在 ...
- javascript中top、clientTop、scrollTop、offsetTop的讲解
下面结合各上图介绍一下各个属性的作用: 一.offsetTop属性: 此属性可以获取元素的上外缘距离最近采用定位父元素内壁的距离,如果父元素中没有采用定位的,则是获取上外边缘距离文档内壁的距离.所谓的 ...
- spring aop与aspectj
AOP:面向切面编程 简介 AOP解决的问题:将核心业务代码与外围业务(日志记录.权限校验.异常处理.事务控制)代码分离出来,提高模块化,降低代码耦合度,使职责更单一. AOP应用场景: 日志记录.权 ...
- [05] call by sharing || 共享参数
转: https://segmentfault.com/a/1190000005177386 众所周知,JavaScript中参数是按值传递的.与访问变量不同,基本类型和引用类型的参数在传递时都如同变 ...