POJ 3436 ACM Computer Factory 最大流输出路径


题目:http://poj.org/problem?id=3436

题意:题意真心很难懂啊。。。产品有p个状态,给定n个机器,给出每个机器要求加工前的状态是什么,且给出加工后的状态是什么。产品初始时状态全部为0,完成加工时状态全部为1。加工前有三种状态,为0和1状态要求产品的对应位置也要是0和1,2状态的话可以随意,加工后只有0和1状态

思路:拆点建图,然后在从汇点出发搜索残余网络中的反向边,建图时所有反向边的容量全部为0,而残余网络中容量不为0的反向边对应的正向边必定有流量经过,统计这些边就好了

总结:不拆点的话会有bug,但题目数据较弱,也可以过。输出路径时重复输出了,,,

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int N = 210;
struct edge
{
int to, cap, id, next;
} g[N*N*10];
struct node
{
int v, u, cap;
}e;
vector<node> vec;
int head[N], pre[N], cur[N], gap[N], level[N];
int cnt, nv, cas = 0;
int arr[N], brr[N][N], crr[N][N];
int ss, tt;
int n, m;
bool vis[N];
void add_edge(int v, int u, int cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].id = 0, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].id = 1, g[cnt].next = head[u], head[u] = cnt++;
}
int sap(int s, int t)
{
memset(level, 0, sizeof level);
memset(gap, 0, sizeof gap);
memcpy(cur, head, sizeof head);
gap[0] = nv;
int v = pre[s] = s, flow = 0, aug = INF;
while(level[s] < nv)
{
bool flag = false;
for(int &i = cur[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] == level[u] + 1)
{
flag = true;
pre[u] = v;
v = u;
aug = min(aug, g[i].cap);
if(v == t)
{
flow += aug;
while(v != s)
{
v = pre[v];
g[cur[v]].cap -= aug;
g[cur[v]^1].cap += aug;
}
aug = INF;
}
break;
}
}
if(flag) continue;
int minlevel = nv;
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && minlevel > level[u])
minlevel = level[u], cur[v] = i;
}
if(--gap[level[v]] == 0) break;
level[v] = minlevel + 1;
gap[level[v]]++;
v = pre[v];
}
return flow;
}
bool judge1(int a[], int n)
{
for(int i = 1; i <= n; i++)
if(a[i] == 1)
return false;
return true;
}
bool judge2(int a[], int b[], int n)
{
for(int i = 1; i <= n; i++)
if(a[i] != b[i] && a[i] != 2) return false;
return true;
}
bool judge3(int a[], int n)
{
for(int i = 1; i <= n; i++)
if(a[i] != 1) return false;
return true;
}
void dfs(int v) //初始时所有的反向边容量全部为0,跑完最大流后反向边不为0的说明流经了对应的正向边
{
vis[v] = true;
for(int i = head[v]; i != -1; i = g[i].next)
{
if(g[i].id == 1 && g[i].cap > 0)
{
if(v != tt && g[i].to != ss && abs(v - g[i].to) != m)
{
e.v = g[i].to - m, e.u = v, e.cap = g[i].cap;
vec.push_back(e);
}
if(! vis[g[i].to])
dfs(g[i].to);
}
}
}
int main()
{
while(~ scanf("%d%d", &n, &m))
{
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 1; i <= m; i++)
{
scanf("%d", &arr[i]);
for(int j = 1; j <= n; j++)
scanf("%d", &brr[i][j]);
for(int j = 1; j <= n; j++)
scanf("%d", &crr[i][j]);
}
ss = 0, tt = 2 * m + 1;
for(int i = 1; i <= m; i++)
{
add_edge(i, m + i, arr[i]);
printf("%d %d\n", i, i + m);
if(judge1(brr[i], n))
add_edge(ss, i, arr[i]), printf("%d %d\n", ss, i);
if(judge3(crr[i], n))
add_edge(m + i, tt, arr[i]), printf("%d %d\n", m + i, tt);
for(int j = 1; j <= m; j++)
{
if(j == i) continue;
if(judge2(brr[j], crr[i], n))
add_edge(m + i, j, arr[i]), printf("%d %d\n", m + i, j);
}
}
nv = tt + 1;
printf("%d", sap(ss, tt));
memset(vis, 0, sizeof vis);
dfs(tt);
printf(" %d\n", vec.size());
for(int i = 0; i < vec.size(); i++)
printf("%d %d %d\n", vec[i].v, vec[i].u, vec[i].cap);
vec.clear();
}
return 0;
}

