POJ3436------ACM Computer Factory

題目連接算法

ACM Computer Factory
Descriptionapp

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.ide

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.ui

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.this

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.spa

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.code

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

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.blog

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.three

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.

題意:有N臺機器生產電腦,每一個電腦有p個部件,,同時每一個機器都有一個效率。不一樣機器可以安裝的部件不必定同樣,面對不一樣的機器,咱們須要讓兩個機器對接起來。
題解: 這個題須要經過拆點去作,機器與機器是之間的傳輸是無窮大的可是機器內部並非,下面是我畫的第一個樣例的圖示
第一個樣例拆點後

我用的是EK算法。代碼不夠精煉,還望海涵。

#include<stdio.h>
#include<string.h>
#include<queue> 
#include<stack>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=100;
int in[55][20],out[50][20];
int cap[MAXN][MAXN];
int pre[MAXN];
bool vis[MAXN];
int p,n;
int N=n*2;
bool match(int *a,int *b)
{
    for(int i=1;i<=p;i++)
    {
        if(a[i]==b[i]) continue;
        if(b[i]==2) continue;
        return false;
    }
    return true;
}
bool matchs(int *a)
{
    for(int i=1;i<=p;i++)
    {
        if(a[i]==1)
            return false;
    }
    return true;
}
bool matcht(int *b)
{
    for(int i=1;i<=p;i++)
    {
        if(b[i]==0)
            return false;
    }
    return true;
}
bool bfs(int s,int t)
{
    memset(pre, -1, sizeof(pre));
    queue<int> que;
    memset(vis,0 , sizeof(vis));
    vis[s]=true;
    pre[s]=s;
    que.push(s);
    int p;
    while(!que.empty())
    {
        p=que.front();
        que.pop();
        for(int i=0;i<=N;i++)
        {
            if(!vis[i]&&cap[p][i]>0)
            {
                vis[i]=true;
                pre[i]=p;
                if(i==t) return true;
                que.push(i);
            }
        }
    }
    return false;
}
int EK(int s,int t)
{
    int ans=0,d;
    while (bfs(s,t))
    {
        d=INF;
        for(int i=t;i!=s;i=pre[i])
        d=min(d,cap[pre[i]][i]);
        for(int i=t;i!=s;i=pre[i])
        {
            cap[pre[i]][i]-=d;
            cap[i][pre[i]]+=d;
        }    
        ans+=d;
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&p,&n )!=EOF)
    {
        memset(cap,0 , sizeof(cap));
        int c;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&c);
            cap[2*i-1][2*i]=c;
            for(int j=1;j<=p;j++)
                scanf("%d",&in[i][j]);
            for(int j=1;j<=p;j++)
                scanf("%d",&out[i][j]);
        }
        N=2*n+1;
        for(int i=1;i<=n;i++)
        {
            if(matchs(in[i]))
            {
                cap[0][2*i-1]=INF;  
// printf("%d %d---\n",0,2*i-1);
            } 
            if(matcht(out[i])) 
            {
                cap[2*i][N]=INF;
// printf("%d %d++++\n",2*i,N);
            }
            for(int j=1;j<=n;j++)
            {
                if(match(out[i],in[j])) 
                {
                    cap[2*i][2*j-1]=INF;
// printf("%d %d##\n",2*i,2*j-1);
                }   
            }
        }
        printf("%d ",EK(0,N));
        int ans=0;
        int x[MAXN],y[MAXN],z[MAXN];
        for (int i = 1; i <n; ++i)
        {
            for(int j=1;j<=n;j++)
            {
                if(match(out[i],in[j])) 
                {
                    if(cap[2*i][2*j-1]!=INF)
                    {
                        ans++;
                        x[ans-1]=i;y[ans-1]=j;z[ans-1]=INF-cap[2*i][2*j-1];
                        if(z[ans-1]<0)//此處是考慮會把回去的邊計算上,咱們須要把他去掉。
                            ans--;

                    }   
                }   
            }
        }
        printf("%d\n",ans);
        for(int i=0;i<ans;i++)
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }
    return 0;
}
相關文章
相關標籤/搜索