kuangbin求带飞网络流 ACM Computer Factory(最大流节点型)

针对ACM计算机工厂的生产线优化问题,通过构建最大流模型,实现了计算机组装效率的最大化。文章详细介绍了如何处理多源多汇的问题,并给出了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - ACM Computer Factory
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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,1Si,2...Si,P Di,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
总结:
搞懂题意以后就是很明显的最大流问题,但还是有一些问题,一个就是给定的流量都是限制节点的而不是边的,所以我们先要寻找规律,经过几次尝试以后发现,任意两个节点之间的边的流量应该是起点和终点节点流量上线的最小值,这是一点,还有一点就是这道题有许多的起点和终点,所以我们用那个图论中常用的多起点多终点的套路,虚拟出来一个终点和起点,就好了,至于最后怎么输出任意两点之间的流量,可以看反向边的流量
//
//  main.cpp
//  ACM Computer Factory
//
//  Created by 张嘉韬 on 16/8/26.
//  Copyright © 2016年 张嘉韬. All rights reserved.
//

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
const int inf=9999999;
const int maxn=60;
int p,n,map[maxn][maxn],v[maxn],ormap[maxn][maxn],ans,ji[maxn][20];
int input[maxn][20],output[maxn][20];
int dfs()
{
    int flag=0,k;
    stack <int> st;
    int vis[maxn],pre[maxn];
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    st.push(0);
    pre[0]=-1;
    while(!st.empty())
    {
        int temp=st.top();
        st.pop();
        for(int i=0;i<=n+1;i++)
        {
            if(map[temp][i]&&!vis[i])
            {
                vis[i]=1;
                st.push(i);
                pre[i]=temp;
                if(i==n+1)
                {
                    flag=1;
                    break;
                }
            }
        }
    }
    int MinFlow=inf;
    if(flag)
    {
        //cout<<"shit"<<endl;
        k=n+1;
        while(k!=0)
        {
            //cout<<k<<" ";
            MinFlow=min(map[pre[k]][k],MinFlow);
            //cout<<MinFlow<<" "<<endl;
            k=pre[k];
            
        }
        k=n+1;
        //cout<<"shit2"<<endl;
        while(k!=0)
        {
            map[pre[k]][k]-=MinFlow;
            map[k][pre[k]]+=MinFlow;
            k=pre[k];
        }
        //cout<<MinFlow<<endl;
        return MinFlow;
    }
    else return 0;
    
}
int isStart(int a)
{
    for(int i=0;i<p;i++) if(input[a][i]==1) return 0;
    return 1;
}
int isEnd(int a)
{
    for(int i=0;i<p;i++) if(output[a][i]!=1) return 0;
    return 1;
}
int legal(int a,int b)
{
    for(int i=0;i<p;i++)
    {
        int out=output[a][i];
        int in=input[b][i];
        if(out&&!in) return 0;
        else if(!out&&in==1) return 0;
    }
    return 1;
}
void init()
{
    memset(map,0,sizeof(map));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(legal(i,j)&&i!=j) map[i][j]=min(v[i],v[j]);
    for(int i=1;i<=n;i++)
    {
        if(isStart(i)) map[0][i]=v[i];
        if(isEnd(i)) map[i][n+1]=v[i];
    }
    for(int i=0;i<=n+1;i++)
        for(int j=0;j<=n+1;j++)
            ormap[i][j]=map[i][j];
}
void print()
{
//    printf("%d\n",ans);
//    for(int i=1;i<=n;i++)
//        for(int j=1;j<=n;j++)
//            if(ormap[i][j]&&map[j][i])
//                printf("%d %d %d\n",i,j,map[j][i]);
    int cut=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(ormap[i][j]>map[i][j])
            {
                ji[cut][0]=i;
                ji[cut][1]=j;
                ji[cut][2]=ormap[i][j]-map[i][j];
                cut++;
            }
        }
    }
    printf("%d %d\n",ans,cut);
    for(int i=0;i<cut;i++)
    {
        printf("%d %d %d\n",ji[i][0],ji[i][1],ji[i][2]);
    }
}
void show()
{
    for(int i=0;i<=n+1;i++)
    {
        for(int j=0;j<=n+1;j++)
        cout<<map[i][j]<<" ";
        cout<<endl;
    }
}
void displayout(int a)
{
    for(int i=0;i<p;i++) {cout<<"("<<i<<")";cout<<output[a][i]<<" ";}
    cout<<endl;
}
void displayin(int a)
{
    for(int i=0;i<p;i++) {cout<<"("<<i<<")";cout<<input[a][i]<<" ";}
    cout<<endl;
}
int main(int argc, const char * argv[]) {
    //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);
    while(scanf("%d%d",&p,&n)!=EOF)
    {
        //cout<<p<<" "<<n;
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
            for(int j=0;j<p;j++) scanf("%d",&input[i][j]);
            for(int j=0;j<p;j++) scanf("%d",&output[i][j]);
        }
//        displayout(1);
//        displayin(3);
//        cout<<legal(1,3)<<endl;
        init();
        //show();
        int MinFlow=0;
        while((MinFlow=dfs())) ans+=MinFlow;
        //cout<<"shit"<<endl;
        print();
        //cout<<"********"<<endl;
    }
    return 0;
}



### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin”专题十四涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了一本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另一个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定一组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单的搜索题目而言,在为期约两周的时间里完成了这一部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进一步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值