Uva 11506 Angry Programmer ( 最小割最大流,拆点,建图)
2013-03-06 18:44
218 查看
这个道题,第一次邻接表写的,wa了,然后换的矩阵,结果还是不对,但是发现哪里错了
主要是建图,判断两个点是不是起点或者汇点,对这两个点,就不要拆了,因为没有费用
其余的所有的点,都用拆成两个点,然后容量为这个computer的费用,其他的就按照规则,从原始点进,从拆出的点出
矩阵的代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 100;
const int INF = 100000000;
int n, m;
int cap
, flow
;
int p
, a
;
int maxflow() {
queue<int> q;
memset(flow, 0, sizeof(flow));
int f = 0;
while ( 1 ) {
memset(a, 0, sizeof(a));
a[1] = INF;
q.push(1);
while ( !q.empty() ) {
int u = q.front(); q.pop();
for ( int v = 1; v <= n + n - 1; v++ ) if ( !a[v] && cap[u][v] > flow[u][v] ) {
p[v] = u;
a[v] = min( a[u], cap[u][v] - flow[u][v] );
q.push(v);
}
}
if ( a
== 0 ) break;
for ( int u = n; u != 1; u = p[u] ) {
//printf("%d ", u );
flow[p[u]][u] += a
;
flow[u][p[u]] -= a
;
}
f += a
;
}
return f;
}
int main()
{
while ( scanf("%d%d", &n, &m) != EOF && !( !n && !m ) ) {
memset( cap, 0, sizeof(cap) );
for ( int i = 2; i < n; ++i ) {
int com, c;
scanf("%d%d", &com, &c);
cap[com][com+n] = cap[com+n][com] = c;
}
for ( int i = 0; i < m; ++i ) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
if ( u == 1 && v != n ) cap[u][v] = cap[v+n][u] = c;
else if ( u == 1 && v == n ) cap[u][v] = cap[v][u] = c;
else if ( u == n && v != 1 ) cap[v+n][u] = cap[u][v] = c;
else if ( u == n && v == 1 ) cap[u][v] = cap[v][u] = c;
else cap[u+n][v] = cap[v+n][u] = c;
}
/*for ( int i = 1; i < 2*n; ++i ) {
for ( int j = 1; j < 2*n; ++j ) printf("%d ", cap[i][j]);
printf("\n");
}*/
int ans = maxflow();
printf("%d\n", ans);
}
}
邻接表代码:(注意 边数要×4)
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 150;
const int M = 4500;
const int INF = 100000000;
struct edge {
int v, next, cap, flow;
}e[M];
int n, m, id;
int head
;
int maxflow() {
int f = 0, a
, p
, r
;
while ( 1 ) {
queue <int> q;
int s = 1, t = n, np = n + n - 1;
q.push(s);
memset( a, 0, sizeof(a) );
a[s] = INF;
while ( !q.empty() ) {
int u = q.front(); q.pop();
for ( int i = head[u]; i != -1; i = e[i].next ) {
int v = e[i].v, cap = e[i].cap, flow = e[i].flow;
if ( cap > flow && !a[v] ) {
a[v] = min( a[u], cap - flow );
p[v] = u;
r[v] = i;
q.push(v);
}
}
}
if ( a[t] == 0 ) break;
for ( int u = t; u != s; u = p[u] ) {
int ei = r[u];
e[ei].flow += a[t];
e[ei^1].flow -= a[t];
}
f += a[t];
}
return f;
}
void add( int u, int v, int c ) {
e[id].next = head[u];
e[id].v = v, e[id].cap = c, e[id].flow = 0;
head[u] = id++;
e[id].next = head[v];
e[id].v = u, e[id].cap = 0, e[id].flow = 0;
head[v] = id++;
}
int main()
{
while ( scanf("%d%d", &n, &m) != EOF && !(!n && !m) ) {
id = 0;
for ( int i = 0; i <= 2*n; ++i ) head[i] = -1;
for ( int i = 2, node, cost; i < n; ++i ) {
scanf("%d%d", &node, &cost);
add( node, node + n, cost );
add( node + n, node, cost );
}
for ( int i = 0; i < m; ++i ) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
if ( u == 1 && v == n ) add( u, v, c ), add( v, u, c );
else if ( u == 1 && v != n ) add( u, v, c ), add( v+n, u, c ) ;
else if ( v == 1 && u == n ) add( v, u, c ), add( u, v, c );
else add( v + n, u, c ), add( u+n, v, c );
}
printf("%d\n", maxflow() );
}
}
主要是建图,判断两个点是不是起点或者汇点,对这两个点,就不要拆了,因为没有费用
其余的所有的点,都用拆成两个点,然后容量为这个computer的费用,其他的就按照规则,从原始点进,从拆出的点出
矩阵的代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 100;
const int INF = 100000000;
int n, m;
int cap
, flow
;
int p
, a
;
int maxflow() {
queue<int> q;
memset(flow, 0, sizeof(flow));
int f = 0;
while ( 1 ) {
memset(a, 0, sizeof(a));
a[1] = INF;
q.push(1);
while ( !q.empty() ) {
int u = q.front(); q.pop();
for ( int v = 1; v <= n + n - 1; v++ ) if ( !a[v] && cap[u][v] > flow[u][v] ) {
p[v] = u;
a[v] = min( a[u], cap[u][v] - flow[u][v] );
q.push(v);
}
}
if ( a
== 0 ) break;
for ( int u = n; u != 1; u = p[u] ) {
//printf("%d ", u );
flow[p[u]][u] += a
;
flow[u][p[u]] -= a
;
}
f += a
;
}
return f;
}
int main()
{
while ( scanf("%d%d", &n, &m) != EOF && !( !n && !m ) ) {
memset( cap, 0, sizeof(cap) );
for ( int i = 2; i < n; ++i ) {
int com, c;
scanf("%d%d", &com, &c);
cap[com][com+n] = cap[com+n][com] = c;
}
for ( int i = 0; i < m; ++i ) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
if ( u == 1 && v != n ) cap[u][v] = cap[v+n][u] = c;
else if ( u == 1 && v == n ) cap[u][v] = cap[v][u] = c;
else if ( u == n && v != 1 ) cap[v+n][u] = cap[u][v] = c;
else if ( u == n && v == 1 ) cap[u][v] = cap[v][u] = c;
else cap[u+n][v] = cap[v+n][u] = c;
}
/*for ( int i = 1; i < 2*n; ++i ) {
for ( int j = 1; j < 2*n; ++j ) printf("%d ", cap[i][j]);
printf("\n");
}*/
int ans = maxflow();
printf("%d\n", ans);
}
}
邻接表代码:(注意 边数要×4)
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 150;
const int M = 4500;
const int INF = 100000000;
struct edge {
int v, next, cap, flow;
}e[M];
int n, m, id;
int head
;
int maxflow() {
int f = 0, a
, p
, r
;
while ( 1 ) {
queue <int> q;
int s = 1, t = n, np = n + n - 1;
q.push(s);
memset( a, 0, sizeof(a) );
a[s] = INF;
while ( !q.empty() ) {
int u = q.front(); q.pop();
for ( int i = head[u]; i != -1; i = e[i].next ) {
int v = e[i].v, cap = e[i].cap, flow = e[i].flow;
if ( cap > flow && !a[v] ) {
a[v] = min( a[u], cap - flow );
p[v] = u;
r[v] = i;
q.push(v);
}
}
}
if ( a[t] == 0 ) break;
for ( int u = t; u != s; u = p[u] ) {
int ei = r[u];
e[ei].flow += a[t];
e[ei^1].flow -= a[t];
}
f += a[t];
}
return f;
}
void add( int u, int v, int c ) {
e[id].next = head[u];
e[id].v = v, e[id].cap = c, e[id].flow = 0;
head[u] = id++;
e[id].next = head[v];
e[id].v = u, e[id].cap = 0, e[id].flow = 0;
head[v] = id++;
}
int main()
{
while ( scanf("%d%d", &n, &m) != EOF && !(!n && !m) ) {
id = 0;
for ( int i = 0; i <= 2*n; ++i ) head[i] = -1;
for ( int i = 2, node, cost; i < n; ++i ) {
scanf("%d%d", &node, &cost);
add( node, node + n, cost );
add( node + n, node, cost );
}
for ( int i = 0; i < m; ++i ) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
if ( u == 1 && v == n ) add( u, v, c ), add( v, u, c );
else if ( u == 1 && v != n ) add( u, v, c ), add( v+n, u, c ) ;
else if ( v == 1 && u == n ) add( v, u, c ), add( u, v, c );
else add( v + n, u, c ), add( u+n, v, c );
}
printf("%d\n", maxflow() );
}
}
相关文章推荐
- UVa 1658 海军上将(最小费用最大流)
- UVa 1660 Cable TV Network (最大流,最小割)
- UVA 10480 Sabotage (网络流,最大流,最小割)
- 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)
- 【动态规划】UVa 1331 最大面积最小三角形剖分
- 最大流最小割 UVA - 10480
- NWERC 2007 / UVa 12124 Assemble (二分搜索&最小值最大问题)
- UVALIVE 3661 Animal Run <最大-最小原理之一 最大流-最小割原理>
- uva 11419(最大匹配,最小点覆盖)
- uva 12124(最小值最大)
- Uva 11248Frequency Hopping 网络流 最大流最小割
- UVA 10099 最大的最小边。。
- NWERC 2007 / UVa 12124 Assemble (二分搜索&最小值最大问题)
- uva11419 【最大二分匹配求最小点覆盖 匈牙利算法】
- 【高效算法设计——最大值最小问题】UVa 714 Coying Books
- UVA 10480 - Sabotage (最小割 /最大流)
- 最大流,最小割(水塘,uva 1515)
- UVa11248 Frequency Hopping(最大流+最小割)
- UVa 1660 电视网络(点连通度+最小割最大流+Dinic)
- uva 1619 单调 求一段序列和 和 其中最小值的乘积的最大值