传送门: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. Ext.grid.Panel表格分页

    转载:http://www.cnblogs.com/libingql/archive/2012/04/22/2464994.html cshtml @{ Layout = null; } <!D ...

  2. Swift - 17 - 数组的初始化

    import UIKit // 声明数组 var array = ["A", "B", "C", "D", " ...

  3. js监听滚动条事件

    (function () { if(document.addEventListener){ document.addEventListener('mousewheel',scrollFunc,fals ...

  4. bc命令详解与实例

    bc: bc 是一种高精度的可交互执行的计算机语言.它在一些浮点数的运算中应用广泛. 一般情况下我们直接输入 bc ,便可进入其工作环境.当然,它还有其他的参数 -h 显示帮助信息并退出 -i 强制进 ...

  5. head First HTML与CSS读书笔记

    调整图片大小 有滚动条的图片可给不了好的用户体验,为了让图片的大小更适合浏览器窗口.这时候就需要对图片的大小进行调整看书之前.我调整图片大小的方式是在<img>元素使用 width 和 h ...

  6. 【jquery学习笔记】关于$(window),$("html,body").scroll()的在不同浏览器的不同反应

    已经很几次碰到了这种问题, 例子: $(window).scroll(function(){ var num=$(window).scrollTop();              //之前的写法是$ ...

  7. FMDB警告Warning: there is at least one open result set around after performing的问题

    FMDB操作sqlite的时候总是报警告Warning: there is at least one open result set around after performing,后来发现是执行查询 ...

  8. JS笔记1

    1.每个函数对象都有一个length属性,表示该函数期望接收的参数个数.它与函数的arguments不同,arguments.length表示函数实际接收的参数个数. 2.javascript 中有五 ...

  9. [转]JS继承的5种实现方式

    参考链接: http://yahaitt.iteye.com/blog/250338 虽说书上都讲过继承的方式方法,但这些东西每看一遍都多少有点新收获,所以单独拿出来放着. 1. 对象冒充 funct ...

  10. C语言基础学习基本数据类型-Char类型

    char类型 char类型用于储存字母和标点之类的字符.但是在技术实现上char却是整数类型.为了处理字符,计算机使用一种数字编码,用特定的整数表示特定的字符.字符变量输入输出用%c符号.定义语法如下 ...