zoj 3229 Shoot the Bullet(有源匯上下界最大流)

Shoot the Bullet

Time Limit: 2 Seconds      Memory Limit: 32768 KB      Special Judge

Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies,youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been inGensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns theBunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautifuldanmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls ofGensokyo among the tengu. Her intelligence gathering abilities are the best inGensokyo!

During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at leastGx photos of girl x in total in theBunkachou. At the k-th day, there are Ck targets,Tk1, Tk2, ...,TkCk. The number of photos of targetTki that Aya takes should be in range [Lki,Rki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use herlast spell card to attack Aya. What's more, Aya cannot take more than Dk photos at the k-th day.Under these constraints, the more photos, the better.

Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

Input

There are about 40 cases. Process to the end of file.

Each case begins with two integers 1 <= n <= 365, 1 <= m <= 1000. Thenm integers, G1, G2, ..., Gm in range [0, 10000]. Thenn days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then C different targets. Each target is described by three integers, 0 <=T < m, 0 <= L <= R <= 100.

Output

For each case, first output the number of photos Aya can take, -1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

Output a blank line after each case.

Sample Input

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 3 9
1 3 9
2 3 9

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9

2 3
12 12 12
3 15
0 3 9
1 3 9
2 3 9
3 21
0 0 3
1 3 6
2 6 12

Sample Output

36
6
6
6
6
6
6

36
9
6
3
3
6
9

-1

 

 題意:有m個女孩,要在n天內拍照,每個女孩至少要拍G[j]張。每天只能拍D[i]張,並且對於第i天,只可以對C[i]個給定的女孩j拍照,而給j拍照時,拍照的數量只能爲[L[j], R[j]]張。問n天后能否拍到滿足條件的照片,若能,輸出每一天在該天拍攝的女孩拍了多少張照片。

思路:虛擬一個源點和匯點。源點到每天連一條邊,容量爲D[i],每天到給定的女孩連一條邊,容量爲L - R,每個女孩連一條邊到匯點,容量爲INF-G[i]。匯點到源點連一條邊,容量爲INF,構成無源匯的循環流圖。再虛擬出一個超級源點ss和匯點tt,根據每個點流進和流出的流量建圖,求一次最大流,滿流則可行。之後,刪除ss和tt,再求一次最大流,即爲最終答案。


AC代碼:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
using namespace std;

const int INF = 1e9;
const int maxn = 2005;

struct Edge
{
    int u, v, cap, flow, next;
} et[maxn * 500];
int low[maxn], cnt[maxn], dis[maxn], pre[maxn], cur[maxn], eh[maxn], du[maxn];
int G[maxn], C[maxn], D[maxn], T[400][1005], L[400][1005], R[400][1005];
int n, m, s, t, num, ss, tt, sum;
void init()
{
    memset(eh, -1, sizeof(eh));
    memset(du, 0, sizeof(du));
    num = sum = 0;
}
void add(int u, int v, int cap, int flow)
{
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap)
{
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
int isap(int s, int t, int nv)
{
    int u, v, now, flow = 0;
    memset(low, 0, sizeof(low));
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(low[u], et[now].cap - et[now].flow);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
                    dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
void build()
{
    init();
    s = 0, t = n + m + 1;
    ss = t + 1, tt = t + 2;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= C[i]; j++)
        {
            addedge(i, n + T[i][j] + 1, R[i][j] - L[i][j]);
            du[i] -= L[i][j];
            du[n + T[i][j] + 1] += L[i][j];
        }
    }
    for(int i = 1; i <= n; i++)
        addedge(s, i, D[i]);
    for(int i = 1; i <= m; i++)
    {
        addedge(n + i, t, INF - G[i]);
        du[n + i] -= G[i];
        du[t] += G[i];
    }
    addedge(t, s, INF);
    for(int i = s; i <= t; i++)
    {
        if(du[i] > 0)
        {
            addedge(ss, i, du[i]);
            sum += du[i];
        }
        else if(du[i] < 0) addedge(i, tt, -du[i]);
    }
}
void solve()
{
    build();
    int tmp = isap(ss, tt, tt + 1); //求可行流
    if(tmp != sum)
    {
        printf("-1\n");
        return;
    }
    int ans = isap(s, t, tt + 1);   //刪除ss和tt,求最大流
    printf("%d\n", ans);
    int cnt = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= C[i]; j++)
        {
            printf("%d\n", et[cnt].flow + L[i][j]);
            cnt += 2;
        }
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= m; i++)
            scanf("%d", &G[i]);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &C[i], &D[i]);
            for(int j = 1; j <= C[i]; j++)
                scanf("%d%d%d", &T[i][j], &L[i][j], &R[i][j]);
        }
        solve();
        puts("");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章