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 ...
随机推荐
- G - Can you answer these queries? & N - 花神游历各国
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to us ...
- 【转】分布式事务之——tcc-transaction分布式TCC型事务框架搭建与实战案例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/73731363 一.背景 有一定分布式开发经验的朋友都知道,产品/项目/系统最初为了 ...
- 通过k8s部署dubbo微服务并接入ELK架构
需要这样一套日志收集.分析的系统: 收集 -- 能够采集多种来源的日志数据 (流式日志收集器) 传输 -- 能够稳定的把日志数据传输到中央系统 (消息队列) 存储 -- 可以将日志以结构化数据的形式存 ...
- 部署开源IP管理工具phpIPAM
一.安装环境程序: yum install httpd mariadb-server php php-cli php-gd php-common php-ldap php-pdo php-pear p ...
- NoSQL 数据库案例实战 -- MongoDB数据备份、恢复
MySQL数据迁移到MongoDB数据库中 前言 一.数据备份 二.数据恢复 前言 本环境是基于 Centos 7.8 系统构建mongodb-enterprise-4.2.8学习环境具体构建,请参考 ...
- html5 image>usemap (attribute)
# html5 image>usemap (attribute) https://caniuse.com/#search=usemap http://www.w3.org/TR/html5 ...
- TypeScript & WebAssembly
TypeScript & WebAssembly WASM (module (func (param $lhs i32) (param $rhs i32) (result i32) local ...
- dart2native 使用Dart 在macOS,Windows或Linux上创建命令行工具
下载dart2.6以上 >dart2native --help 编写源文件 // bin\main.dart main(List<String> args) { print('hel ...
- 华盛顿邮报专访:SPC能否再掀起币圈新浪潮?
近日,美国知名媒体华盛顿邮报对话NGK灵石团队技术副总裁Daphne Patel女士,对话主题为"SPC能否再掀起币圈新浪潮".此次对话以问答的形式展开,将SPC的最新情况呈现在你 ...
- 聊一下PBN程序图例中的XTT
PBN航路点的定位容差用XTT与ATT来表示,ATT=0.8*XTT.保护区半宽用1.5*XTT+BV计算得到,BV值在不同的航段取值不同. 对于A至E类航空器,距ARP 30nm以外BV值为2 ...