poj3204Ikki's Story I - Road Reconstruction
这题只是最大流的一个模板题目。
题目要求怎样修改道路才会增加最大流。
思路就是在残余的网络流里面在某一个零边增加一个单位的网络流。
查看是否有到首都的道路。
View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#define inf 0x7ffffff
#define maxn 505
int map[maxn][maxn], visit[maxn], e[maxn][maxn];
int f[maxn][maxn] , pre[maxn], prev[maxn];
int queue[maxn * 2];
int n, m;
inline int min(int a, int b)
{
return a > b? b:a;
}
void init()
{
memset(map, 0, sizeof(map));
memset(e, 0, sizeof(e));
memset(f, 0, sizeof(f));
int x, y, val;
for (int i = 1; i <= m;i ++)
{
scanf("%d%d%d", &x, &y, &val);
x ++; y ++;
map[x][0] ++;
map[x][map[x][0]] = y;
e[x][ y] = val;
}
return;
}
bool flow()
{
int front = 1, rear = 0;
pre[1] = 1;
prev[1] = inf;
queue[front] = 1;
memset(visit, 0, sizeof(visit));
visit[1] = 1;
while (front != rear)
{
rear ++;
int x = queue[rear];
for (int i = 1; i <= map[x][0]; i ++)
{
int start = map[x][i];
if (visit[start] == 0)
{
if (e[x][start] > f[x][start])
{
pre[start] = x;
prev[start] = min(e[x][start] - f[x][start], prev[x]);
visit[start] = 1;
front ++;
queue[front] = start;
if (start == n)
{
return true;
}
}
else if (f[start][x] > 0)
{
pre[start] = -x;
prev[start] = min(prev[x], f[start][x]);
visit[start] = 1;
front ++;
queue[front] = start;
if (start == n)
{
return true;
}
}
}
}
}
return false;
}
void update()
{
int x = n;
while (x != 1)
{
int y = x;
x = pre[x];
if (x < 0)
{
x = -x;
f[y][x] -= prev[n];
}
else
{
f[x][y] += prev[n];
}
}
return;
}
void funs()
{
int num = 0;
while (flow()) update();
for (int i = 1; i < n; i ++)
{
for (int j = 1; j <= map[i][0]; j ++)
{
int x = map[i][j];
if (e[i][x] == f[i][x])
{
e[i][x] ++;
if (flow()) num ++;
e[i][x] --;
}
}
}
printf("%d\n", num);
return;
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
init();
funs();
}
return 0;
}