hdu 2485 Destroying the bus stations 迭代加深搜索

求最少去掉几个公交站使得从s到t的最短路径大于k。

迭代加深搜索。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 60
#define maxm 50000
int n,m,K,cnt,up;
int head[maxn],pre[maxn];
int road[maxn][maxn];
bool del[maxn];
queue<int> Q;
bool flag;
struct Edge
{
    int from,to,next;
}e[maxm];
void make(int from,int to)
{
    e[cnt].from=from;
    e[cnt].to=to;
    e[cnt].next=head[from];
    head[from]=cnt++;
}
void bfs()
{
    int i;
    memset(pre,0,sizeof(pre));
    pre[1]=-1;
    Q.push(1);
    while(!Q.empty())
    {
        int u=Q.front();Q.pop();
        for(i=head[u];i!=-1;i=e[i].next)
        {
            if(del[e[i].to]==0&&pre[e[i].to]==0)
            {
                pre[e[i].to]=u;
                Q.push(e[i].to);
            }
        }
    }
}
void dfs(int x)
{
    int i;
    //if(flag==true) return ;
    bfs();
    if(pre[n]==0)
    {
        flag=true;
        return ;
    }
    int num=0;
    for(i=n;i!=-1;i=pre[i])
    {
        num++;
        road[x][num]=i;
    }
    for(i=1;i<=num;i++)
    if(num-1>K)
    {
        flag=true;
        return;
    }
    if(x>up) return;
    for(i=num-1;i>1;i--)
    {
        del[road[x][i]]=1;
        dfs(x+1);
        del[road[x][i]]=0;
    }

}
int main()
{
    while(1)
    {
        int i,j,u,v;
        scanf("%d%d%d",&n,&m,&K);
        if(n==0) break;
        cnt=0;
        flag=0;
        memset(head,-1,sizeof(head));
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            make(u,v);
        }
        for(i=0;i<=n-2;i++)
        {
            up=i;
            memset(del,0,sizeof(del));
            dfs(1);
            if(flag==true)
                break;
        }
        printf("%d\n",up);
    }
    return 0;
}

hdu 2485 Destroying the bus stations 迭代加深搜索,布布扣,bubuko.com

时间: 2024-12-14 06:22:11

hdu 2485 Destroying the bus stations 迭代加深搜索的相关文章

HDU 2485 Destroying the bus stations(费用流)

http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要破坏多少个车站. 思路: 把每个点拆分为两个点,容量为1,费用为0.之后相邻的车站连边,容量为INF,费用为1,表示经过一个车站需要1时间. 这样一来,跑一遍费用流计算出在费用不大于k的情况下的最大流,也就是最小割,即至少要破坏的车站数. 在网络中寻求关于f的最小费用增广路,就等价于在伴随网络中寻求

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

题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成对的点容量的限制.对于边i -> j,先建边i ->i',再建i'->j.i ->i'只能建一次,容量为1,费用为0.i'->j的容量是INF.此题中因为已经有源点,所以源点(1)不能限制容量. 1 #include<iostream> 2 #include<c

HDU 2485 Destroying the bus stations

2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; const int INF=0x7FFFFFFF; const int maxn=50+10;//点的数量 int n,m,k; in

Destroying the bus stations (hdu 2485 网络流+最短路)

Destroying the bus stations Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2313    Accepted Submission(s): 739 Problem Description Gabiluso is one of the greatest spies in his country. Now he'

Destroying the bus stations

hdu2485:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你一个图,让你删除其中的一些点,然后使得1到n的最小距离大于k,求删除的最小的点数. 题解:DFS枚举最短路径上的点. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #def

hdu 1560 DNA sequence(迭代加深搜索)

DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 15   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The twenty-first century

hdu 1560 迭代加深搜索

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 只能说bin神太给力了.. 又学到不少新知识.. 迭代加深搜索,貌似 又叫IDA*, 就是给搜索深度一个限制,搜索到一个满足条件就结束. 注意剪枝~ 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; char g[10][10]; int size[10];

HDUOJ----2485 Destroying the bus stations

Destroying the bus stations Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2072    Accepted Submission(s): 647 Problem Description Gabiluso is one of the greatest spies in his country. Now he'

HDU2485 Destroying the bus stations(最小割---点割)

Destroying the bus stations Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2319    Accepted Submission(s): 743 Problem Description Gabiluso is one of the greatest spies in his country. Now he'