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.
 

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.

题目大意:有n(n≤50)个点,起点1到终点n,有m条有向边(m≤4000)。现破坏掉若干起点和终点以外的点,使得从起点到终点经过的边数必须大于k条。问最少要破坏多少个点,保证从起点到终点没有边。

我们先来看一个可以AC但实际上错误的思路o(╯□╰)o(为什么错误还能AC啊?数据弱呗……)

思路:先求每个点到起点和终点的最短路径,然后每个点拆成两个点x、x',如果dis(s,x) + dis(x,t) ≤ k,那么建一条边x→x',容量为1(源点和汇点容量为无穷大)。对每条边(i, j),连一条边i'→j,容量为无穷大。求最小割。根据最大流最小割定理,最大流为答案。因为对于一点x,如果dis(s,x) + dis(x,t) > k,那么没必要破坏点x。那么问题就变成了最少破坏多少个点,使得从1到n必须要经过一个点,经过那个点的话从1到n必然会大于k。

先上代码(15MS):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x3fff3fff; struct SAP {
int head[MAXN], cur[MAXN], pre[MAXN], gap[MAXN], dis[MAXN];
int to[MAXE], cap[MAXE], flow[MAXE], next[MAXE];
int ecnt, n, st, ed; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = ; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cap[ecnt] = ; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
//printf("%d->%d %d\n", u, v, c);
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(ed);
dis[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
++gap[dis[u]];
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(dis[v] > n && cap[p ^ ]) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int Max_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
int ans = , minFlow = INF, u;
for(int i = ; i <= n; ++i) {
cur[i] = head[i];
gap[i] = ;
}
u = pre[st] = st;
bfs();
while(dis[st] < n) {
bool flag = false;
for(int &p = cur[u]; p; p = next[p]) {
int v = to[p];
if(cap[p] > flow[p] && dis[v] + == dis[u]) {
flag = true;
minFlow = min(minFlow, cap[p] - flow[p]);
pre[v] = u;
u = v;
if(u == ed) {
ans += minFlow;
while(u != st) {
u = pre[u];
flow[cur[u]] += minFlow;
flow[cur[u] ^ ] -= minFlow;
}
minFlow = INF;
}
break;
}
}
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(cap[p] > flow[p] && dis[v] < minDis) {
minDis = dis[v];
cur[u] = p;
}
}
if(--gap[dis[u]] == ) break;
gap[dis[u] = minDis + ]++;
u = pre[u];
}
return ans;
}
} G; struct SP {
int head[MAXN], head2[MAXN], dis_st[MAXN], dis_ed[MAXN];
int to[MAXE], next[MAXE], to2[MAXE], next2[MAXE];
int ecnt, n, st, ed; void init(int ss, int tt, int nn) {
memset(head, , sizeof(head));
memset(head2, , sizeof(head2));
ecnt = ;
st = ss; ed = tt; n = nn;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt;
to2[ecnt] = u; next2[ecnt] = head2[v]; head2[v] = ecnt++;
} void make_dis_st() {
memset(dis_st, 0x3f, sizeof(dis_st));
queue<int> que; que.push(st);
dis_st[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(dis_st[v] > n) {
dis_st[v] = dis_st[u] + ;
que.push(v);
}
}
}
} void make_dis_ed() {
memset(dis_ed, 0x3f, sizeof(dis_ed));
queue<int> que; que.push(ed);
dis_ed[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head2[u]; p; p = next2[p]) {
int v = to2[p];
if(dis_ed[v] > n) {
dis_ed[v] = dis_ed[u] + ;
que.push(v);
}
}
}
} void make_G(int k) {
make_dis_st();
//for(int i = 1; i <= n; ++i) printf("%d ", dis_st[i]);
make_dis_ed();
//for(int i = 1; i <= n; ++i) printf("%d ", dis_ed[i]);
G.init();
G.add_edge(, + n, INF);
G.add_edge(n, n + n, INF);
for(int i = ; i < n; ++i)
if(dis_st[i] + dis_ed[i] <= k) G.add_edge(i, i + n, );
for(int u = ; u <= n; ++u) {
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
G.add_edge(u + n, v, INF);
}
}
}
} T; int n, m, k, a, b; int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
if(n == && m == && k == ) break;
T.init(, n, n);
while(m--) {
scanf("%d%d", &a, &b);
T.add_edge(a, b);
}
T.make_G(k);
printf("%d\n", G.Max_flow(, n + n, n + n));
}
}

