您的位置:首页 > 其它

poj_3436 ACM Computer Factory(Dinic + 拆点)

2016-10-27 19:54 375 查看
ACM Computer Factory

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7212 Accepted: 2566 Special Judge
Description

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 P numbers 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,P Di,1 Di,2...Di,P,
where Qi specifies performance, Si,j — input specification for part j, Di,k — output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ N ≤ 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.

好难读懂的题。

因为题目中点与点之间均有可能相通,所以要做拆点操作,这道题的话就是将一个点分为入点与出点,

让每个点内的入点指向它的出点,流量为题目所给权值。然后如果两个点之间可以相通, 就连上一条有向边,流量为inf,

即最大整数值。这样就能保证每条路径中每个顶点最多访问一次。

然后就是多源多汇网络流了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int s, t;
int n, p;

struct Edge
{
int from, to, cap, flow;
};

vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
int cur[maxn];
bool vis[maxn];
int num_edge;

void addEdge(int u, int v, int c)
{
Edge edge;
edge.from = u, edge.to = v, edge.cap = c, edge.flow = 0;
edges.push_back(edge);
edge.from = v, edge.to = u, edge.cap = 0, edge.flow = 0;
edges.push_back(edge);

num_edge = edges.size();
G[u].push_back(num_edge - 2);
G[v].push_back(num_edge - 1);
}

bool BFS()
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s), d[s] = 0, vis[s] = 1;

while(!Q.empty())
{
int x = Q.front();
Q.pop();
for(int i = 0; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}

int DFS(int x, int a)
{
if(x == t || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}

int Dinic()
{
int flow = 0;
while(BFS())
{
memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}

struct Node
{
int c;
int in[12];
int out[12];
} nodes[maxn];

int main()
{
while(~scanf("%d%d", &p, &n))
{
s = 0, t = 2*n+1;

for(int i = s; i <= t; i++) G[i].clear();
edges.clear();

for(int i = 1; i <= n; i++)
{
scanf("%d", &nodes[i].c);
for(int j = 1; j <= p; j++) scanf("%d", &nodes[i].in[j]);
for(int j = 1; j <= p; j++) scanf("%d", &nodes[i].out[j]);
}

//1~n: in ; n+1~2*n: out
for(int i = 1; i <= n; i++)
{
addEdge(i, i+n, nodes[i].c);

int j;
for(j = 1; j <= p; j++)
if(nodes[i].in[j] == 1) break;
if(j > p) addEdge(s, i, inf);

for(j = 1; j <= p; j++)
if(nodes[i].out[j] != 1) break;
if(j > p) addEdge(i+n, t, inf);

for(int j = 1; j <= n; j++)
{
if(i == j) continue;
int flag = 0;
for(int k = 1; k <= p; k++)
{
if(nodes[i].out[k] + nodes[j].in[k] == 1)
{
flag = 1;
break;
}
}
if(flag) continue;
else addEdge(i+n, j, inf);
}
}
printf("%d ", Dinic());

int cot = 0;
for(int i = n+1; i <= 2*n; i++)
{
for(int j = 0; j < G[i].size(); j++)
{
Edge e = edges[G[i][j]];
if(e.flow > 0&& e.to >= 1 && e.to <= n) cot++;
}
}

printf("%d\n", cot);

for(int i = n+1; i <= 2*n; i++)
{
for(int j = 0; j < G[i].size(); j++)
{
Edge e = edges[G[i][j]];
if(e.flow > 0 && e.to >= 1 && e.to <= n )
printf("%d %d %d\n", e.from - n, e.to, e.flow);
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: