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了一个费用流构图: 源连所有点,配对的点连啊,所有点连汇... 后 ...
随机推荐
- poj3347 Kadj Squares (计算几何)
D - Kadj Squares Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- 如何获取iframe DOM的值
在Web开发时,很多时候会遇到一个问题.我在一个页面嵌入了iframe,并且我想获得这个iframe页面某个元素的值.那么该如何实现这个需求呢? 先来看下演示: 效果演示 iframe1中文本框的值: ...
- git使用笔记(六)github
By francis_hao Nov 20,2016 github介绍 github是一个网站https://github.com/,可以实现基于git(当然,svn也是可以的)的代码托管工作. ...
- 解析 Array.prototype.slice.call(arguments,0)
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- 前端面试:什么是css reset
HTML标签在浏览器中都有默认的样式,不同的浏览器的默认样式之间存在差别.例如ul默认带有缩进样式,在IE下,它的缩进是由margin实现的,而在Firefox下却是由padding实现的.开发时浏览 ...
- bzoj1833: [ZJOI2010]count 数字计数 && codevs1359 数字计数
bzoj1833 codevs1359 这道题也是道数位dp 因为0有前导0这一说卡了很久 最后发现用所有位数减1~9的位数就okay.....orzczl大爷 其他就跟51nod那道统计1出现次数一 ...
- jqueryDateTable.js排序
{% block js %} <script type="text/javascript"> $('#datatable').dataTable( { "or ...
- python3 内置函数(转)
http://www.runoob.com/python/python-built-in-functions.html divmod(7,2) # 返回(3,1)商和余的元组 frozenset() ...
- centOS 7 部署samba
部署samba **每个用户有自己的目录,可以浏览内容,也可以删除** 清空防火墙规则 [root@bogon ~]# iptables -F 安装samba [root@bogon ~]# yum ...
- Invalidation queue with "bit-sliceability"
BACKGROUND, FEATURES In a computer system having more than one memory storage facility, a special da ...