Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

[noi2008]志愿者招募

$
0
0
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
 
#define uns unsigned
#define int64 long long
#ifdef WIN32
#define fmt64 "%I64d"
#else
#define fmt64 "%lld"
#endif
#define oo 0x13131313
#define REP(i, n) for (i = 0; i < (n); ++i)
#define dual(e) (mem + (((e) - mem) ^ 1))
 
using namespace std;
 
struct edge { int t, f, w; edge *n; };
 
edge mem[60005], *adjc = mem, *adj[1005];
int S, T, n, m, a[1005], q[1005], dis[1005];
bool vst[1005]; int64 cost;
 
void link(int u, int v, int f, int w)
{
    *adjc = (edge) {v, f,  w, adj[u]}, adj[u] = adjc++;
    *adjc = (edge) {u, 0, -w, adj[v]}, adj[v] = adjc++;
}
 
bool spfa()
{
    int h, t;
    memset(vst, 0, sizeof vst);
    memset(dis, 0x13, sizeof dis), dis[S] = 0;
    for (q[h = t = S] = -1; ~h; vst[h] = 0, h = q[h])
        for (edge *e = adj[h]; e; e = e->n)
            if (e->f && dis[h] + e->w < dis[e->t])
                if (dis[e->t] = dis[h] + e->w, !vst[e->t]) {
                    if (vst[e->t] = 1, q[h] && dis[e->t] < dis[q[h]])
                        q[e->t] = q[h], q[h] = e->t; else q[t = q[t] = e->t] = -1;
                }
    return dis[T] < oo;
}
 
int dfs(int u, int f)
{
    if (u == T) return cost += f * dis[T], f;
    int l = f; static int x;
    vst[u] = 1;
    for (edge *e = adj[u]; e; e = e->n)
        if (e->f && dis[e->t] == dis[u] + e->w && !vst[e->t]) {
            x = dfs(e->t, f < e->f ? f : e->f);
            f -= x, e->f -= x, dual(e)->f += x;
            if (!f) return vst[u] = 0, l;
        }
    return l - f;
}
 
int main ()
{
    int i, j;
    scanf("%d%d", &n, &m);
    S = n + 1, T = S + 1;
    for (i = 1; i <= n; ++i) {
        scanf("%d", a + i);
        link(i - 1, i, oo, 0);
    }
    for (i = 0; i <= n; ++i) {
        j = a[i + 1] - a[i];
        if (j > 0)
            link(i, T, j, 0);
        else if (j < 0)
            link(S, i, -j, 0);
    }
    for (i = 1; i <= m; ++i) {
        int l, r, w;
        scanf("%d%d%d", &l, &r, &w);
        link(r, l - 1, oo, w);
    }
 
    for (; spfa(); )
        dfs(S, oo);
    printf(fmt64"\n", cost);
}


作者:JerryDung 发表于2013-5-6 21:38:14 原文链接
阅读:34 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles