【BZOJ 1458】 士兵佔領,bzoj1458士兵佔領

來源:互聯網
上載者:User

【BZOJ 1458】 士兵佔領,bzoj1458士兵佔領
1458: 士兵佔領

Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 632 Solved: 366
[Submit][Status][Discuss]
Description

有一個M * N的棋盤,有的格子是障礙。現在你要選擇一些格子來放置一些士兵,一個格子裡最多可以放置一個士兵,障礙格裡不能放置士兵。我們稱這些士兵佔領了整個棋盤當滿足第i行至少放置了Li個士兵, 第j列至少放置了Cj個士兵。現在你的任務是要求使用最少個數計程車兵來佔領整個棋盤。

Input

第一行兩個數M, N, K分別表示棋盤的行數,列數以及障礙的個數。 第二行有M個數表示Li。 第三行有N個數表示Ci。 接下來有K行,每行兩個數X, Y表示(X, Y)這個格子是障礙。

Output

輸出一個數表示最少需要使用計程車兵個數。如果無論放置多少個士兵都沒有辦法佔領整個棋盤,輸出”JIONG!” (不含引號)

Sample Input

4 4 4

1 1 1 1

0 1 0 3

1 4

2 2

3 3

4 3

Sample Output

4

資料範圍

M, N <= 100, 0 <= K <= M * N

有源匯有上下界的最小流。

詳見《有上下界的網路流問題》

#include <iostream>#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#define M 500005#include <queue>#define inf 0x3f3f3f3fusing namespace std;struct edge{    int from,to,cap,flow,ne;}E[M];int tot,h[M],d[M],s,t,cur[M],cant[105][105],v[M],n,m;void Addedge(int from,int to,int cap){    E[++tot]=(edge){from,to,cap,0,h[from]};    h[from]=tot;    E[++tot]=(edge){to,from,0,0,h[to]};    h[to]=tot;}int bfs(){    for (int i=s;i<=t;i++)        v[i]=0;    v[s]=1;    queue<int> q;    q.push(s);    d[s]=0;    while (!q.empty())    {        int x=q.front();        q.pop();        for (int i=h[x];i;i=E[i].ne)        {            edge e=E[i];            if (!v[e.to]&&e.cap>e.flow)            {                v[e.to]=1;                d[e.to]=d[x]+1;                q.push(e.to);            }        }    }    return v[t];}int dfs(int x,int a){    if (x==t||!a) return a;    int flow=0;    for (int &i=cur[x];i;i=E[i].ne)    {        edge &e=E[i];        if (d[e.to]!=d[x]+1) continue;        int f=dfs(e.to,min(a,e.cap-e.flow));        if (f>0)        {            flow+=f;            a-=f;            e.flow+=f;            E[i^1].flow-=f;            if (!a) break;        }    }    return flow;}int dinic(){    int flow=0;    while (bfs())    {        for (int i=s;i<=t;i++)            cur[i]=h[i];        flow+=dfs(s,inf);    }    return flow;}int main(){    tot=1;    int k;    scanf("%d%d%d",&n,&m,&k);    int jud=0;    int s1=n+m+1,t1=n+m+2;    s=0,t=n+m+3;    for (int i=1;i<=n;i++)    {        int x;        scanf("%d",&x);        jud+=x;        Addedge(s1,i,m-x);        Addedge(s,i,x),Addedge(s1,t,x);    }    for (int i=1;i<=m;i++)    {        int x;        scanf("%d",&x);        Addedge(i+n,t1,n-x);        Addedge(s,t1,x),Addedge(i+n,t,x);    }    for (int i=1;i<=k;i++)    {        int x,y;        scanf("%d%d",&x,&y);        cant[x][y]=1;    }    for (int i=1;i<=n;i++)        for (int j=1;j<=m;j++)            if (!cant[i][j])                Addedge(i,n+j,1);    int flow=dinic();    if (flow==jud)    {        Addedge(t1,s1,inf);        flow-=dinic();        printf("%d\n",E[tot-1].flow);    }    else puts("JIONG!");    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.