# bzoj2400 Spoj 839 Optimal Marks

3 2

2

-1

0

1 2

2 3

2

2

n<=500，m<=2000

2结点的值定为0即可。

#### 题解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 60010
using namespace std;
const int inf = 0x3f3f3f3f;
struct flows {
struct node {
int to, next, flow;
} e[N];
int tot, st[N], dis[N], cur[N];
void init() {
tot = -1, memset(e, -1, sizeof e),
memset(st, -1, sizeof st);
}
void add(int x, int y, int z) {
e[++tot].to = y;
e[tot].flow = z;
e[tot].next = st[x];
st[x] = tot;
//      printf("add:%d %d %d\n", x, y, z);
}
void add_edge(int x, int y, int z) {
}
queue <int> que;
int bfs(int S, int T) {
memcpy(cur, st, sizeof st);
memset(dis, 0, sizeof dis);
while (!que.empty()) que.pop();
que.push(S);
dis[S] = 1;
while(!que.empty()) {
int now = que.front();
que.pop();
for (int i = st[now]; ~i; i = e[i].next)
if (e[i].flow && !dis[e[i].to]) {
dis[e[i].to] = dis[now] + 1;
if (e[i].to == T) return 1;
que.push(e[i].to);
}
}
return 0;
}
int finds(int now, int T, int flow) {
if (now == T)
return flow;
int f;
for (int i = cur[now]; ~i; i = e[i].next) {
cur[now] = i;
if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
(f = finds(e[i].to, T, min(flow, e[i].flow)))) {
e[i].flow -= f;
e[i^1].flow += f;
return f;
}
}
return 0;
}
int dinic(int S, int T) {
int flow = 0, x;
while(bfs(S, T)) {
while(x = finds(S, T, inf)) {
flow += x;
}
}
return flow;
}
}flow;
int n, m, S, T, a[N], vis[N], val[N];
long long ans1, ans2;
struct edge {
int x, y;
}e[N];
void work(int now) {
flow.init();
S = 0, T = n + 1;
for (int i = 1; i <= m; ++i)
for (int i = 1; i <= n; ++i)
if (a[i] >= 0)
if ((a[i] & (1 << now)))
else
}
void dfs(int now, int k) {
vis[now] = 1;
val[now] |= 1 << k;
for (int i = flow.st[now]; ~i; i = flow.e[i].next)
if (!vis[flow.e[i].to] && flow.e[i].flow)
dfs(flow.e[i].to, k);
}
main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i)
scanf("%d%d", &e[i].x, &e[i].y);
long long ans = 0;
for (int i = 0; i < 31; ++i) {
work(i);
ans += flow.dinic(S, T) * (1ll << i);
memset(vis, 0, sizeof vis);
dfs(S, i);
}
for (int i = 1; i <= n; ++i)
if (a[i] >= 0)
val[i] = a[i];
for (int i = 1; i <= m; ++i)
ans1 += val[e[i].x] ^ val[e[i].y],
ans2 += val[i];
printf("%lld\n%lld\n", ans1, ans2);
}