zoukankan      html  css  js  c++  java
  • ACM Computer Factory (邻接矩阵 dinic 模板)(最大流+路径输出)POJ

    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.

    很裸的一道题

    网络流首发ac

    其实dinic 代码并不算长

    本体难在建模 只要有了想法 把图建好

    那么我们直接用网络流模板dinic就可以了

    我们只需要将每个机器拆成两割点 在中间连一条边

    这条边 的流量代表效率

    然后再暴力匹配 把每一个能匹配的机器建立单向边

    那么就可以很轻易的ac了

    #include <iostream>
    #include <queue>
    #include<map>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    
    const int INF = 0x7fffffff;
    int p,n;
    int level[205];
    int Si, Ei, Ci;
    
    int node[105][55];
    int va[105];
    int vis[105][105];
    int to[105][3];
    
    struct Dinic
    {
        int c;
        int f;
    } edge[205][205];
    
    bool dinic_bfs()      //bfs方法构造层次网络
    {
        //cout<<"level"<<endl;
        queue<int> q;
        memset(level, 0, sizeof(level));
        q.push(0);
        level[0] = 1;
        int u, v;
        while (!q.empty())
        {
            u = q.front();
            q.pop();
            for (v = 1; v <= 2*n+1; v++)
            {
                if (!level[v] && edge[u][v].c>edge[u][v].f)
                {
                    level[v] = level[u] + 1;
                    q.push(v);
                }
            }
        }
        return level[2*n+1] != 0;                //question: so it must let the sink node is the Mth?/the way of yj is give the sink node's id
    }
    
    int dinic_dfs(int u, int cp)             //use dfs to augment the flow
    {
        int tmp = cp;
        int v, t;
        if (u == 2*n+1)
            return cp;
        for (v = 1; v <= 2*n+1&&tmp; v++)
        {
            if (level[u] + 1 == level[v])
            {
                if (edge[u][v].c>edge[u][v].f)
                {
                    t = dinic_dfs(v, min(tmp, edge[u][v].c - edge[u][v].f));
                    edge[u][v].f += t;
                    edge[v][u].f -= t;
                    tmp -= t;
                }
            }
        }
        return cp - tmp;
    }
    
    int dinic()
    {
        int sum=0, tf=0;
        while (dinic_bfs())
        {
            while (tf = dinic_dfs(0, INF))
                sum += tf;
        }
        return sum;
    }
    
    int main()//0为起点 2n+1为终点 奇数为边的起点 偶数为边的终点
    {
            scanf("%d%d",&p,&n);
            memset(vis,0,sizeof(vis));
            memset(node,0,sizeof(node));
            memset(edge,0,sizeof(edge));
            for(int i=1;i<=p;i++) node[2*n+1][i]=1;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&edge[2*i-1][2*i].c);
                for(int j=1;j<=p;j++)
                    scanf("%d",&node[2*i-1][j]);
                for(int j=1;j<=p;j++)
                    scanf("%d",&node[2*i][j]);
            }
            for(int i=1;i<=n;i++)//起点为奇数
            {
                for(int j=0;j<=n;j++)//重点为偶数
                {
                    int tmp1=i*2-1;
                    int tmp2=j*2;
                    int flag=0;
                    for(int k=1;k<=p;k++)
                    {
                        if(node[tmp1][k]!=2&&node[tmp1][k]!=node[tmp2][k])
                        {
                            flag=1;
                        }
                    }
                    if(flag==0)
                    {
                        edge[tmp2][tmp1].c=1e9;
                        vis[tmp2][tmp1]=1;
                    }
                }
            }
            for(int i=1;i<=n;i++)
            {
                int flag=0;
                int tmp1=i*2;
                for(int j=1;j<=p;j++)
                {
                    if(node[tmp1][j]!=1)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0) edge[tmp1][2*n+1].c=1e9;
            }
    //        for(int i=0;i<=2*n+1;i++)
    //        {
    //            for(int j=0;j<=2*n+1;j++)
    //            {
    //                printf("%10d ",edge[i][j].c);
    //            }
    //            printf("
    ");
    //        }
            int ans=dinic();
            int num=0;
            for(int i=1;i<=n;i++)//由一个终点到达另外的一个起点
            {
                for(int j=1;j<=n+1;j++)
                {
                    int tmp1=2*i;
                    int tmp2=2*j-1;
                    if(vis[tmp1][tmp2]==1&&edge[tmp1][tmp2].f>0)
                    {
                        num++;
                        to[num][0]=i;
                        to[num][1]=j;
                        to[num][2]=edge[tmp1][tmp2].f;
                    }
                }
            }
            printf("%d %d
    ",ans,num);
            for(int i=1;i<=num;i++)
            {
                printf("%d %d %d
    ",to[i][0],to[i][1],to[i][2]);
            }
        return 0;
    }
    
  • 相关阅读:
    笔记20200521002:多线程【线程的优先级】
    笔记20200521001:多线程【守护线程】
    笔记20200520:多线程【线程强制执行_join】
    笔记20200519:多线程【线程礼让_yield】
    笔记20200518:多线程【线程休眠_sleep】
    2020.4.4号全国疫情哀悼日网页变灰色前端是如何实现的?-pink老师
    2020年最新版Web前端学习路线图-前端小白入门必读-pink老师推荐
    ECharts数据可视化项目-大屏数据可视化展示-echarts 图表入门基础视频制作-pink老师直播课更新完毕
    2019前端学习路线心得-黑马程序员pink老师
    2019年最新超级有趣好玩的html+css网页布局课程,前端入门基础,html5+css3零基础入门课程-黑马程序员pink老师精心录制
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852274.html
Copyright © 2011-2022 走看看