但是这样做是错的,为什么呢?我们来看一个Discuss里的数据:

8 10 5
1 2
2 3
3 4
4 5
5 6
6 8
1 7
7 8
4 7
7 4
这个数据输出应该是1(破坏点7),但是上面的代码会输出2。因为上面的思路忽略了一点:当我们破坏掉某个点的时候,经过另一些从起点到终点的距离可能会变化以至于大于k。

那么怎么办呢?我们只能退而求其次o(╯□╰)o,虽然这也是一个能AC但是错的思路

思路:每个点拆成两个点x、x'(还是拆点╮(╯▽╰)╭),然后建一条边x→x',容量为1(源点和汇点为无穷大),费用为0。然后对每条边(i, j)建一条边,容量为无穷大,费用为1。那么不断增广直到费用大于k时停止增广,这是流量就是答案(还是求流╮(╯▽╰)╭)。

上代码(46MS):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x3f3f3f3f;//can't modify int n, m, k, a, b; struct MinCostFlow {
bool vis[MAXN];
int head[MAXN], dis[MAXN], pre[MAXN];
int to[MAXE], next[MAXE], cost[MAXE], flow[MAXE];
int n, st, ed, ecnt; void init(int ss, int tt, int nn) {
memset(head, , sizeof(head));
ecnt = ;
st = ss, ed = tt, n = nn;
} void add_edge(int u, int v, int c, int f) {
to[ecnt] = v; cost[ecnt] = f; flow[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = -f; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} bool spfa() {
memset(vis, , sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(st);
vis[st] = true; dis[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = false;
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(flow[p] && dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
pre[v] = p;
if(!vis[v]) {
vis[v] = true;
que.push(v);
}
}
}
}
return dis[ed] <= k;
} void min_cost_flow(int &minFlow, int &fee) {
minFlow = fee = ;
while(spfa()) {
fee += dis[ed];
int u = ed, tmp = INF;
while(u != st) {
tmp = min(tmp, flow[pre[u]]);
u = to[pre[u] ^ ];
}
u = ed;
while(u != st) {
flow[pre[u]] -= tmp;
flow[pre[u] ^ ] += tmp;
u = to[pre[u] ^ ];
}
minFlow += tmp;
}
} int mincost() {
int ret, tmp;
min_cost_flow(tmp, ret);
return ret;
} int maxflow() {
int ret, tmp;
min_cost_flow(ret, tmp);
return ret;
}
} G; int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
if(n == && m == && k == ) break;
G.init(, n * , n * );
G.add_edge(, + n, INF, );
G.add_edge(n, n + n, INF, );
for(int i = ; i < n; ++i) G.add_edge(i, i + n, , );
while(m--) {
scanf("%d%d", &a, &b);
G.add_edge(a + n, b, INF, );
}
printf("%d\n", G.maxflow());
}
}

10 11 5

1 2
2 3
3 4
4 5
5 10
2 9
1 6
6 7
7 8
8 9
9 10

然后上面的代码就过不了这组数据o(╯□╰)o,代码输出1,正确输出为2,怪不得我想不明白为什么是对的o(╯□╰)o(这数据敢不敢再弱一点……)

最后我们只能再退而求其次了o(╯□╰)o,暴力枚举答案,然后枚举最短路径上的点,深搜,再枚举删点后最短路径上的点,再深搜……

