[BZOJ2127]happiness

Sample Input

1 2
1 1
100 110
1
1000


Sample Output

1210


Solution

$a+b=v_1\\c+d=v_2\\a+d+e=b+c+e=v_1+v_2$

$a=b=\frac{v_1}{2},c=d=\frac{v_2}{2},e=\frac{v_1+v_2}{2}$

#include<bits/stdc++.h>
using namespace std;

x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

const int maxn = 2e5+10;
const int inf = 1e9;

int n,m,s,t,tot=1;
int l[102][102],r[102][102],a[102][102],b[102][102];
struct edge{int to,nxt,w;}e[maxn<<1];

void ins2(int u,int v,int w) {ins(u,v,w),ins(v,u,w);}

int bfs() {
memset(vis,0,(t+1)*10);
memset(dis,63,(t+1)*10);
queue<int > q;q.push(s);vis[s]=1;
while(!q.empty()) {
int now=q.front();q.pop();
if(!vis[e[i].to]&&e[i].w>0) {
dis[e[i].to]=dis[now]+1;
if(e[i].to==t) return 1;
q.push(e[i].to),vis[e[i].to]=1;
}
}return 0;
}

int dfs(int x,int f) {
if(x==t) return f;
int used=0;
if(e[i].w>0&&dis[e[i].to]==dis[x]+1) {
int d=dfs(e[i].to,min(f-used,e[i].w));
if(d>0) e[i].w-=d,e[i^1].w+=d,used+=d;
if(used==f) break;
}dis[x]=-1;return used;
}

int dinic() {
int flow=0;
while(bfs()) flow+=dfs(s,inf);
return flow;
}

int p(int x,int y) {return (x-1)*m+y;}

int main() {
s=n*m+2,t=s+1;
for(int i=1,x;i<=n;i++)
for(int i=1,x;i<=n;i++)
for(int i=1,x;i<=n-1;i++)
for(int j=1;j<=m;j++)
for(int i=1,x;i<=n-1;i++)
for(int j=1;j<=m;j++)
for(int i=1,x;i<=n;i++)
for(int j=1;j<=m-1;j++)