题目:给出一个图,问最少删除多少个点,使得从点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 沿着最短路迭代加深搜索的更多相关文章

  1. 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 ...

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

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

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

    Destroying the bus stations                                                                          ...

  4. Destroying the bus stations

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

  5. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  6. [poj 2331] Water pipe ID A*迭代加深搜索(dfs)

    Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...

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

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

  8. HDU 2485 Destroying the bus stations

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

  9. Destroying the bus stations HDU - 2485(最小割点)

    题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa  然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...

随机推荐

  1. linux-用户建立及权限分配

    1.建立用户   useradd –d /usr/test -m test 此命令创建了一个用户test,用户主目录为/usr/test 2.设置用户密码 .修改自己的密码 passwd ,需要输入旧 ...

  2. 用一行代码初始化ArrayList

    方法1: ArrayList<String> arrList1 = (ArrayList<String>) Arrays.asList("Buenos Aires&q ...

  3. java android ExecutorService 线程池解析

    ExecutorService: 它也是一个接口,它扩展自Executor接口,Executor接口更像一个抽象的命令模式,仅有一个方法:execute(runnable);Executor接口简单, ...

  4. CodeForces 163B Lemmings 二分

    Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...

  5. C#操作注册表全攻略

    相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了.这东西对Windows系统来说可是比较重要的,也是病 毒常常会光顾的地方,比如病毒和恶意软件常常会在注册表的启动项 ...

  6. CSS定位规则之BFC 你居然一直不知道的东西!!!!!

    相关文档: http://blog.sina.com.cn/s/blog_877284510101jo5d.html http://www.cnblogs.com/dojo-lzz/p/3999013 ...

  7. Unity3D中使用Leap Motion进行手势控制

    Leap Motion作为一款手势识别设备,相比于Kniect,长处在于准确度. 在我的毕业设计<场景漫游器>的开发中.Leap Motion的手势控制作为重要的一个环节.以此,谈谈开发中 ...

  8. 大礼包!ANDROID内存优化(大汇总)

    写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上把网上搜集的各种内存零散知识点进行汇总.挑选.简化后整理而成. 所以我将本文定义为一个工具类的文章,如果你在A ...

  9. Php-SPL库中的迭代器类详解(转)

    SPL提供了多个迭代器类,分别提供了迭代访问.过滤数据.缓存结果.控制分页等功能.,因为php总是在不断壮大,我尽可能列出SPL中所有的迭代类.下面其中一些迭代器类是需要php5.4,另外一些如Sea ...

  10. NHibernate讲解

    第1章 NHibernate体系结构 总览 对NHibernate体系结构的非常高层的概览: 这幅图展示了NHibernate使用数据库和配置文件数据来为应用程序提供持久化服务(和持久化的对象). 我 ...