传送门: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. 使用ul,添加新闻信息列表

    在浏览网页时,你会发现网页上有很多信息的列表,如新闻列表.图片列表,如下图所示. 新闻列表 图片列表 这些列表就可以使用ul-li标签来完成.ul-li是没有前后顺序的信息列表. 语法: <ul ...

  2. 31.Spring-开发流程.md

    [toc] 1.简单开发流程 1.1引用类库 基本类库: ## 1.2创建spring配置文件,文件的名称为固定格式:applicationContext.xml或者bean.xml: <?xm ...

  3. hibernate_validator_07

    一.校验组序列 默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的. 就拿我们上一个例子来说,我们可以这样:首先在我们检查车 ...

  4. [500lines]500行代码写web server

    项目地址:https://github.com/aosabook/500lines/tree/master/web-server.作者是来自Mozilla的Greg Wilson.项目是用py2写成. ...

  5. 【COGS1672】难存的情缘

    [题目描述] 一天机房的夜晚,无数人在MC里奋斗着... 大家都知道矿产对于MC来说是多么的重要,但由于矿越挖越少,勇士们不得不跑到更远的地方挖矿,但这样路途上就会花费相当大的时间,导致挖矿效率底下. ...

  6. openstack VM可以ping外部网络,但是外部网络ping不通VM

    经过无数次的尝试,终于搭建好了完整的Openstack,本来VM可以获取到IP地址,但是等到我大功告成的时候,突然发现外部网络却不能ping进VM,我可是整整折腾了我几个通宵,这是哭啊.然而,皇天不负 ...

  7. Qt信号槽连接在有默认形参下的情况思考

    写下这个给自己备忘,比如函数 ) 你在调用端如论是test(3)或者test(),都可以正确调用到这个函数. 但是,如果放到Qt中的信号槽的话,这个还是值得讲一讲的,不然的话,可能会引起相应的误会. ...

  8. curl 解析

    获得页面 使用命令:curl http://curl.haxx. se 这是最简单的使用方法.用这个命令获得了http://curl.haxx. se指向的页面,同样,如果这里的URL指向的是一个文件 ...

  9. Ubuntu完全教程,让你成为Ubuntu高手!

    Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音.了解发音是有意义的,您不是第一个为此困惑的人,当然,也不会是最后一个:) 大多数的美国人读 ubun ...

  10. 转:快乐Node码农的十个习惯

    文章来源于:http://www.infoq.com/cn/articles/node.js-habits 从问世到现在将近20年,JavaScript一直缺乏其它有吸引力的编程语言,比如Python ...