1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <bits/stdc++.h>
using namespace std;
const int N = 10010000; const int INF = (int)2e9;
int n, m, k;
int S, T, vis[N];
struct edge { int v, f; edge *next, *rev; } pool[N * 2], *h[N], *cu = pool, *cur[N];
void addedge(int u, int v, int f) { edge *p = cu++, *q = cu++; p->v = v, p->next = h[u], h[u] = p, p->f = f, p->rev = q; q->v = u, q->next = h[v], h[v] = q, q->f = 0, q->rev = p; }
int dep[N], Q[N];
bool bfs() { for(int i = S; i <= T; i++) cur[i] = h[i], dep[i] = 0; int s = 1, t = 0; dep[S] = 1, Q[++t] = S; while(s <= t) { int u = Q[s++]; for(edge *p = h[u]; p; p = p->next) { int v = p->v; if(p->f && !dep[v]) { Q[++t] = v, dep[v] = dep[u] + 1; if(v == T) return 1; } } } return 0; }
int dfs(int u, int lim) { if(u == T || !lim) return lim; int ret = 0; for(edge *p = cur[u]; p; p = p->next) { cur[u] = p; int v = p->v; if(p->f && dep[v] == dep[u] + 1) { int tmp = dfs(v, min(lim, p->f)); p->f -= tmp, p->rev->f += tmp; ret += tmp, lim -= tmp; } } if(!ret) dep[u] = -1; return ret; }
int dinic() { int ret = 0; while(bfs()) ret += dfs(S, INF); return ret; }
int a[1005][1005]; int dx[8] = {1, 1, 2, 2, -1, -1, -2, -2}; int dy[8] = {2, -2, 1, -1, 2, -2, 1, -1};
int id(int x, int y) { return (x - 1) * (m + 1) + y; }
int main() { cin >> n >> m >> k; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> a[i][j]; S = 0; T = n * (m + 1) + 1; for(int i = 1; i <= n; i++) { addedge(S, id(i, 1), INF); for(int j = 1; j <= m; j++) { if(a[i][j] != -1) addedge(id(i, j), id(i, j + 1), 100 - a[i][j]); else addedge(id(i, j), id(i, j + 1), INF); } addedge(id(i, m + 1), T, INF); } for(int i = 1; i <= k; i++) { int x, y; cin >> x >> y; for(int j = 1; j <= m; j++) addedge(id(x, j), id(y, j + 1), INF); } printf("%.2lf\n", 1.0 * 100 - 1.0 * dinic() / (1.0 * n)); return 0; }
|