POJ-3436(网络流+最大流+输出路径)
ACM Computer Factory
POJ-3436
- 题目就是一个工厂n个加工机器,每个机器有一个效率w,q个材料入口,q个材料出口,每个口有三个数表示状态,1表示一定有入/出的材料,0表示没有入/出的材料,2表示可能有入的材料。如果一个机器入口全是0,代表这是起始机器,如果一个机器出口全是1,那么代表这是末尾机器。
- 将每个机器i拆成两点i和i+n分别代表进和出
- 建立超级源点,连在起始机器上,流量INF。 建立超级汇点,找到末尾机器连在超级汇点上,流量INF。
- 一个机器拆成的两个点i和i+n连上,流量就是这个点的效率w。
- 然后暴力匹配,看一个点的所有出口是否可以完全对应一个点的入口,如果可以,匹配上,流量INF。
- 跑EK算法,得到最大流。
这里的输出路径不太理解
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<sstream>
#include<vector>
#include<queue>
using namespace std;
const int maxn=110;
const int INF=0X3F3F3F3F;
int p1,n,m;
int s,t;
int q[maxn],port[maxn][maxn];
struct node{
int from,to,flow;
};
struct Edge {
int from, to, cap, flow;
Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};
vector<Edge> edges;//这里的边是实际边数的两倍,包括反向边
vector<int> G[maxn];//邻接表,G[i][j]表示结点i的第j条边在edges数组中的序号
int a[maxn];//a[i]表示起点到i的可改进量
int p[maxn];//edges数组中的编号,最短路图上p的入弧编号
void init(int n) {
for (int i = 0; i <= n; i++)
G[i].clear();
edges.clear();
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));//反向弧,容量为0
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
int Maxflow(int s, int t) {
int flow = 0;
for (;;) {
memset(a, 0, sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (!a[e.to] && e.cap > e.flow) {
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap - e.flow);
Q.push(e.to);
}
}
if (a[t]) break;
}
if (!a[t]) break;
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
flow += a[t];
}
return flow;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>p1>>n){
init(2*n+1);
s=0,t=2*n+1;
for(int i=1;i<=n;i++){
cin>>q[i];
bool flag=true;
for(int j=0;j<p1;j++){
cin>>port[i][j];
if(port[i][j]==1){
flag=false;
}
}
if(flag)
AddEdge(s,i,INF);
flag=true;
for(int j=0;j<p1;j++){
cin>>port[i+n][j];
if(port[i+n][j]!=1)
flag=false;
}
if(flag)
AddEdge(i+n,t,INF);
}
for(int i=1;i<=n;i++)
AddEdge(i,i+n,q[i]);
for(int i=n+1;i<=2*n;i++){
for(int j=1;j<=n;j++){
if(i==j)
continue;
bool flag=true;
for(int k=0;k<p1;k++){
if(port[i][k]==port[j][k]||port[j][k]==2){
continue;
}
flag=false;
break;
}
if(flag)
AddEdge(i,j,INF);
}
}
int ans=Maxflow(s,t);
vector<node> v;
for(int i=0;i<edges.size();i+=2){
if(edges[i].from<=n||edges[i].to>=1+n||edges[i].to==2*n+1||edges[i].from==0)
continue;
if(edges[i].flow){
v.push_back(node{edges[i].from-n,edges[i].to,edges[i].flow});
}
}
cout<<ans<<" "<<v.size()<<endl;
for(int i=0;i<v.size();i++){
cout<<v[i].from<<" "<<v[i].to<<" "<<v[i].flow<<endl;
}
}
return 0;
}
POJ-3436(网络流+最大流+输出路径)的更多相关文章
- (poj)3414 Pots (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- A - ACM Computer Factory POJ - 3436 网络流
A - ACM Computer Factory POJ - 3436 As you know, all the computers used for ACM contests must be ide ...
- ACM Computer Factory POJ - 3436 网络流拆点+路径还原
http://poj.org/problem?id=3436 每台电脑有$p$个组成部分,有$n$个工厂加工电脑. 每个工厂对于进入工厂的半成品的每个组成部分都有要求,由$p$个数字描述,0代表这个部 ...
- POJ 1149PIGS 网络流 最大流
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 9320 Description ...
- A - ACM Computer Factory - poj 3436(最大流)
题意:有一个ACM工厂会生产一些电脑,在这个工厂里面有一些生产线,分别生产不同的零件,不过他们生产的电脑可能是一体机,所以只能一些零件加工后别的生产线才可以继续加工,比如产品A在生产线1号加工后继续前 ...
- ACM Computer Factory - poj 3436 (最大流)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5949 Accepted: 2053 Special Judge ...
- POJ-3436-ACM Computer Factory(最大流, 输出路径)
链接: https://vjudge.net/problem/POJ-3436#author=0 题意: 为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产 ...
- POJ 1273 网络流(最大流)模板
http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...
- poj 3436 网络流构图经典
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6012 Accepted: 2 ...
随机推荐
- python的threading的使用(join方法,多线程,锁threading.Lock和threading.Condition
一.开启多线程方法一 import threading,time def write1(): for i in range(1,5): print('1') time.sleep(1) def wri ...
- C# 同步和异步(概念理解)
C# 同步和异步 IO 概念区分 四个相关概念: 同步(Synchronous) 异步( Asynchronous) 阻塞( Blocking ) 非阻塞( Nonblocking) 同步异步图解 ...
- transformers---FloatProgress not found. Please update jupyter and ipywidgets.
问题 运行 huggingface transformers 的 demo,报错FloatProgress not found.具体如下: import torch from transformers ...
- Chapter Zero 0.1.1 计算机硬件五大单元
计算机硬件的五大单元 先从CPU说起,CPU的全称如下: 中央处理器(Central Processing Unit,CPU) CPU作为一个特定功能的芯片,内含微指令集, 主机的功能差异,主要参考C ...
- Kubernets二进制安装(7)之部署主控节点服务--apiserver二进制安装
kube-apiserver集群规划 主机名 角色 IP地址 mfyxw30.mfxyw.com kube-apiserver主 192.168.80.30 mfyxw40.mfyxw.com kub ...
- MySQL数据库系列(四)- InnoDB下的共享表空间和独立表空间详解
一.概念 共享表空间: Innodb的所有数据保存在一个单独的表空间里面,而这个表空间可以由很多个文件组成,一个表可以跨多个文件存在,所以其大小限制不再是文件大小的限制,而是其自身的限制.从Innod ...
- github 无法访问
描述: 1. ping 丢失 100% 2. git 失败 Failed to connect to github.com port 443: Timed out 3.打开网站 超时 解决: http ...
- ysoserial-URLDNS学习
简述 ysoserial很强大,花时间好好研究研究其中的利用链对于了解java语言的一些特性很有帮助,也方便打好学习java安全的基础,刚学反序列化时就分析过commoncollections,但是是 ...
- 让你像黑客一样写代码(not really)
让你像黑客一样写代码(not really) http://poznan.tvp.pl 这是一个波兰的视频网站. poznan 波兹南(波兰城市 视屏链接 http://video.sina.com. ...
- ADN vs CDN All In One
ADN vs CDN All In One Netlify & JAMstack https://app.netlify.com/teams/xgqfrms/sites ADN Applica ...