码迷,mamicode.com
首页 > 其他好文 > 详细

poj3436--ACM Computer Factory(最大流,拆点dinic)

时间:2014-12-09 23:10:51      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   ar   color   os   sp   for   strong   

ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5501   Accepted: 1887   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.
认真写的第一个题,被__int64 坑了 。
题目大意:组装电脑,p个组件,n个机器。0代表没有该组件,1代表一定有该组件,2代表可以有也可以没有。要求找出从初始什么组件都没有的电脑,组装成完整的电脑的最多的流水线。输出组装数,和机器连接方式。
从全是0为源点,全是1为汇点,每台机器拆点,然后连接出图来,再dinic。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define LL __int64
struct node
{
    int u , v , w ;
    int next , k ;
} p[1000000];
struct node1
{
    int u , v , w ;
} q[1000000] ;
queue <int> que ;
LL in[110] , out[110] , num[110] ;
int head[110] , cnt ;
int arr[110] , vis[110] ;
void init()
{
    cnt = 0 ;
    memset(in,0,sizeof(in)) ;
    memset(out,0,sizeof(out)) ;
    memset(head,-1,sizeof(head)) ;
}
void add(int u,int v,int w)
{
    p[cnt].u = u ;
    p[cnt].v = v ;
    p[cnt].w = w ;
    p[cnt].k = w ;
    p[cnt].next = head[u] ;
    head[u] = cnt++ ;
    p[cnt].u = v ;
    p[cnt].v = u ;
    p[cnt].w = 0 ;
    p[cnt].k = 0 ;
    p[cnt].next = head[v] ;
    head[v] = cnt++ ;
}
bool judge(LL x,LL y,int m)
{
    int i , k1 , k2 ;
    for(i = 1 ; i <= m ; i++)
    {
        k1 = x % 10 ;
        k2 = y % 10 ;
        x /= 10 ;
        y /= 10 ;
        if(k1 == 2 || k2 == 2)
            continue ;
        if( k1 != k2 )
            return false ;
    }
    return true ;
}
bool bfs(int s,int e)
{
    int i , u , v ;
    while( !que.empty() )
        que.pop();
    memset(arr,-1,sizeof(arr)) ;
    vis[s] = 1 ;
    arr[s] = 0 ;
    que.push(s) ;
    while( !que.empty() )
    {
        u = que.front() ;
        que.pop() ;
        for(i = head[u] ; i != -1 ; i = p[i].next )
        {
            v = p[i].v ;
            if( arr[v] == -1 && p[i].w )
            {
                arr[v] = arr[u] + 1 ;
                que.push(v) ;
            }
        }
    }
    if( arr[e] > 0 )
        return true ;
    return false ;
}
int dfs(int u,int t,int min1)
{
    if( u == t )
        return min1 ;
    int i , v , a , ans = 0 ;
    for(i = head[u] ; i != -1 ; i = p[i].next)
    {
        v = p[i].v ;
        if( arr[v] == arr[u] + 1  && p[i].w && ( a = dfs(v,t,min(min1,p[i].w) ) ) )
        {
            p[i].w -= a ;
            p[i^1].w += a ;
            ans += a ;
            min1 -= a ;
            if( !min1 ) break ;
        }
    }
    if( ans )
        return ans ;
    arr[u] = -1 ;
    return 0 ;
}
int main()
{
    int n , m , i , j , u , v , w , k , max_flow , sum ;
    while( scanf("%d %d", &m, &n) != EOF )
    {
        init() ;
        max_flow = sum = 0 ;
        for(i = 1 ; i <= n ; i++)
        {
            k = 0 ;
            scanf("%I64d", &num[i]) ;
            for(j = 0 ; j < m ; j++)
            {
                scanf("%d", &w) ;
                in[i] = in[i]*10 + w ;
                k = k*10 + 1 ;
            }
            for(j = 0 ; j < m ; j++)
            {
                scanf("%d", &w) ;
                out[i] = out[i]*10 + w ;
            }
        }
        for(i = 1 ; i <= n ; i++)
        {
            add(i,i+50,num[i]) ;
            if( judge(0,in[i],m) )
                add(0,i,INF) ;
            if( judge(k,out[i],m) )
                add(i+50,101,INF) ;
            for(j = 1 ; j <= n ; j++)
            {
                if( i == j ) continue ;
                if( judge(out[i],in[j],m) )
                    add(i+50,j,INF) ;
            }
        }
        while( bfs(0,101) )
        {
            while( k = dfs(0,101,INF) )
                max_flow += k ;
        }
        for(i = 51 ; i <= n+50 ; i++)
        {
            for(j = head[i] ; j != -1 ; j = p[j].next)
            {
                if( p[j].k == INF && p[j].u != 0 && p[j].v != 101 && p[j].w < p[j].k )
                {
                    q[sum].u = i-50 ;
                    q[sum].v = p[j].v ;
                    q[sum].w = p[j].k - p[j].w ;
                    sum++ ;
                }
            }
        }
        if( max_flow == 0 )
            printf("0 0\n") ;
        else
        {
            printf("%d %d\n", max_flow, sum) ;
            for(i = 0 ; i < sum ; i++)
                printf("%d %d %d\n", q[i].u, q[i].v, q[i].w) ;
        }
    }
    return 0;
}

poj3436--ACM Computer Factory(最大流,拆点dinic)

标签:des   style   io   ar   color   os   sp   for   strong   

原文地址:http://blog.csdn.net/winddreams/article/details/41830407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!