HDU 2485 Destroying the bus stations (IDA*+ BFS)
传送门: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)的更多相关文章
- 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 ...
- 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
- HDU 2485 Destroying the bus stations(费用流)
http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...
- HDU 2485 Destroying the bus stations
2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...
- hdu 2485 Destroying the bus stations 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations ...
- Destroying the bus stations
Destroying the bus stations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1832 Acce ...
- Destroying the bus stations HDU - 2485(最小割点)
题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa 然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...
- POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索
题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...
随机推荐
- android编译系统学习
近日接手了后续android新平台项目搭建的任务. 本文内容基于sprd公司提供的android5.1源码. 一.一般的编译工作流程 我们代码一般情况下是从芯片商SPRD/MTK获得的. 源码的编译上 ...
- 【转】那些好用的iOS开发工具
原文:http://www.devtang.com/blog/2014/06/29/ios-dev-tools/ 前言 从苹果发明iPhone起,AppStore上的一个又一个类似flappy bir ...
- Asp.Net 母版页
背景:回顾下以前用到过的asp.net控件 介绍: 使用 ASP.NET 母版页可以为应用程序中的页创建一致的布局.单个母版页可以为应用程序中的所有页(或一组页)定义所需的外观和标准行为.然后可以创建 ...
- asp.net 图片质量压缩(不改变尺寸)
private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; en ...
- [转载]delete指针之后应该赋值NULL
首先,C++标准规定:delete空指针是合法的,没有副作用.但是,delete p后,只是释放了指针指向的内存空间.p并不会自动被置为NULL,而且指针还在,同时还指向了之前的地址. 问题来了,对一 ...
- 【BZOJ1010】玩具装箱
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- Android动画 fillAfter和fillBefore
fillBefore是指动画结束时画面停留在此动画的第一帧; fillAfter是指动画结束是画面停留在此动画的最后一帧. java代码设置如下: /*****动画结束时,停留在最后一帧******* ...
- When Colon Scripting is comming (脚本最佳体验)
当冒号脚本来临-- 脚本最佳体验 冒号指派 说明; 冒号替代等号指派赋值,当命名声明指派时指定.相当于声明当前作用域的一个名字指派. 当对指定对象的属性赋值时候,依旧请使用等号.即不废弃等号赋值功用, ...
- asp.net使用Mysql乱码处理
在asp.net与mysql数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文用户名的时候可以 ...
- android 调用系统相机
// 调用相机拍照的请求码 public static final int REQUEST_TAKE_PHOTO_CODE = 1; public static final int REQUEST_T ...