Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8309 | Accepted: 3017 | 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,1Di,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
Source
Northeastern Europe 2005, Far-Eastern Subregion
解题思路:把每个工厂拆成两个点,一个点为工厂需要的原料,一个点为工厂的产品,这两个点的容量为Qi,然后将每个工厂的产出点与其他工厂原料点相连(如果两点不矛盾,也就是产出可以作为另一个工厂的原料),容量为无穷大,定义一个源点s,连向所有的000点(不含1的都可以),定义一个汇点t,所有111的点连向这个汇点,这样我们求一个s到t的最大流就行。
#include <stdio.h>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int p, n;
int s, t;
int tot;
int level[1000];//每个点的层次
bool visit[1000];
int head[1000];
struct Factory{
int In[15], Out[15], value;
}Fa[60];
struct edge{
int v, w, last;
}Edge[1000];
struct info{
int u, v, w;
bool operator <(const info &res) const {
if(u == res.u) return v < res.v;
else return u < res.u;
}
}Info[1000];
edge Ori[1000];//备份原图
void add(int u, int v, int w)
{
Edge[tot].v = v;
Edge[tot].w = w;
Edge[tot].last = head[u];
head[u] = tot++;
//添加反向边
Edge[tot].v = u;
Edge[tot].w = 0;
Edge[tot].last = head[v];
head[v] = tot++;
}
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
s = 0;
t = 2 * n + 1;
}
bool judge_s(int x)
{
for(int i = 1; i <= p; i++)
{
if(Fa[x].In[i] == 1) return false;
}
return true;
}
bool judge_t(int x)
{
for(int i = 1; i <= p; i++)
{
if(Fa[x].Out[i] != 1) return false;
}
return true;
}
bool judge(int x, int y)//判断x工厂的产物是否能作为y工厂的原料
{
for(int i = 1; i <= p; i++)
{
if(Fa[y].In[i] == 2) continue;
else if(Fa[y].In[i] == 0 && Fa[x].Out[i] == 1) return false;
else if(Fa[y].In[i] == 1 && Fa[x].Out[i] == 0) return false;
}
return true;
}
bool bfs()
{
queue<int> q;
while(!q.empty()) q.pop();
memset(level, -1, sizeof(level));
memset(visit, false, sizeof(visit));
q.push(s);
level[s] = 0;
while(!q.empty())
{
int u = q.front();
q.pop();
if(u == t) return true;
visit[u] = true;
for(int i = head[u]; i != -1; i = Edge[i].last)
{
int v = Edge[i].v;
if(!visit[v] && Edge[i].w)
{
visit[v] = true;
level[v] = level[u] + 1;
q.push(v);
}
}
}
return false;
}
int dfs(int ss, int tt, int ff)
{
//ss 为起点, tt 为终点, ff为流入起点的流量
int res = 0;//流入该点后已经使用过的流量
if(ss == tt) return ff;
for(int i = head[ss]; i != -1; i = Edge[i].last)
{
int v = Edge[i].v;
int w = Edge[i].w;
int term = min(ff - res, w);
if(level[v] == level[ss] + 1 && term)
{
int temp = dfs(v, tt, term);
Edge[i^1].w += temp;
Edge[i].w -= temp;
res += temp;
if(res == ff) return res;
}
}
return res;
}
int Dinic()
{
int Maxflow = 0;
while(bfs())
{
Maxflow += dfs(s, t, inf);
//cout<<"Maxflow == "<<Maxflow<<endl;
}
return Maxflow;
}
int main()
{
scanf("%d%d", &p, &n);
init();
for(int i = 1; i <= n; i++)
{
scanf("%d", &Fa[i].value);
for(int j = 1; j <= p; j++)
{
scanf("%d", &Fa[i].In[j]);
}
for(int j = 1; j <= p; j++)
{
scanf("%d", &Fa[i].Out[j]);
}
}
//建图
for(int i = 1; i <= n; i++)
{
if(judge_s(i)) add(s, i, inf);
if(judge_t(i)) add(i, t, Fa[i].value);
else add(i, i + n, Fa[i].value);
for(int j = 1; j <= n; j++)
{
if(j == i) continue;
if(judge(i, j)) add(i + n, j, Fa[i].value);
}
}
//备份原图
for(int i = 0; i < tot; i++)
{
Ori[i] = Edge[i];
}
//求解最大流
int Maxflow = Dinic();
//利用原图进行求解边
int ans = 0;
for(int i = 1; i <= n; i++)
{
if(judge_t(i)) continue;
for(int j = head[i + n]; j != -1; j = Edge[j].last)
{
int v = Edge[j].v;
int w = Edge[j].w;
if(v == i) continue;
int ww = Ori[j].w;
if(w == ww) continue;
Info[++ans].u = i;
Info[ans].v = v;
Info[ans].w = ww - w;
}
}
printf("%d %d\n", Maxflow, ans);
for(int i = 1; i <= ans; i++)
{
printf("%d %d %d\n", Info[i].u, Info[i].v, Info[i].w);
}
return 0;
}