Air strike II BFS+DFS

Problem Description
Recently kingdom X is suffer a lot from the air strike caused by the enemy, after the air strike, L cities of kingdom X get fired. But because of the lake of the resource of the fire control, the firefighters can only put out the fire in K cities. After that, the fire would spread to the whole kingdom. There are N cities in kingdom X and M undirected roads connected them. If a city is fired on day t, then the cities adjacent to it (can be reached by only one road) would get fired on day t+1.

Now your task is to help Fat Brother to select these K cities in order to have as much as much time to let the citizens in kingdom X to flee. To achieve this, the time when the last city in kingdom X gets fired should be as late as possible.

The input date guarantees that you can arrive any city from any other city in kingdom X.
Input
The first line of the date is an integer T (1 <= T <= 100), which is the number of the text cases.

Then T cases follow, each case starts with four number N, M, L, K (1 <= N <= 100, 1 <= M <= N*(N-1)/2, 1 <= L <= 6, 0 <= K < L) which described before. Then goes one line with L integers indicates the index of the cities which are fired. The cities in kingdom X are numbered from 1 to N. Then goes M lines, each line contains two different integers x, y which means that there is road between city x and city y, note that there might be more than one roads between two cities. The input date guarantees that you can reach any city in kingdom X from any other city and there is at most one road between any two cities, no road connected a city to itself.
Output
For each case, output the case number first, then output the time when the last city get fired under the best strategy. Note that the air strike was on day 1.

See the sample input and output for more details.
Sample Input
4
3 2 2 1
1 2
1 2
2 3
3 3 3 0
1 2 3
1 2
2 3
1 3
3 3 3 1
1 2 3
1 2
2 3
1 3
3 3 3 2
1 2 3
1 2
2 3
1 3
Sample Output
Case 1: 3
Case 2: 1
Case 3: 2
Case 4: 2

解题思路:因为最多只有6个城市着火且最多只能灭掉五个,那就用状态表示灭掉火的城市,然后再依次BFS进行统计即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 110;
vector<int> line[maxn];
int fire[maxn], N, M, L, K, vis[maxn], ans;
struct Node{
    int id, time;
};

void init() {
    scanf("%d%d%d%d", &N, &M, &L, &K);

    for(int i = 0; i <= N; i++)
        line[i].clear();

    for(int i = 0; i < L; i++)
        scanf("%d", &fire[i]);

    int x, y;
    for(int i = 0; i < M; i++) {
        scanf("%d%d", &x, &y);
        line[x].push_back(y);
        line[y].push_back(x);
    }
}

int bfs() {
    queue<Node> q;
    while(!q.empty())
        q.pop();
    int cnt = L - K;
    if(cnt == N)
        return 0;

    for(int i = 0; i < L; i++) 
        if(vis[fire[i]]) {
            Node nod;
            nod.id = fire[i];
            nod.time = 0;
            q.push(nod);
        }
    while(!q.empty()) {
        Node node = q.front();
        q.pop();
        int ID = node.id;
        for(int i = 0; i < line[ID].size(); i++) { 
            if(!vis[line[ID][i]]) {
                Node tmp;
                tmp.id = line[ID][i];
                tmp.time = node.time + 1;
                cnt++;
                vis[line[ID][i]] = 1;
                q.push(tmp);
                if(cnt == N)
                    return tmp.time;
            }
        }
    }
}

void dfs() {
    ans = 0;
    for(int i = 0; i < (1 << L); i++) {
        int tmp = 0;
        for(int j = 0; j < L; j++)
            if(i & (1 << j))
                tmp++;
        if(tmp == K) {
            for(int j = 0; j <= N; j++)
                vis[j] = 0;
            for(int j = 0; j < L; j++)
                vis[fire[j]] = 1;
            for(int j = 0; j < L; j++)
                if(i & (1 << j)) 
                    vis[fire[j]] = 0;
            ans = max(ans, bfs());
        }
    }
}

int main() {
    int test, cas = 1;
    scanf("%d", &test);
    while(test--) {
        init();
        dfs();
        printf("Case %d: %d\n", cas++, ans + 1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值