【
题意】有K台挤奶机,C头奶牛,在奶牛和机器间有一组长度不同的路,每台机器每天最多能为M头奶牛挤奶。现在要寻找一个方案,安排每头奶牛到某台机器挤奶,使得C头奶牛中走过的路径长度的和的最大值最小。 挺好的一道题~也算是经典的模型,里面所用到的方法也很经典、常用。 【
思路】易知模型其实是个二分图,只是每头奶牛可以去多台机器,所以二分图多重匹配就可以解,因为在练最大流,就拿最大流解了。 先
Floyd预处理两两之间的最短距离,然后
二分距离,每次以二分的mid为容量限制建图,即保留图中长度<=mid的边,然后添加源汇,源到每头牛连容量为1的边,每台机器到汇点连容量为M的边,然后跑一次最大流,如果满流,说明C头奶牛都可以在mid的情况下被分配,则不断二分找mid的最小值即可。 严格上讲这类题需要拆点的,详见
此题。本题不用拆点是因为其本身就是二分图,不存在间接流量边。 [cpp] #define MID(x,y) ((x+y)/2) #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int MAXV = 255; const int MAXE = 100005; const int oo = 0x3fffffff; struct node{ int u, v, flow; int opp; int next; }; struct Dinic{ node arc[MAXE]; int vn, en, head[MAXV]; //vn点个数(包括源点汇点),en边个数 int cur[MAXV]; //当前弧 int q[MAXV]; //bfs建层次图时的队列 int path[MAXE], top; //存dfs当前最短路径的栈 int dep[MAXV]; //各节点层次 void init(int n){ vn = n; en = 0; mem(head, -1); } void insert_flow(int u, int v, int flow){ arc[en].u = u; arc[en].v = v; arc[en].flow = flow; arc[en].opp = en + 1; arc[en].next = head[u]; head[u] = en ++; arc[en].u = v; arc[en].v = u; arc[en].flow = 0; //反向弧 arc[en].opp = en - 1; arc[en].next = head[v]; head[v] = en ++; } bool bfs(int s, int t){ mem(dep, -1); int lq = 0, rq = 1; dep[s] = 0; q[lq] = s; while(lq < rq){ int u = q[lq ++]; if (u == t){ return true; } for (int i = head[u]; i != -1; i = arc[i].next){ int v = arc[i].v; if (dep[v] == -1 && arc[i].flow > 0){ dep[v] = dep[u] + 1; q[rq ++] = v; } } } return false; } int solve(int s, int t){ int maxflow = 0; while(bfs(s, t)){ int i, j; for (i = 1; i <= vn; i ++) cur[i] = head[i]; for (i = s, top = 0;;){ if (i == t){ int mink; int minflow = 0x3fffffff; for (int k = 0; k < top; k ++) if (minflow > arc[path[k]].flow){ minflow = arc[path[k]].flow; mink = k; } for (int k = 0; k < top; k ++) arc[path[k]].flow -= minflow, arc[arc[path[k]].opp].flow += minflow; maxflow += minflow; top = mink; //arc[mink]这条边流量变为0, 则直接回溯到该边的起点即可(这条边将不再包含在增广路内). i = arc[path[top]].u; } for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){ int v = arc[j].v; if (arc[j].flow && dep[v] == dep[i] + 1) break; } if (j != -1){ path[top ++] = j; i = arc[j].v; } else{ if (top == 0) break; dep[i] = -1; i = arc[path[-- top]].u; } } } return maxflow; } }dinic; int map[250][250]; void floyd(int n){ for (int k = 0; k < n; k ++){ for (int i = 0; i < n; i ++){ if(map[i][k] == oo) continue; for (int j = 0; j < n; j ++){ if (map[j][k] == oo) continue; if (map[i][j] > map[i][k] + map[k][j]) map[i][j] = map[i][k] + map[k][j]; } } } return ; } int go(int mid, int k, int c, int m){ dinic.init(k+c+2); for (int i = 1; i <= k; i ++){ dinic.insert_flow(k+c+1, i, m); } for (int i = k+1; i <= k+c; i ++){ dinic.insert_flow(i, k+c+2, 1); } for (int i = 0; i < k; i ++){ for (int j = k; j < k+c; j ++){ if (map[i][j] <= mid){ dinic.insert_flow(i+1, j+1, 1); } } } return dinic.solve(k+c+1, k+c+2); } int BS(int k, int c, int m){ int l = 0, r = 10000000; while(l < r){ int mid = MID(l,r); if (go(mid, k, c, m) == c){ r = mid; } else{ l = mid + 1; } } return r; } int main(){ int k, c, m; scanf("%d %d %d", &k, &c, &m); for (int i = 0; i < k+c; i ++){ for (int j = 0; j < k+c; j ++){ scanf("%d", &map[i][j]); if (map[i][j] == 0) map[i][j] = oo; } } floyd(k+c); printf("%d\n", BS(k, c, m)); return 0; }[/cpp]