HDU 2485 destroying the bus stations iterative deepening search

Source: Internet
Author: User

Remove at least a few bus stops so that the shortest path from S to T is greater than K.

Iterative deepening of search.

#include <cstdio>#include <cstring>#include <queue>using namespace std;#define maxn 60#define maxm 50000int 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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.