Destroying the bus stations
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1562 | Accepted: 512 |
Description
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.
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.
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
2
题意:给出一个有向图,1为起点,n为终点,每条边长度为1,问至少要删除多少个点,使得不存在起点到终点长度小于等于k的路径。
思路:这题和poj1815相似,也是拆点,转化为求点不相交的路径的条数。具体做法是
将每个点拆点成i和i‘,连边,容量为1。
然后用floyd求出每两点之间的最短路径,对于原图中的边[u v],若该边在起点到终点长度小于等于k的路径上,则加边u'->v,最后求最小割即可。
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 10005; struct Edge{ int u, v, cap, flow, next; }et[1000000]; int low[maxn], dis[maxn], cnt[maxn], pre[maxn], cur[maxn], eh[maxn]; int G[maxn][maxn]; int s, t, n, m, num, k; void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int cap, int flow){ Edge e = {u, v, cap, flow, eh[u]}; et[num] = e; eh[u] = num++; } void addedge(int u, int v, int cap){ add(u, v, cap, 0); add(v, u, 0, 0); } int isap(int s, int t, int nv){ int u, v, now, flow = 0; memset(low, 0, sizeof(low)); memset(cnt, 0, sizeof(cnt)); memset(dis, 0, sizeof(dis)); for(u = 0; u <= nv; u++) cur[u] = eh[u]; low[s] = INF, cnt[0] = nv, u = s; while(dis[s] < nv) { for(now = cur[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break; if(now != -1) { cur[u] = pre[v] = now; low[v] = min(low[u], et[now].cap - et[now].flow); u = v; if(u == t) { for(; u != s; u = et[pre[u]].u) { et[pre[u]].flow += low[t]; et[pre[u]^1].flow -= low[t]; } flow += low[t]; low[s] = INF; } } else { if(--cnt[dis[u]] == 0) break; dis[u] = nv, cur[u] = eh[u]; for(now = eh[u]; now != -1; now = et[now].next) if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1) dis[u] = dis[et[now].v] + 1; cnt[dis[u]]++; if(u != s) u = et[pre[u]].u; } } return flow; } void floyd(){ for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(G[i][j] > G[i][k] + G[k][j]) G[i][j] = G[i][k] + G[k][j]; } int main() { int a[maxn], b[maxn]; while(scanf("%d%d%d", &n, &m, &k), n || m || k) { init(); s = 0, t = 2 * n + 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { if(i == j) G[i][j] = 0; else G[i][j] = INF; } for(int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); G[a[i]][b[i]] = 1; } floyd(); for(int i = 1; i <= n; i++) { if(i == 1 || i == n) addedge(i, i + n, INF); else addedge(i, i + n, 1); } addedge(s, 1, INF); addedge(2 * n, t, INF); for(int i = 0; i < m; i++) { int u = a[i], v = b[i]; if(G[1][u] + 1 + G[v][n] <= k) addedge(u + n, v, INF); } printf("%d\n", isap(s, t, t + 1)); } return 0; }