题目链接

题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量

分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普通的最小费用最大流也能过,只是相对来说慢了点

#include <bits/stdc++.h>

const int N = 5e2 + 5;
const int INF = 0x3f3f3f3f;
struct Min_Cost_Max_Flow {
struct Edge {
int from, to, cap, flow, cost;
};
std::vector<Edge> edges;
std::vector<int> G[N];
bool vis[N];
int d[N], p[N], a[N];
int n, m; void init(int n) {
this->n = n;
for (int i=0; i<=n; ++i) {
G[i].clear ();
}
edges.clear ();
}
void add_edge(int from, int to, int cap, int cost) {
edges.push_back ((Edge) {from, to, cap, 0, cost});
edges.push_back ((Edge) {to, from, 0, 0, -cost});
m = edges.size ();
G[from].push_back (m - 2);
G[to].push_back (m - 1);
}
bool SPFA(int s, int t, int &flow, int &cost) {
memset (d, INF, sizeof (d));
memset (vis, false, sizeof (vis));
memset (p, -1, sizeof (p));
d[s] = 0; vis[s] = true; p[s] = 0; a[s] = INF; std::queue<int> que; que.push (s);
while (!que.empty ()) {
int u = que.front (); que.pop ();
vis[u] = false;
for (int i=0; i<G[u].size (); ++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] = std::min (a[u], e.cap - e.flow);
if (!vis[e.to]) {
vis[e.to] = true;
que.push (e.to);
}
}
}
} if (d[t] == INF) {
return false;
}
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
void run(int s, int t, int &flow, int &cost) {
flow = cost = 0;
while (SPFA (s, t, flow, cost)); printf ("%d %d\n", flow, 2 * flow - cost);
for (int i=0; i<edges.size (); i+=2) {
if (edges[i].from == s || edges[i].to == t || edges[i].flow == 0) {
continue;
}
printf ("%d %d\n", edges[i].from, edges[i].to);
}
}
};
Min_Cost_Max_Flow mcmf;
char group[N], sex[N];
bool list[N];
int n, m; int main() {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
scanf ("%s", group + 1);
scanf ("%s", sex + 1); mcmf.init (n + 1);
int s = 0, t = n + 1;
for (int i=1; i<=n; ++i) {
if (group[i] == '0') {
mcmf.add_edge (s, i, 1, 0);
} else {
mcmf.add_edge (i, t, 1, 0);
}
int m; scanf ("%d", &m);
memset (list, false, sizeof (list));
for (int j=1; j<=m; ++j) {
int v; scanf ("%d", &v);
list[v] = true;
}
if (group[i] == '1') {
continue;
}
int cost = (sex[i] == '1');
for (int j=1; j<=n; ++j) {
if (list[j] || group[i] == group[j]) {
continue;
}
mcmf.add_edge (i, j, 1, cost + (sex[j] == '1'));
}
}
int flow, cost;
mcmf.run (s, t, flow, cost);
} return 0;
}

  

费用流 ZOJ 3933 Team Formation的更多相关文章

  1. ZOJ 3933 Team Formation

    费用流裸题......比赛的时候少写了一句话....导致增加了很多无用的边一直在TLE #include<cstdio> #include<cstring> #include& ...

  2. 位运算 ZOJ 3870 Team Formation

    题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...

  3. Zoj 3870——Team Formation——————【技巧,规律】

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  4. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

  5. ZOJ 3870 Team Formation 位运算 位异或用与运算做的

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...

  6. ZOJ - 3870 Team Formation(异或)

    题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  8. ZOJ 3870:Team Formation(位运算&思维)

    Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

  9. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

随机推荐

  1. 存储过程使用CTE 和 case when

    未用SQL CTE and case when: ALTER PROCEDURE [dbo].[usp_rptDropboxBatchSummary1] )='ALL', )='ALL', )='AL ...

  2. python基础——枚举类

    python基础——枚举类 当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份: JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12 好处是简单 ...

  3. 瞧一瞧迷一般的SQLDA

    With static SQL, host variables used in embedded SQL statements are known at application compile tim ...

  4. 与你相遇好幸运,Waterline的多表关联

    >一对一关联 表示一个模型可能只与另一个模型关联.为了使模型知道它与其他哪些模型关联,外键必需包含在记录中.. http://imfly.github.io/sails-docs/concept ...

  5. C# 设计模式 提高可维护性的几点原则

    为了提高软件的可维护性,应该遵循以下几点原则: 1.“开放--封闭”原则(OCP) 一个软件实体应该对扩展开放,对修改封闭. 2.里氏代换原则(LSP) 子类型必须能替换她们的基类型,反过来则不成立. ...

  6. Arch Linux Installation Guide

    Arch Linux Installation Guide   timedatectl set-ntp true   sed -i '/Score/{/China/!{n;s/^/#/}}' /etc ...

  7. OCJP(1Z0-851) 模拟题分析(一)11

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  8. 构造方法 static 块 {}块 执行顺序

    package com.test.innerclass; public class HelloB extends HelloA { public HelloB() { System.out.print ...

  9. 判断一个类到底是从哪个jar包中调用的工具类

    项目中使用的jar包较多时,会出现jar冲突的情况,有时候很难判断当前使用的这个类是从哪个jar包中调用的.因为一般我们只能看到jar包的名称,不清楚其中的类的目录结构. 这个类的作用就是说明当前调用 ...

  10. Linux光纖卡配置,磁盤掛載,多路徑設置

    Linux光纖卡配置 1.首先根據光纖卡類型加載對應的驅動.我這裡常用的是QLogic和Brocade光纖卡 [root@rhcsasm2 host3]# lspci | grep Fibre   - ...