Sicily 1827/1947. Snipers

1827. Snipers

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Snipers play an important role in wars. They kill enemy soldiers who are far away. A small team of snipers can stop hundreds of infantrymen. When in war, a commander must set snipers in the most valuable positions.
       You are the commander today. The enemy is approaching. You must send out your snipers to stop them. There are N+1 positions, which are identified by 0, 1… N. The enemy soldiers set out at position 0. Their destination is position N. They can go from one position to another if the two positions are “connected”. You are in position N and you don’t want any enemy soldiers get to you. The snipers can only be set in position 1, 2… N-1.

       For position i (0 < i < N), no one can pass by if there are no less than Ki (0 < Ki< 100) snipers in it. That means no enemy soldiers can come to the position and then go ahead to another. They will be shot before they arrive. Please note that position 0 and N are NOT connected.

Input

The first line contains an integer indicating the number of test cases.
For each test case, two integers N and M (0 < N  < 50, 0  < M  < 200) are in the first line. The next line has N-1 integers. They are  K1, K2… KN-1. Following are M lines. Each contains a pair of integers A and B (0  < A, B  < N), which means that position A and B are “connected”. There is NO bland line between two cases.

Output

For each test case, output the less snipers needed, one case per line. There is NO bland line between two cases.

Sample Input

1
4 5
2 3 4
0 1
0 3
1 2
2 4
3 4

Sample Output

6

// Problem#: 1827
// Submission#: 3589660
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>

const int MAXN = 105;
const int MAXNN = MAXN / 2;

int graph[MAXN][MAXN];
int node[MAXNN];
int n, c[MAXN + 1][MAXN + 1], s, t;
int f[MAXN + 1][MAXN + 1];
int pre[MAXN + 1], tag[MAXN + 1];

int find() {
    int queue[MAXN + 10], head, tail;
    int x, loop;
    for (loop = 0; loop <= n; loop++) tag[loop] = 0;
    queue[0] = s;
    head = 0;
    tail = 1;
    tag[s] = 1;
    while (head != tail) {
        x = queue[head++];
        if (head == MAXN + 10) head = 0;
        for (loop = 0; loop <= n; loop++) if (tag[loop] == 0) {
            if (f[x][loop] < c[x][loop]) {
                pre[loop] = x;
                tag[loop] = 1;
                if (loop == t) return 1;
                queue[tail++] = loop;
                if (tail == MAXN + 10) tail = 0;
            } else if (f[loop][x] > 0) {
                pre[loop] = x;
                tag[loop] = -1;
                if (loop == t) return 1;
                queue[tail++] = loop;
                if (tail == MAXN + 10) tail = 0;
            }
        }
    }
    return 0;
}

void adjust() {
    int min = 2000000000, loop;
    for (loop = t; loop != s; loop = pre[loop]) {
        if (tag[loop] > 0) {
            if (c[pre[loop]][loop] - f[pre[loop]][loop] < min)
                min = c[pre[loop]][loop] - f[pre[loop]][loop];
        } else {
            if (f[loop][pre[loop]] < min)
                min = f[loop][pre[loop]];
        }
    }
    for (loop = t; loop != s; loop = pre[loop]) {
        if (tag[loop] > 0) {
            f[pre[loop]][loop] += min;
        } else {
            if (f[loop][pre[loop]] < min)
                f[loop][pre[loop]] -= min;
        }
    }
}

void solve() {
    int i, j;
    for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) f[i][j] = 0;
    while (find()) adjust();
}

int main() {
    int testcase, nn, mm;
    scanf("%d", &testcase);
    while (testcase--) {
        memset(graph, 0, sizeof(graph));
        memset(node, 0, sizeof(node));
        scanf("%d%d", &nn, &mm);
        for (int i = 1; i <= nn - 1; i++) scanf("%d", node + i);
        for (int i = 0; i < mm; i++) {
            int a1, a2, t;
            scanf("%d%d", &a1, &a2);
            if (a1 > a2) {
                t = a1;
                a1 = a2;
                a2 = t;
            }
            graph[a1][a2] = 1;
            if (a2 != nn && a1 != 0) graph[a2][a1] = 1;
        }
        memset(c, 0, sizeof(c));
        for (int i = 1; i <= nn - 1; i++)
            c[i][i + nn] = node[i];
        for (int i = 0; i <= nn; i++)
            for (int j = 0; j <= nn; j++)
                if (graph[i][j]) {
                    int t1, t2;
                    if (i == 0) {
                        t1 = 0;
                        t2 = j;
                    } else {
                        t1 = i + nn;
                        t2 = j;
                    }
                    c[t1][t2] = 2000000000;
                }
                n = nn * 2 - 1;
                s = 0;
                t = nn;
                solve();
                int maxFlow = 0;
                for (int i = 0; i <= n; i++)  maxFlow += f[i][nn];
                printf("%d\n", maxFlow);
    }
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值