# bzoj 1458: 士兵占领

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

solution：

  1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #define mem(a,b) memset(a,b,sizeof(a))
5 using namespace std;
6 const int INF=(1<<31)-1;
7 inline int minn(int a,int b){return a<b?a:b;}
8 struct son
9 {
10     int u,v,next,w;
11 }a1[500001];
12 int first[500001],e;
13
14 void addbian(int u,int v,int w)
15 {
16     a1[e].v=v;
17     a1[e].u=u;
18     a1[e].w=w;
19     a1[e].next=first[u];
20     first[u]=e++;
21 }
22
23 int ji[501][501];
24 int n,m,k;
25 int u,o;
26 int num1[501],num2[501];
27 int heng[501],shu[501];
28 int sum,ans;
29 int S,T;
30 int dep[1001];
31
32 int dui[1000001],he,en;
33 inline void clear(){he=1;en=0;}
34 inline bool empty(){return en>=he?0:1;}
35 inline void push(int x){dui[++en]=x;}
36 inline void pop(){++he;}
37 inline int top(){return dui[he];}
38
39 int bfs()
40 {
41     mem(dep,0);
42     clear();
43     dep[S]=1;push(S);
44     while(!empty())
45     {
46         int now=top();pop();
47         for(int i=first[now];i!=-1;i=a1[i].next)
48         {
49             int temp=a1[i].v;
50             if(dep[temp]||!a1[i].w)continue;
51             dep[temp]=dep[now]+1;
52             push(temp);
53             if(temp==T)return 1;
54         }
55     }
56     return 0;
57 }
58
59 int dfs(int x,int val)
60 {
61     if(x==T)return val;
62     int val2=val,k;
63     for(int i=first[x];i!=-1;i=a1[i].next)
64     {
65         int temp=a1[i].v;
66         if(dep[temp]!=dep[x]+1||!a1[i].w||!val2)continue;
67         k=dfs(temp,minn(val2,a1[i].w));
68         if(!k){dep[temp]=0;continue;}
69         a1[i].w-=k;a1[i^1].w+=k;val2-=k;
70     }
71     return val-val2;
72 }
73
74 int Dinic()
75 {
76     int ans=0;
77     while(bfs())
78       ans+=dfs(S,INF);
79     return ans;
80 }
81
82 int main(){
83     int flag=0;
84     mem(first,-1);
85     scanf("%d%d%d",&n,&m,&k);
86     S=0;T=n+m+1;
87     for(int i=1;i<=n;++i)
88     {scanf("%d",&heng[i]);sum+=heng[i];}
89     for(int i=1;i<=m;++i)
90     {scanf("%d",&shu[i]);sum+=shu[i];}
91
92     for(int i=1;i<=k;++i)
93     {scanf("%d%d",&u,&o);ji[u][o]=1;++num1[u];++num2[o];}
94
95     for(int i=1;i<=n;++i)if(m-num1[i]<heng[i])flag=1;
96     for(int i=1;i<=m;++i)if(n-num2[i]<shu[i])flag=1;
97
98     for(int i=1;i<=n;++i)
99       for(int j=1;j<=m;++j)
101
102     for(int i=1;i<=n;++i)
104     for(int i=n+1;i<=n+m;++i)
106
107     int hh=Dinic();
108     if(flag)
109       printf("JIONG!");
110     else
111       printf("%d",sum-hh);
112     //while(1);
113     return 0;
114 }
code

posted @ 2017-07-30 06:22  A_LEAF  阅读(154)  评论(0编辑  收藏  举报