搜索(484MS,慢了点但起码是对了╮(╯▽╰)╭):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x3fff3fff; int n, m, k, a, b, ans, ecnt;
int SP[MAXN][MAXN];
int head[MAXN], dis[MAXN], pre[MAXN];
int to[MAXE], next[MAXE];
bool del[MAXN]; void init() {
memset(head, , sizeof(head));
memset(del, , sizeof(del));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
} bool bfs() {
memset(dis, 0x3f, sizeof(head));
queue<int> que; que.push();
dis[] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(!del[v] && dis[v] > n) {
dis[v] = dis[u] + ;
pre[v] = u;
if(v == n) return dis[n] <= k;
que.push(v);
}
}
}
return false;
} bool flag; void dfs(int dep) {
if(!bfs()) {
flag = true;
return ;
}
if(dep > ans) return ;
int u = pre[n], cnt = ;
while(u != ) {
SP[dep][cnt++] = u;
u = pre[u];
}
for(int i = cnt - ; i >= ; --i) {
del[SP[dep][i]] = true;
dfs(dep + );
del[SP[dep][i]] = false;
}
} int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
if(n == && m == && k == ) break;
init();
while(m--) {
scanf("%d%d", &a, &b);
add_edge(a, b);
}
flag = false;
for(ans = ; ans < n; ++ans) {
dfs();
if(flag) break;
}
printf("%d\n", ans);
}
}

HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)的更多相关文章

  1. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

  2. HDU 2485 Destroying the bus stations(费用流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...

  3. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...

  4. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  5. HDU 2485 Destroying the bus stations

    2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...

  6. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

  7. Destroying the bus stations

    Destroying the bus stations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1832   Acce ...

  8. 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...

  9. 2018.10.13 bzoj1834: [ZJOI2010]network 网络扩容(最大流+费用流)

    传送门 网络流水题啊. 第一问直接放心跑最大流(本来还以为有什么tricktricktrick). 第二问就直接把原来的边(u,v,c,w)(u,v,c,w)(u,v,c,w)变成(u,v,c,0)( ...

随机推荐

  1. MySql is marked as crashed and should be repaired问题

    在一次电脑不知道为什么重启之后数据库某表出现了 is marked as crashed and should be repaired这个错误,百度了一下,很多都是去找什么工具然后输入命令之类的,因为 ...

  2. 个人免签即时到账收款接口 bufpay.com 支持多账号收款

    有很多 bufpay 的用户反馈,单个手机收款有些时候不太方便,切换手机太麻烦:或者是营业额比较多,希望分摊到多个账号上面. 基于以上的问题,bufpay 开发了多手机收款的功能:每个收款的手机安装 ...

  3. activemq整合springboot使用(个人微信小程序用)

    1.引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  4. MySQL学习之视图的使用

    视图基本操作 创建视图 视图的本质就是SQL指令(select语句) 基本语法:create view 视图名 as  select 指令; 在这里的select指令可以是单表数据,也可以是连接查询. ...

  5. Ldap实现AD域认证

    1.java Ldap基础类 package com.common; import java.io.FileInputStream; import java.io.IOException; impor ...

  6. js判断当前浏览器是否是源生app的webview

    有些时候,我们在开发过程中需要判断,当前页面被打开是否是处于源生的webview里面,或者NODEJS做服务器后端支持的时候,判断请求来源是否来至于源生webview里面被打开的页面请求GET/POS ...

  7. 解决 LLVM 错误 cannot specify -o when generating multiple output files

    Xcode 9 使用 LLVM 混淆器会提示错误: clang: error: cannot specify -o when generating multiple output files 通过对比 ...

  8. spark源码编译记录

    spark在项目中已经用了一段时间了,趁现在空闲,下个源码编译在IDEA里面阅读下,特此记录过程. 前提已经安装maven和git 1.上官网下载源码的包: 2.然后解压到一个文件夹 3.编译,编译的 ...

  9. java中stream部分笔记

    Stream流表面上看起来与集合类似,允许你转换和检索数据.然而,两者却有显著的不同1.流不存储元素.它们存储在底层的集合或者按需生成2.流操作不改变他们的源数据.例如filter方法不会从一个新流中 ...

  10. 成都Uber优步司机奖励政策(3月23日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...