POJ3204-Ikki's Story I - Road Reconstruction

Ikki's Story I - Road Reconstruction
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 8020 Accepted: 2315

Description

Ikki is the king of a small country – Phoenix, Phoenix is so small that there is only one city that is responsible for the production of daily goods, and uses the road network to transport the goods to the capital. Ikki finds that the biggest problem in the country is that transportation speed is too slow.

Since Ikki was an ACM/ICPC contestant before, he realized that this, indeed, is a maximum flow problem. He coded a maximum flow program and found the answer. Not satisfied with the current status of the transportation speed, he wants to increase the transportation ability of the nation. The method is relatively simple, Ikki will reconstruct some roads in this transportation network, to make those roads afford higher capacity in transportation. But unfortunately, the country of Phoenix is not so rich in GDP that there is only enough money to rebuild one road. Ikki wants to find such roads that if reconstructed, the total capacity of transportation will increase.

He thought this problem for a loooong time but cannot get it. So he gave this problem to frkstyc, who put it in this POJ Monthly contest for you to solve. Can you solve it for Ikki?

Input

The input contains exactly one test case.

The first line of the test case contains two integers NM (N ≤ 500, M ≤ 5,000) which represents the number of cities and roads in the country, Phoenix, respectively.

M lines follow, each line contains three integers abc, which means that there is a road from city a to city b with a transportation capacity of c (0 ≤ ab < nc ≤ 100). All the roads are directed.

Cities are numbered from 0 to n − 1, the city which can product goods is numbered 0, and the capital is numbered n − 1.

Output

You should output one line consisting of only one integer  K, denoting that there are  K roads, reconstructing each of which will increase the network transportation capacity.

Sample Input

2 1
0 1 1

Sample Output

1

Source



题意:给出一个有向图,找这样一种边的个数,就是增加该边的容量,可以使得最大流变大。

解题思路:

有两种做法:

1.先跑一遍最大流,得到残流图,然后枚举每一条每一条边,若该条边是要求的边,首先他应该是满流边,即残流为0,然后可以将这条边残流加1,再用bfs判能否找到增广路,如果可以,那么就是要求的边。

2.可以知道如果一条边是满流边,而且这条边的两个端点都能分别找到到起点和终点的非满流路径,那么这条边即为所求边。所以先跑一遍最大流,得到残流图, 然后分别从起点和终点沿着非满流边进行bfs,看每个点能否到达起点或终点,然后枚举每一条边,首先要求这条边是满流边,然后边的起点要能到达起点,边的终点要能到达终点,如果可以,那么这条边就是所求的边。


方法一:

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;
#define MAXN 509  

struct node
{
	int u, v, next,cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN],vis[MAXN];
int cnt;

void init()
{
	cnt = 0;
	memset(s, -1, sizeof(s));
}

void add(int u, int v, int c)
{
	edge[cnt].u = u;
	edge[cnt].v = v;
	edge[cnt].cap = c;
	edge[cnt].next = s[u];
	s[u] = cnt++;
	edge[cnt].u = v;
	edge[cnt].v = u;
	edge[cnt].cap = 0;
	edge[cnt].next = s[v];
	s[v] = cnt++;
}

bool BFS(int ss, int ee)
{
	memset(d, 0, sizeof d);
	d[ss] = 1;
	queue<int>q;
	q.push(ss);
	while (!q.empty())
	{
		int pre = q.front();
		q.pop();
		for (int i = s[pre]; ~i; i = edge[i].next)
		{
			int v = edge[i].v;
			if (edge[i].cap > 0 && !d[v])
			{
				d[v] = d[pre] + 1;
				q.push(v);
			}
		}
	}
	return d[ee];
}

