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
| #include <iostream> #include <cstdio> #include <cstring> #include <queue> #define MAX_N 10000 #define MAX_M 200000 #define INF 2147483647 using namespace std; int n, m, s, t, ans, a[65][10]; int pre[MAX_N+5], cnt; struct node {int v, c, w, nxt;} E[MAX_M+5]; void init() {s = 0, t = n*m+n+1, cnt = 0; memset(pre, -1, sizeof(pre));} void insert(int u, int v, int c, int w) { E[cnt].v = v, E[cnt].c = c, E[cnt].w = w, E[cnt].nxt = pre[u], pre[u] = cnt++; E[cnt].v = u, E[cnt].c = 0, E[cnt].w =-w, E[cnt].nxt = pre[v], pre[v] = cnt++; } bool SPFA() { queue <int> que; bool inque[MAX_N+5]; int dis[MAX_N+5], pree[MAX_N+5]; memset(inque, false, sizeof(inque)), memset(pree, -1, sizeof(pree)); for (int i = s; i <= t; i++) dis[i] = INF; dis[s] = 0, que.push(s), inque[s] = true; while (!que.empty()) { int u = que.front(); que.pop(), inque[u] = false; for (int i = pre[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c, w = E[i].w; if (c && dis[u]+w < dis[v]) { dis[v] = dis[u]+w, pree[v] = i; if (!inque[v]) que.push(v), inque[v] = true; } } } if (dis[t] == INF) return false; int flow = INF; for (int i = pree[t]; ~i; i = pree[E[i^1].v]) flow = min(flow, E[i].c); for (int i = pree[t]; ~i; i = pree[E[i^1].v]) E[i].c -= flow, E[i^1].c += flow; ans += dis[t]; return true; } int main() { scanf("%d%d", &m, &n), init(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]); for (int i = 1; i <= n; i++) insert(s, i, 1, 0); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) for (int k = 1; k <= n; k++) insert(i, j*n+k, 1, a[i][j]*k); for (int j = 1; j <= m; j++) for (int k = 1; k <= n; k++) insert(j*n+k, t, 1, 0); while (SPFA()) ; printf("%.2lf", (double)ans/n); return 0; }
|