1. 程式人生 > >hdu 2485 Destroying the bus stations (最小割=最大流)

hdu 2485 Destroying the bus stations (最小割=最大流)

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2485

Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station.
No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station.


Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.

 

 

Input

There are several test cases. Input ends with three zeros.

For each test case:

The first line contains 3 integers, n, m and k. (0< n <=50, 0< m<=4000, 0 < k < 1000)
Then m lines follows. Each line contains 2 integers, s and f, indicating that there is a road from station No. s to station No. f.

 

 

Output

For each test case, output the minimum number of stations Gabiluso must destroy.

 

 

Sample Input

 

5 7 3 1 3 3 4 4 5 1 2 2 5 1 4 4 5 0 0 0

 

 

Sample Output

 

題意:

       給你一個n個節點m條邊的有向圖,從1號節點到n號節點沒有直接的邊,然後問你最少需要刪除幾個節點,才能使得該有向圖沒有從1號節點到n號節點長度<=k的路.(當然1號節點和n號節點是不可以刪除的)

最小割=最大流。

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 110;
int tdis[maxn], wdis[maxn], vis[maxn];
int map[maxn][maxn], dis[maxn], cap[maxn][maxn],pre[maxn];
int mp[maxn][maxn];
int m, k, n;
void bfs(int st)
{
	queue<int>que;
	memset(dis, inf, sizeof(dis));
	que.push(st);
	dis[st] = 0;
	while (!que.empty())
	{
		int u = que.front();
		que.pop();
		for (int i = 1; i <= n; i++)
		{
			if (map[u][i] && dis[u] + 1 < dis[i])
			{
				dis[i] = dis[u] + 1;
				que.push(i);
			}
		}
	}
}
int maxflow(int st,int end)
{
	int res, _min;
	res = 0;
	while (1)
	{
		queue<int>pq;
		memset(vis, 0, sizeof(vis));
		pq.push(st);
		vis[st] = 1;
		while (!pq.empty())
		{
			int u = pq.front();
			pq.pop();
			if (vis[end])
			{
				break;
			}
			for (int i = st; i <= end; i++)
			{
				if (!vis[i] && cap[u][i] > 0)
				{
					vis[i] = 1;
					pre[i] = u;
					pq.push(i);
				}
			}
		}
		if (!vis[end])
		{
			break;
		}
		_min = inf;
		for (int i = end; i != st; i = pre[i])
		{
			if (cap[pre[i]][i] < _min)
			{
				_min = cap[pre[i]][i];
			}
		}
		res += _min;
		for (int i = end; i != st; i = pre[i])
		{
			cap[pre[i]][i] -= _min;
			cap[i][pre[i]] += _min;
		}
	}
	return res;
}

int main()
{
	//freopen("C://input.txt", "r", stdin);
	while (scanf("%d%d%d", &n, &m, &k) != EOF)
	{
		if (n == 0 && m == 0 && k == 0)
		{
			break;
		}
		memset(mp, 0, sizeof(mp));
		memset(map, 0, sizeof(map));
		memset(cap, 0, sizeof(cap));
		for (int i = 1; i <= m; i++)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			mp[u][v] = 1;
			map[u][v] = map[v][u] = 1;
		}
		bfs(1);
		for (int i = 1; i <= n; i++)
		{
			tdis[i] = dis[i];
		}
		bfs(n);
		for (int i = 1; i <= n; i++)
		{
			wdis[i] = dis[i];
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (mp[i][j])
				{
					cap[i+n][j] = inf;
				}
			}
			if (tdis[i] + wdis[i] <= k)
			{
				cap[i][i + n] = 1;
			}
		}
		cap[1][1 + n] = inf;
		cap[n][n + n] = inf;
		printf("%d\n", maxflow(1, n + n));
	}
	return 0;
}