题目:给出一个图,问最少删除多少个点,使得从点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. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]

    3.5 Delete删除用户 删除也是通过ObjectID获得对象进行删除 [Authorize] public async Task<ActionResult> Delete(strin ...

  2. CloudStack 4.2 与CloudStack 4.1二级存储API发生变化

    CloudStack 4.1查看二级存储 http://192.168.150.16:8080/client/api?command=listHosts&response=json&s ...

  3. JQueryMobile页面跳转参数的传递解决方案

    在JQueryMobile开发手机端应用使用可能需要考虑相关的页面跳转带来的参数问题.因为JQueryMobile其实也是HTML5实践的结果.HTML5中有localStorage和sessionS ...

  4. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  5. hdu 5272 Dylans loves numbers 水题

    Dylans loves numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...

  6. JBoss提供的常用的对称加密算法

    package com.test.resteasy; import java.io.File; import java.net.MalformedURLException; import java.n ...

  7. js实现按回车自行提交

    <script type="text/javascript"> document.onkeydown = function (e) { var theEvent = w ...

  8. 初识ASP.NET---若干常见错误

    近期在学习ASP.NET的相关知识,期间遇到了一些错误,比較常见的错误总结了一下,希望此文能给ASP.NET刚開始学习的人一些帮助.同一时候记录这些错误也方便今后自己查看. 1.  GridView& ...

  9. 一个简单的弹出层ProgressBar

    https://github.com/eltld/SimpleLoading

  10. Android蓝牙传感应用

    Android手机一般以客户端的角色主动连接SPP协议设备(接上蓝牙模块的数字传感器),连接流程是: 1.使用registerReceiver注册BroadcastReceiver来获取蓝牙状态.搜索 ...