POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索
题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个。
分析:
上网搜了一下,发现很多人用网络流做的,发现我不会。再后来看到这篇说网络流的做法是错的,囧。
后来发现点数有点少,直接上爆搜。每次搜索前先跑一遍最短路,判断是否满足,如果满足更新答案,退出。
否则,在求最短路时记录一下路径,然后枚举删除最短路上的点,继续搜。
第一次写时,记录路径用的是全局变量,每次搜下一层的时候就会变,发现居然也A了。。。
我试了一下,发现n不会等于1。。。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ char op;
inline void Int(int &x){
while(!isdigit(op=getchar()));
x = op-'0';
while(isdigit(op=getchar()))
x = x*10+op-'0';
} const int MAXN = 55;
const int MAXM = 4010;
const int INF = 1e9; int g[MAXN][MAXN];
int dis[MAXN],n,m,k;
bool bad[MAXN],use[MAXN];
int pre[MAXN]; bool dijkstra(){
rep1(i,n)
dis[i] = INF;
Clear(use);
dis[1] = 0;
int now , MIN;
pre[1] = 0;
rep(q,n){
MIN = INF;
rep1(j,n)
if(!bad[j]&&!use[j]&&dis[j]<MIN)
MIN = dis[now=j];
if(MIN==INF)
break;
use[now] = true;
rep1(j,n)
if(!bad[j]&&!use[j]){
if(dis[j]>dis[now]+g[now][j]){
pre[j] = now;
dis[j] = dis[now]+g[now][j];
}
}
}
return dis[n]>k;
} int ans;
void dfs(int res,int sum){
if(sum>=ans)return;
if( dijkstra() ){
ans = sum;
return;
}
if(!res)
return; int path[MAXN]; // 得要在每次搜索时记录
copy(pre,pre+n+1,path); int x = n;
while(path[x]){
x = path[x];
if(x==1)continue;
bad[x] = true;
dfs(res-1,sum+1);
bad[x] = false;
}
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int x,y;
while(true){
Int(n);Int(m);Int(k);
if(!n&&!m&&!k)break;
rep1(i,n)
rep1(j,n)
g[i][j] = INF;
while(m--){
Int(x);Int(y);
g[x][y] = 1;
} Clear(bad);
ans = INF;
dfs(n-1,0);
printf("%d\n",ans);
} return 0;
}
POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索的更多相关文章
- HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)
Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...
- 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
- HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations ...
- Destroying the bus stations
Destroying the bus stations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1832 Acce ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- [poj 2331] Water pipe ID A*迭代加深搜索(dfs)
Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...
- HDU 2485 Destroying the bus stations (IDA*+ BFS)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...
- HDU 2485 Destroying the bus stations
2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...
- Destroying the bus stations HDU - 2485(最小割点)
题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa 然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...
随机推荐
- POJ2503——Babelfish
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- canvas加载gif
http://ernestdelgado.com/public-tests/gifoncanvas/ <!DOCTYPE html><html><head> < ...
- js 判断pc与手机
var u = navigator.userAgent; if ((u.indexOf('Mac') > -1 || u.indexOf('Windows') > -1) &&am ...
- Navicat 导入数据报错 --- 1153 - Got a packet bigger than 'max_allowed_packet' bytes
在用Navicat导入SQL文件时报错:MySql 错误 Err [Imp] 1153 - Got a packet bigger than 'max_allowed_packet' bytes 查了 ...
- mongodb - 命令行增删改查
# insert db.person.insert({ }) db.person.insert({ }) db.person.insert({ }) db.person.insert({ }) # s ...
- HDU 5590 ZYB's Biology 水题
ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力
B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- (高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1----->10进制----->ba2)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class POJ_1220_ ...
- 最长公共子序列(LCS问题)
先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...