直接遍历残余网络求路径

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int N = 210;
struct edge
{
int to, cap, id, next;
} g[N*N*10];
struct node
{
int v, u, cap;
}e;
vector<node> vec;
int head[N], pre[N], cur[N], gap[N], level[N];
int cnt, nv, cas = 0;
int arr[N], brr[N][N], crr[N][N];
int ss, tt;
int n, m;
bool vis[N];
void add_edge(int v, int u, int cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].id = 0, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].id = 1, g[cnt].next = head[u], head[u] = cnt++;
}
int sap(int s, int t)
{
memset(level, 0, sizeof level);
memset(gap, 0, sizeof gap);
memcpy(cur, head, sizeof head);
gap[0] = nv;
int v = pre[s] = s, flow = 0, aug = INF;
while(level[s] < nv)
{
bool flag = false;
for(int &i = cur[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] == level[u] + 1)
{
flag = true;
pre[u] = v;
v = u;
aug = min(aug, g[i].cap);
if(v == t)
{
flow += aug;
while(v != s)
{
v = pre[v];
g[cur[v]].cap -= aug;
g[cur[v]^1].cap += aug;
}
aug = INF;
}
break;
}
}
if(flag) continue;
int minlevel = nv;
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && minlevel > level[u])
minlevel = level[u], cur[v] = i;
}
if(--gap[level[v]] == 0) break;
level[v] = minlevel + 1;
gap[level[v]]++;
v = pre[v];
}
return flow;
}
bool judge1(int a[], int n)
{
for(int i = 1; i <= n; i++)
if(a[i] == 1)
return false;
return true;
}
bool judge2(int a[], int b[], int n)
{
for(int i = 1; i <= n; i++)
if(a[i] != b[i] && a[i] != 2) return false;
return true;
}
bool judge3(int a[], int n)
{
for(int i = 1; i <= n; i++)
if(a[i] != 1) return false;
return true;
}
int main()
{
while(~ scanf("%d%d", &n, &m))
{
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 1; i <= m; i++)
{
scanf("%d", &arr[i]);
for(int j = 1; j <= n; j++)
scanf("%d", &brr[i][j]);
for(int j = 1; j <= n; j++)
scanf("%d", &crr[i][j]);
}
ss = 0, tt = 2 * m + 1;
for(int i = 1; i <= m; i++)
{
add_edge(i, m + i, arr[i]);
if(judge1(brr[i], n))
add_edge(ss, i, arr[i]);
if(judge3(crr[i], n))
add_edge(m + i, tt, arr[i]);
for(int j = 1; j <= m; j++)
{
if(j == i) continue;
if(judge2(brr[j], crr[i], n))
add_edge(m + i, j, arr[i]);
}
}
nv = tt + 1;
printf("%d", sap(ss, tt));

for(int i = m + 1; i <= 2 * m; i++)
for(int j = head[i]; j != -1; j = g[j].next)
{
if(i - m == g[j].to || g[j].to == tt) continue;//不是和自身相连,另外一端不是汇点
if(g[j^1].cap > 0) //反向边容量大于0
{
e.v = i - m, e.u = g[j].to, e.cap = g[j^1].cap;
vec.push_back(e);
}
}
printf(" %d\n", vec.size());
for(int i = 0; i < vec.size(); i++)
printf("%d %d %d\n", vec[i].v, vec[i].u, vec[i].cap);
vec.clear();
}
return 0;
}



智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告