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 ...
随机推荐
- codeforces626E.Simple Skewness(三分)
Define the simple skewness of a collection of numbers to be the collection's mean minus its median. ...
- 牛客小白月赛30 B.最好的宝石 (线段树)
题意:RT. 题解:很明显的线段树维护区间最大值操作,但是我们同时还要维护最大值的个数,我们在build或者modify操作完子树然后push_up的时候,我们先从两个儿子取max更新父节点的最大值, ...
- Warm up HDU - 4612 树的直径
题意:给出n个点和m条边的无向图,存在重边,问加一条边以后,剩下的桥的数量最少为多少. 题解: 你把这个无向图缩点后会得到一个只由桥来连接的图(可以说这个图中的所有边都是桥,相当于一棵树),然后我们只 ...
- Codeforces Round #646 (Div. 2) B. Subsequence Hate (思维,前缀和)
题意:给你一个只含有\(0\)和\(1\)的字符串,每次操作可以将\(0\)改成\(1\)或\(1\)改成\(0\),问最少操作多少次,使得子序列中不含有\(010\)和\(101\). 题解:仔细想 ...
- 2019 ICPC Asia Taipei-Hsinchu Regional Problem K Length of Bundle Rope (贪心,优先队列)
题意:有\(n\)堆物品,每次可以将两堆捆成一堆,新堆长度等于两个之和,每次消耗两个堆长度之和的长度,求最小消耗使所有物品捆成一堆. 题解:贪心的话,每次选两个长度最小的来捆,这样的消耗一定是最小的, ...
- python爬虫笔记Day01
python爬虫笔记第一天 Requests库的安装 先在cmd中pip install requests 再打开Python IDM写入import requests 完成requests在.py文 ...
- k8s二进制部署 - coredns安装
coredns的资源清单文件rabc.yaml apiVersion: v1 kind: ServiceAccount metadata: name: coredns namespace: kube- ...
- codeforces - 15C Industrial Nim(位运算+尼姆博弈)
C. Industrial Nim time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- Unknown command '\b'. 关于Mysql导入外部数据库脚本报错的解决
来自网络转载 还是字符集的问题 使用source导入外部sql文件: mysql> source F:\php\bookorama.sql;--------------source F:---- ...
- ARM cache 工作方式
综述 现在的CPU的速度远远高于现代存储器的速度所以出现了cache.按和CPU距离分为L1级和L2级(SM)等.这里主要记录一下自己对cache的工作方式的学习理解. cache工作方式 cahe是 ...