int DFS(int x, int exp, int ee)
{
	if (x == ee || !exp) return exp;
	int temp, flow = 0;
	for (int i = nt[x]; ~i; i = edge[i].next, nt[x] = i)
	{
		int v = edge[i].v;
		if (d[v] == d[x] + 1 && (temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
		{
			edge[i].cap -= temp;
			edge[i ^ 1].cap += temp;
			flow += temp;
			exp -= temp;
			if (!exp) break;
		}
	}
	if (!flow) d[x] = 0;
	return flow;
}

void Dinic_flow(int ss, int ee)
{
	int ans = 0;
	while (BFS(ss, ee))
	{
		for (int i = 0; i <= ee; i++) nt[i] = s[i];
		ans += DFS(ss, INF, ee);
	}
}

int main()
{
	int n, m,w;
	while (~scanf("%d %d", &n, &m))
	{
		init();
		int u,v,w;
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			add(u, v, w);
		}
		Dinic_flow(0,n-1);
		int ans = 0;
		for (int i = 0; i < cnt; i+=2)
		{
			if (!edge[i].cap)
			{
				edge[i].cap++;
				if (BFS(0, n - 1)) ans++;
				edge[i].cap--;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


方法二:


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;
#define MAXN 509  

struct node
{
	int u, v, next,cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN],vis[MAXN];
int cnt;
vector<int>g1[MAXN], g2[MAXN];

void init()
{
	cnt = 0;
	memset(s, -1, sizeof(s));
}

void add(int u, int v, int c)
{
	edge[cnt].u = u;
	edge[cnt].v = v;
	edge[cnt].cap = c;
	edge[cnt].next = s[u];
	s[u] = cnt++;
	edge[cnt].u = v;
	edge[cnt].v = u;
	edge[cnt].cap = 0;
	edge[cnt].next = s[v];
	s[v] = cnt++;
}

bool BFS(int ss, int ee)
{
	memset(d, 0, sizeof d);
	d[ss] = 1;
	queue<int>q;
	q.push(ss);
	while (!q.empty())
	{
		int pre = q.front();
		q.pop();
		for (int i = s[pre]; ~i; i = edge[i].next)
		{
			int v = edge[i].v;
			if (edge[i].cap > 0 && !d[v])
			{
				d[v] = d[pre] + 1;
				q.push(v);
			}
		}
	}
	return d[ee];
}

int DFS(int x, int exp, int ee)
{
	if (x == ee || !exp) return exp;
	int temp, flow = 0;
	for (int i = nt[x]; ~i; i = edge[i].next, nt[x] = i)
	{
		int v = edge[i].v;
		if (d[v] == d[x] + 1 && (temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
		{
			edge[i].cap -= temp;
			edge[i ^ 1].cap += temp;
			flow += temp;
			exp -= temp;
			if (!exp) break;
		}
	}
	if (!flow) d[x] = 0;
	return flow;
}

void Dinic_flow(int ss, int ee)
{
	int ans = 0;
	while (BFS(ss, ee))
	{
		for (int i = 0; i <= ee; i++) nt[i] = s[i];
		ans += DFS(ss, INF, ee);
	}
}

void dfs1(int k)
{
	vis[k] = 1;
	int Size = g1[k].size();
	for (int i = 0; i<Size; i ++)
		if (!vis[g1[k][i]]) dfs1(g1[k][i]);
}

void dfs2(int k)
{
	vis[k] = 2;
	int Size = g2[k].size();
	for (int i = 0; i<Size; i++)
		if (!vis[g2[k][i]]) dfs2(g2[k][i]);
}

int main()
{
	int n, m,w;
	while (~scanf("%d %d", &n, &m))
	{
		init();
		int u,v,w;
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			add(u, v, w);
		}
		Dinic_flow(0,n-1);
		for (int i = 0; i < n; i++) g1[i].clear(), g2[i].clear();
		queue<int>q;
		int ans = 0;
		for (int i = 0; i < cnt; i+=2)
		{
			if (edge[i].cap)
			{
				g1[edge[i].u].push_back(edge[i].v);
				g2[edge[i].v].push_back(edge[i].u);
			}
			else q.push(i);
		}
		dfs1(0), dfs2(n - 1);
		while (!q.empty())
		{
			int pre = q.front();
			q.pop();
			if (vis[edge[pre].u] == 1 && vis[edge[pre].v] == 2) ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值