比赛 20120720 评测结果 AAAAAAATTT
题目名称 长途奔袭 最终得分 70
用户昵称 王者自由 运行时间 3.350 s
代码语言 C++ 内存使用 22.29 MiB
提交时间 2012-07-20 10:08:42
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1200, INF = 0x7ffffff;
int n, m, k, s, t, r;
int G[N][N], w[N][N], c[N][N], f[N][N];
int p[N], d[N], a, g, e;
bool l[N];
inline void adde(int u, int v, int W, int C) {
    w[u][v] = W;
    c[u][v] = C;
    w[v][u] = 0;
    c[v][u] = -C;
}
void MinCostFlow() {
    queue<int> q;
    for(; ; ) {
        for(int i=s; i<=t; i++)
            d[i] = INF, l[i] = 0;
        d[s] = 0;
        q.push(s);
        while(!q.empty()) {
            int u = q.front(); q.pop();
            l[u] = 0;
            for(int v=s; v<=t; v++)
                if(w[u][v] > f[u][v] && d[v] > d[u] + c[u][v]) {
                    d[v] = d[u] + c[u][v];
                    p[v] = u;
                    if(!l[v]) {
                        l[v] = 1;
                        q.push(v);
                    }
                }
        }
        if(d[t] == INF) break;
        a = INF;
        for(int u=t; u!=s; u=p[u])
            a = min(a, w[p[u]][u] - f[p[u]][u]);
        for(int u=t; u!=s; u=p[u]) {
            f[p[u]][u] += a;
            f[u][p[u]] -= a;
        }
        g += d[t] * a;
        e += a;
    }
}
int main() {
    freopen("YZ.in", "r", stdin);
    freopen("YZ.out", "w", stdout);
    scanf("%d %d", &n, &m);
    s = 0; t = n * 2 + 1;
    for(int i=1; i<=n; i++) {
        scanf("%d", &r);
        adde(s, i+n, 1, r);
        adde(s, i, 1, 0);
        adde(i+n, t, 1, 0);
    }
    int x, y, z;
    for(int i=1; i<=m; i++) {
        scanf("%d %d %d", &x, &y, &z);
        if(x > y) swap(x, y);
        adde(x, y+n, 1, z);
    }
    MinCostFlow();
    printf("%d\n", g);
    return 0;
}