A - ACM Computer Factory

POJ - 3436

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.

Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.

Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.

Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.

Output specification describes the result of the operation, and is a set of Pnumbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.

The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.

After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.

As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

Input

Input file contains integers P N, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2...Si,PDi,1 Di,2...Di,P, where Qi specifies performance, Si,j — input specification for part jDi,k — output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ ≤ 50, 1 ≤ Qi ≤ 10000

Output

Output the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.

If several solutions exist, output any of them.

Sample Input

Sample input 1
3 4
15 0 0 0 0 1 0
10 0 0 0 0 1 1
30 0 1 2 1 1 1
3 0 2 1 1 1 1
Sample input 2
3 5
5 0 0 0 0 1 0
100 0 1 0 1 0 1
3 0 1 0 1 1 0
1 1 0 1 1 1 0
300 1 1 2 1 1 1
Sample input 3
2 2
100 0 0 1 0
200 0 1 1 1

Sample Output

Sample output 1
25 2
1 3 15
2 3 10
Sample output 2
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1
Sample output 3
0 0

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.
 
 
 
首先这个题目是一个最小割+dinic+拆点
这个题目要注意一点就是要建双向边,这个题目和G - Island Transport 这个题目很像,都是双向边,
但是有一点不一样,那个题目两个点之间只需要建立两条边,这个要建立三条边,这个这个拆点了。
除此之外就是一个裸的最小割了。
 
 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + ;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init(int n)
{
for (int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m)!=EOF)//n是城市的数量,m是高速公路的数量
{
init(*n+);
int s, t, x, y;
scanf("%d%d", &s, &t);
for(int i=;i<=n;i++)
{
scanf("%d", &x);
add(i, i + n, x);
}
for(int i=;i<=m;i++)
{
scanf("%d%d", &x, &y);
add(x + n, y, inf);
add(y+n, x, inf);
}
int ans = Maxflow(s, t+n);
printf("%d\n", ans);
}
return ;
}
 
 
 
 
 

最新文章

  1. 在SQL Server里我们为什么需要意向锁(Intent Locks)?
  2. RDVTabBarController的基本使用 以及tabbar的防止双点击方法
  3. JSON 数据格式
  4. github展示项目
  5. NHibernate系列文章十:NHibernate对象二级缓存下
  6. Java--&gt;把txt中的所有字符按照码表值排序
  7. 使用jQuery动态加载js脚本文件的方法
  8. 经典递归算法研究:hanoi塔的理解与实现
  9. 1206: B.求和
  10. 随心所欲~我也做个集合遍历器吧(自己的foreach,委托的威力)
  11. 初次搭建vue环境(最基础的)
  12. java基础学习系列三
  13. 【转】kali配置--修改IP和DNS
  14. windows10下安装mysql-8.0.15-winx64以及连接服务器过程中遇到的一些问题
  15. 课程五(Sequence Models),第一 周(Recurrent Neural Networks) —— 2.Programming assignments:Dinosaur Island - Character-Level Language Modeling
  16. dp填表法,刷表法
  17. 10.30 正睿停课训练 Day12
  18. day_5.24py
  19. SSH高级服务
  20. thinkphp5 列表页数据分页查询3-带搜索条件

热门文章

  1. android学习笔记——利用BaseAdapter生成40个列表项
  2. 从3dMax导出供threeJS使用的带动作模型与加载
  3. Extjs更新grid
  4. python脚本如何同时运行多个
  5. B. 复读机的力量
  6. RabbitMQ 消息队列入门
  7. 实现一个简单的基于动态代理的 AOP
  8. 详解 Hashtable
  9. TortoiseSVN的使用,以及冲突解决办法
  10. Aria2任意文件写入漏洞