题目链接

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

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

  1. #include <bits/stdc++.h>
  2.  
  3. const int N = 5e2 + 5;
  4. const int INF = 0x3f3f3f3f;
  5. struct Min_Cost_Max_Flow {
  6. struct Edge {
  7. int from, to, cap, flow, cost;
  8. };
  9. std::vector<Edge> edges;
  10. std::vector<int> G[N];
  11. bool vis[N];
  12. int d[N], p[N], a[N];
  13. int n, m;
  14.  
  15. void init(int n) {
  16. this->n = n;
  17. for (int i=0; i<=n; ++i) {
  18. G[i].clear ();
  19. }
  20. edges.clear ();
  21. }
  22. void add_edge(int from, int to, int cap, int cost) {
  23. edges.push_back ((Edge) {from, to, cap, 0, cost});
  24. edges.push_back ((Edge) {to, from, 0, 0, -cost});
  25. m = edges.size ();
  26. G[from].push_back (m - 2);
  27. G[to].push_back (m - 1);
  28. }
  29. bool SPFA(int s, int t, int &flow, int &cost) {
  30. memset (d, INF, sizeof (d));
  31. memset (vis, false, sizeof (vis));
  32. memset (p, -1, sizeof (p));
  33. d[s] = 0; vis[s] = true; p[s] = 0; a[s] = INF;
  34.  
  35. std::queue<int> que; que.push (s);
  36. while (!que.empty ()) {
  37. int u = que.front (); que.pop ();
  38. vis[u] = false;
  39. for (int i=0; i<G[u].size (); ++i) {
  40. Edge &e = edges[G[u][i]];
  41. if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
  42. d[e.to] = d[u] + e.cost;
  43. p[e.to] = G[u][i];
  44. a[e.to] = std::min (a[u], e.cap - e.flow);
  45. if (!vis[e.to]) {
  46. vis[e.to] = true;
  47. que.push (e.to);
  48. }
  49. }
  50. }
  51. }
  52.  
  53. if (d[t] == INF) {
  54. return false;
  55. }
  56. flow += a[t];
  57. cost += d[t] * a[t];
  58. int u = t;
  59. while (u != s) {
  60. edges[p[u]].flow += a[t];
  61. edges[p[u]^1].flow -= a[t];
  62. u = edges[p[u]].from;
  63. }
  64. return true;
  65. }
  66. void run(int s, int t, int &flow, int &cost) {
  67. flow = cost = 0;
  68. while (SPFA (s, t, flow, cost));
  69.  
  70. printf ("%d %d\n", flow, 2 * flow - cost);
  71. for (int i=0; i<edges.size (); i+=2) {
  72. if (edges[i].from == s || edges[i].to == t || edges[i].flow == 0) {
  73. continue;
  74. }
  75. printf ("%d %d\n", edges[i].from, edges[i].to);
  76. }
  77. }
  78. };
  79. Min_Cost_Max_Flow mcmf;
  80. char group[N], sex[N];
  81. bool list[N];
  82. int n, m;
  83.  
  84. int main() {
  85. int T; scanf ("%d", &T);
  86. while (T--) {
  87. scanf ("%d", &n);
  88. scanf ("%s", group + 1);
  89. scanf ("%s", sex + 1);
  90.  
  91. mcmf.init (n + 1);
  92. int s = 0, t = n + 1;
  93. for (int i=1; i<=n; ++i) {
  94. if (group[i] == '0') {
  95. mcmf.add_edge (s, i, 1, 0);
  96. } else {
  97. mcmf.add_edge (i, t, 1, 0);
  98. }
  99. int m; scanf ("%d", &m);
  100. memset (list, false, sizeof (list));
  101. for (int j=1; j<=m; ++j) {
  102. int v; scanf ("%d", &v);
  103. list[v] = true;
  104. }
  105. if (group[i] == '1') {
  106. continue;
  107. }
  108. int cost = (sex[i] == '1');
  109. for (int j=1; j<=n; ++j) {
  110. if (list[j] || group[i] == group[j]) {
  111. continue;
  112. }
  113. mcmf.add_edge (i, j, 1, cost + (sex[j] == '1'));
  114. }
  115. }
  116. int flow, cost;
  117. mcmf.run (s, t, flow, cost);
  118. }
  119.  
  120. return 0;
  121. }

  

费用流 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. IOS- 单例

    单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例: ...

  2. Jquery获取select 控件的change事件时选中的值

    HTML代码如下: <div class="col-sm-9 col-xs-12"> <select id="groupid" class=& ...

  3. iOS中NSUserDefaults详解

    NSUserDefault 作为iOS中一种轻量级数据本地化方式,简单易用,经常用于存储一些应用相关属性记录,例如图书app的背景色,进度,上次阅读的书籍及相关配置信息.NSUserDefault实质 ...

  4. [Android Pro] 关于inputStream.available()方法获取文件的总大小

    reference to :http://hold-on.iteye.com/blog/1017449 如果用inputStream对象的available()方法获取流中可读取的数据大小,通常我们调 ...

  5. CLR via C#(18)——Enum

    1. Enum定义 枚举类型是经常用的一种“名称/值”的形式,例如: public enum FeedbackStatus     {         New,         Processing, ...

  6. HTML5学习之智能表单(二)

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

  7. 在PYTHON3中,使用Asyncio来管理Event loop

    #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time def functio ...

  8. oracle使用dbms_metadata包取得所有对象DDL语句

    当我们想要查看某个表或者是表空间的DDL的时候,可以利用dbms_metadata.get_ddl这个包来查看. dbms_metadata包中的get_ddl函数详细参数 GET_DDL函数返回创建 ...

  9. JqueryEasyUI 解决IE下datagrid无法刷新的问题 分类: JavaScript JqueryEasyUI 2014-09-20 10:05 510人阅读 评论(1) 收藏

    问题描述: 在使用JqueryEasyUI 时,发现在IE下$('#table').datagrid('reload');无效,数据并没有被刷新,究其原因,是因为刷新时,datagrid请求的url没 ...

  10. js判断当前的访问是手机/电脑

    <script type="text/javascript"> var commonURL = 'http://www.xxx.com/'; function mobi ...