码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 546E

时间:2015-08-05 16:21:08      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

第一行是城市数量、路的数量

第二行是开始的城市小兵

第三行是结束时候的

后面的是哪两个城市之间有路

士兵只能移动一次,问能不能达到最后的状态

输出,,,

比如本来第二个城市的士兵是0 10 0 0

他和1 4城有路,需要分别运3个,4个冰,那么输出就是3 3 0 4


#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 200 + 5;
const int S = N - 1;
const int T = N - 2;

int n, m, ans, a[N], b[N], cap[N][N];
bool h[N];

int dfs(int x, int k) {
    if(x == T)
        return k;
    h[x] = 1;
    for(int i = 0; i < N; i++) {
        if(!h[i] and cap[x][i] > 0) {
            int res = dfs(i, min(k, cap[x][i]));
            if(res > 0) {
                cap[x][i] -= res;
                cap[i][x] += res;
                return res;
            }
        }
    }
    return 0;
}

int main ()
{
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", a + i);
    for(int i = 1; i <= n; i++)
        scanf("%d", b + i);
    int suma = 0, sumb = 0;
    for(int i = 1; i <= n; i++) {
        cap[2 * i][2 * i - 1] = inf;
        cap[S][2 * i] = a[i];
        cap[2 * i - 1][T] = b[i];
        suma += a[i];
        sumb += b[i];
    }
    if(suma != sumb) {
        puts("NO");
        return 0;
    }

    for(int i = 1; i <= m; i++) {
        int x, y;
        scanf("%d %d", &x, &y);
        cap[2 * x][2 * y - 1] = inf;
        cap[2 * y][2 * x - 1] = inf;
    }

    int flow = 0;

    for(; ;) {
        memset(h, 0, sizeof(h));
        int ans = dfs(S, inf);
        if(!ans)
            break;
        flow += ans;
    }

    if(flow != suma)
        puts("NO");
    else {
        puts("YES");
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
                printf("%d ", cap[j * 2 - 1][i * 2]);
            puts("");
        }
    }
    return 0;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 546E

标签:

原文地址:http://blog.csdn.net/zhangwenchi/article/details/47297463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!