传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485

题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1,n两个点。

题解:这个题目可以用最小流解决,也可以用IDA*  +  BFS解决。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; #define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++) typedef long long LL;
const int N=1005;
const int M=105;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7; int n,m,k;
int lin[N][N];//邻接表储存
int fa[N];//记录从1到n最短路径
bool vis[N];//标记节点是否被删除
int ans,Min;
bool flag; struct xh
{
int x,y,next;
}edge[N]; int bfs()
{
int q[2222];
int he=0,ta=0;
mset(fa,-1);
fa[1]=0;
mset(q,0);
q[ta++]=1;
while(he!=ta)
{
int x=q[he++];
for(int i=1;i<=lin[x][0];i++)
{
int v=lin[x][i];
if(fa[v]==-1&&!vis[v])
{
fa[v]=x;
q[ta++]=v;
if(n==v) return 1;
}
}
}
return 0;
} void dfs(int dian,int depth)//求去掉最少个数点
{
if(flag==false) return ;
if(dian>depth) return ; int p=bfs();
int sa[N]={0},t=0;
if(p==0){flag=false;return;}
for(int i=n;i!=1;i=fa[i])
{
sa[t++]=i;
if(t>k){flag=false;return ;}
} for(int i=1;i<t;i++)
{
int x=sa[i];
if(vis[x]) continue;
vis[x]=1;
dfs(dian+1,depth);
vis[x]=0;
if(flag==false) return ;
}
} void IDA()
{
if(n<=2)
{
puts("0");
return ;
}
int depth=0;
flag=true;
vis[1]=1;
while(flag)
{
dfs(0,depth);
if(!flag) break;
depth++;
}
pi1(depth);
} int main()
{
// freopen("input.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k))
{
mset(lin,0);
forb(i,0,m)
{
int a,b,t;
si2(a,b);
t=++lin[a][0];
lin[a][t]=b;
//这个地方写双向的就WA了,单向的就A了
}
IDA();
}
return 0;
}

超时代码:(IDA*  +  DFS)个人感觉应该差不多,但是就是超时了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; #define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++) typedef long long LL;
const int N=1005;
const int M=105;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7; int n,m,k;
int lin[N][N];//邻接表储存
int cnt[M];//记录从1到n最短路径
bool vis[N];//标记节点是否被删除
int ans,Min;
bool flag; struct xh
{
int x,y,next;
}edge[N]; void dfs2(int k,int de)//求最短距离
{
cnt[de]=k;//路径
if(k==n)
{
Min=min(Min,de);
return ;
} for(int i=1;i<=lin[k][0];i++)
{
int t=lin[k][i];
if(vis[t]) continue;
vis[t]=1;
dfs2(t,de+1);
vis[t]=0;
}
} void dfs1(int dian,int depth)//求去掉最少个数点
{
if(flag==false) return ;
Min=INF;
mset(cnt,0);
dfs2(1,0);
// cout<<"最短距离"<<Min<<endl;
// cout<<"路径:";
// if(Min!=INF)
// {
// for(int i=0;i<=Min;i++)
// printf("%d ",cnt[i]);
// cout<<endl;
// }
// system("pause"); int xh=Min;
if(xh>=k)
{
flag=false;
ans=min(ans,dian);
return ;
}
if(dian>=depth)
return ; int sa[N];
for(int i=0;i<=xh;i++)
sa[i]=cnt[i];
for(int i=1;i<xh;i++)
{
int t=sa[i];
if(vis[t]) continue;
vis[t]=1;
dfs1(dian+1,depth);
vis[t]=0;
if(flag==false) return ;
}
} int main()
{
// freopen("input.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k))
{
mset(lin,0);
forb(i,0,m)
{
int a,b,t;
si2(a,b);
t=++lin[a][0];
lin[a][t]=b;
t=++lin[b][0];
lin[b][t]=a;
}
ans=INF;
mset(vis,0);
vis[1]=1;
flag=true;
int depth=-1;
while(flag)
{
depth++;
dfs1(0,depth);
} pi1(depth);
}
return 0;
}

HDU 2485 Destroying the bus stations (IDA*+ BFS)的更多相关文章

  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. HDU 2485 Destroying the bus stations(费用流)

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

  4. HDU 2485 Destroying the bus stations

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

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

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

  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. Destroying the bus stations HDU - 2485(最小割点)

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

  9. POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索

    题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...

随机推荐

  1. Wpf Binding.Path设置

    Binding.Path 获取或设置绑定源属性的路径. 每个绑定通常都具有四个组件:绑定目标对象.目标属性.绑定源,以及要使用的绑定源值的路径.有关这些数据绑定概念的更多信息,请参见数据绑定概述. 使 ...

  2. 关于android MTK相机L版本,切换屏幕比例后,分辨率随之改变,但重新进入相机后原有分辨率不再生效问题

    BUG详细:比如4:3的时候是200W,切成全屏变400W,重新切回4:3为300W,退出相机后,重新进入又变成200W. 原因分析:这个版本的设计如此,当你点选屏幕比例的时候,程序设计是把这个比例值 ...

  3. 说说RMAN里的obsolete

    RMAN> report obsolete; RMAN retention policy will be applied to the commandRMAN retention policy ...

  4. QQ情侣头像~

                       

  5. 再谈CMake与RPATH

    之前写过一篇<探讨CMake中关于RPATH的使用>,只要针对的方面是在编译生成之后(不包括安装的make install)如何去除RPATH的问题.今天给大家主要介绍一下如何让CMake ...

  6. [C#] 用一种更优美的方式来替换掉又多又长的switch-case代码段

    switch-case语句是我们编码过程中常用的一种分支语句.然而正所谓成也萧何败萧何,每当我们向一个已经拥有了成百上千行的switch-case代码段中添加新的case分支的时候,我们是否有过为代码 ...

  7. js 实现 C# 的 format 方法

    2014-11-08 12:18:51 更新,修复原形链方法被当作关键词的bug,其实之前是想用全局关键词的,不过还是算了,array里有太多单词了.                          ...

  8. powerpoint取色器有什么用|ppt取色器使用教程

    在使用powerpoint过程中常常发现一些功能我们很少用到,其实是自己不会用的原因,关于powerpoint取色器有什么用呢?接下来我们一起来学一下ppt取色器使用教程. powerpoint取色器 ...

  9. 常用排序算法之——快速排序(C语言+VC6.0平台)

    经典排序算法中快速排序具有较好的效率,但其实现思路相对较难理解. #include<stdio.h> int partition(int num[],int low,int high) / ...

  10. 转:MongoDB调查总结

    与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问速度:举例来说,在传统的关系型数据库中,一个COUNT类型的操作会锁定数据集,这样可以保证得到“当前”情况下的精确值 ...