程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 2485 Destroying the bus stations (dfs+bfs)

hdu 2485 Destroying the bus stations (dfs+bfs)

編輯:C++入門知識

Destroying the bus stations Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1942    Accepted Submission(s): 588     Problem Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station. No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station.     Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.     Input There are several test cases. Input ends with three zeros.   For each test case:   The first line contains 3 integers, n, m and k. (0< n <=50, 0< m<=4000, 0 < k < 1000) Then m lines follows. Each line contains 2 integers, s and f, indicating that there is a road from station No. s to station No. f.      Output For each test case, output the minimum number of stations Gabiluso must destroy.     Sample Input 5 7 3 1 3 3 4 4 5 1 2 2 5 1 4 4 5 0 0 0     Sample Output 2     Source 2008 Asia Regional Beijing       思路:   dfs+bfs。   1.bfs尋找一條的可行路線(起點到終點的距離<=k)。   2.如果可行路線不存在就會得到一個答案了,return。如果存在,對線路中的每個點dfs(去掉這個點),然後再回到1.   ps:這題和hdu1983挺像的。     代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 55
#define INF 0x3f3f3f3f
using namespace std;

int n,m,k,ans,aans,cnt,flag;
int pp[maxn];
bool vis[maxn],ok[maxn];
struct Node
{
    int v;
    int next;
} edge[4005];
struct node
{
    int x,cxx;
    int path[maxn];
} cur,now,q[maxn];

void addedge(int u,int v)
{
    cnt++;
    edge[cnt].v=v;
    edge[cnt].next=pp[u];
    pp[u]=cnt;
}
bool bfs()
{
    int i,j,u,v;
    int head=0,tail=-1;
    memset(vis,0,sizeof(vis));
    cur.x=1;
    cur.cxx=0;
    vis[1]=1;
    q[++tail]=cur;
    while(head<=tail)
    {
        now=q[head];
        head++;
        u=now.x;
        for(i=pp[u]; i; i=edge[i].next)
        {
            cur=now;
            v=edge[i].v;
            if(ok[v]) continue ;
            if(!vis[v])
            {
                vis[v]=1;
                cur.x=v;
                cur.cxx++;
                cur.path[cur.cxx]=v;
                if(v==n) return true ;
                q[++tail]=cur;
            }
        }
    }
    return false ;
}
void dfs(int num)
{
    if(num>=ans) return ;
    int i,j,u;
    if(bfs())
    {
        node tt=cur;
        if(tt.cxx>k)
        {
            if(ans>num) ans=num;
            return ;
        }
        for(i=1; i<tt.cxx; i++)
        {
            u=tt.path[i];
            ok[u]=1;
            dfs(num+1);
            ok[u]=0;
        }
    }
    else
    {
        if(ans>num) ans=num;
        return ;
    }
}
int main()
{
    int i,j,u,v;
    while(scanf("%d%d%d",&n,&m,&k),n||m||k)
    {
        cnt=0;
        memset(pp,0,sizeof(pp));
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        ans=INF;
        memset(ok,0,sizeof(ok));
        dfs(0);
        printf("%d\n",ans);
    }
    return 0;
}

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved