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个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...
随机推荐
- (转)ecshop 后台商品分类添加图片的功能
转之--http://blog.sina.com.cn/s/blog_4696b3760100n5ee.html 1 .首先找到数据表 ecs_category (商品分类表) 添加一 cat_i ...
- dbms_job dbms_scheduler简单比较
---------------------------陈旧的-------------------------------------/*--------------------- 创建job --- ...
- AutoLayout适配
http://www.raywenderlich.com/113768/adaptive-layout-tutorial-in-ios-9-getting-started iOS布局和屏幕适配的一点总 ...
- 【USACO 1.4.4】母亲的牛奶
[题目描述] 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原 ...
- 巧用Session Manager还原Firefox丢失会话
今天Firefox Crash之后,我的会话全部丢失了.按照以往来说,Firefox在重新启动之后或者Crash之后会有一个会话还原的页面.但今天确实没有.后来我进行Google查阅,试了很多种办法. ...
- jQuery 知识积累
1.select下拉框设置选中项 //设置下拉框第一项为选中项$("#selectId option:first").prop("selected", 'sel ...
- 面向对象设计模式之Flyweight享元模式(结构型)
动机:采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行代价——主要指内存需求方面的代价.如何在避免大量细粒度对象问题的同 时,让外部客户程序仍然能够透明地使用面向对象的 ...
- 织梦DedeCms用SQL语句调用数据库任意内容
dedecms多站点数据利用SQL句段进行互相调用数据方法:2个或者多个DEDE的站怎么互相调用数据,非JS调用,前提是2个或者多个dedecms站点都安装的同一个数据库的不同数据表内,才能实现功能. ...
- PHP面向对象(OOP)编程完全教程:10.__set(),__get(),__isset(),__unset()四个方法的应用
一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...
- S5PV210启动过程分析
一.三星官方推荐方式 1.数据手册<S5PV210_iROM_Application_note>中截取: