题目:给出一个图,问最少删除多少个点,使得从点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. Flex编程注意之直接获取某个组件的对象(this[]用法)通过id获取控件

    有这样一个需求:假如你new了一百次Button,同时这些button的id分别赋值如btn1.id = "button1"; btn2.id = "button2&qu ...

  2. Windows Server Backup 备份Hypver-V VM

    在Windows Server 2012中,可以通过Windows Server Backup备份Hypver-V VM.在还原时,将会还原到Hypver-V管理器中. 设置只保存2个备份副本,命令如 ...

  3. Mysql的存储过程(以Mysql为例进行讲解)

       我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储 在数据库中,用户通过指定存 ...

  4. 记一次Time-Wait导致的问题

    去年(2014年)公司决定服务框架改用Finagle(后续文章详细介绍),but 公司业务系统大部分是C#写的,然后 finagle只提供了 scala/java 的Client 于是 只能自己动手丰 ...

  5. JavaScript中伪协议 javascript:研究

    将javascript代码添加到客户端的方法是把它放置在伪协议说明符javascript:后的URL中.这个特殊的协议类型声明了URL的主体是任意的javascript代码,它由javascript的 ...

  6. 关于Javascript的内存泄漏问题的整理稿

    写了好长时间javascript小功能模块,从来没有关注过内存泄漏问题.记得以前写C++程序的时候,内存泄漏是个严重的问题,我想是时候关注一下了.网上找了篇文章,Mark一下.原文地址:http:// ...

  7. 第七届ACM趣味程序设计竞赛第四场(正式赛) 题解

    Final Pan's prime numbers 题目连接: http://acm.uestc.edu.cn/#/problem/show/1272 题意 给你n,要求你在[4,n]范围内找到一个最 ...

  8. Android游戏开发:物理游戏之重力系统开发--圆形自由落体Demo

    本节为大家提供有关物理游戏的知识,讲解了一个简单的圆形自由落体Demo的编写.. Java代码 package com.himi; import java.util.Random; import ja ...

  9. box-flex等分总结

    首先要知道在应用 box-flex 时必须给父容器定义 css 属性 display:box 其子容器才可以进行划分. .box{ display: -webkit-box; display: -mo ...

  10. cocos2d-x android 字体的设置

    我们知道 ios 自带的字体 和 android 自带的字体不同 为了使我们开发的游戏中的字体统一 我们就需要自己的字体(包括从mac 拷贝出来的 字体) 从 mac 中 copy 出 Thonbur ...