POJ2112-Optimal Milking
来源:互联网 发布:淘宝详情图尺寸 编辑:程序博客网 时间:2024/04/19 05:56
Optimal Milking
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 17153 Accepted: 6167Case Time Limit: 1000MS
Description
FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Input
* Line 1: A single line with three space-separated integers: K, C, and M.
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
Output
A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
Sample Input
2 3 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0
Sample Output
2
Source
USACO 2003 U S Open
题意:有K个挤奶器运到牧场,C头奶牛,在奶牛和挤奶器之间有一组不同长度的路。K个挤奶器的位置用1~K的编号标明,奶牛的位置用K+1~K+C 的编号标明。每台挤奶器每天最多能为M头奶牛挤奶。安排每头奶牛到某个挤奶器挤奶,每头奶牛到挤奶器之间都有一个最短距离的路程,现在要求所有奶牛的最短路程中的最大值最小。每个测试数据中至少有一个安排方案,每条奶牛到挤奶器有多条路。
解题题解: 先用Floyd求出两两之间的最短距离,然后二分枚举最短距离,用二分图多重匹配求出最大能完成的奶牛个数,小于总的奶牛个数,说明距离太小,大于说明距离可以继续缩小。也可以用网络流的方法
多重二分匹配:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>#include <bitset>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int k,c,m;int y[1005][505];int visit[1005];int sumy[1005];int mp[300][300],g[300][300];bool path(int u,int sum){ for(int i=0;i<k;i++) { if(!visit[i]&&g[u][i]<=sum) { visit[i]=1; if(sumy[i]<m) { y[i][sumy[i]++]=u; return 1; } for(int j=0;j<sumy[i];j++) { if(path(y[i][j],sum)) { y[i][j]=u; return 1; } } } } return 0;}int MaxMatch(int sum){ int ans=0; memset(sumy,0,sizeof sumy); for(int i=0;i<c;i++) { memset(visit,0,sizeof visit); if(path(i,sum)) ans++; } return ans;}int main(){ while(~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",&mp[i][j]); if(!mp[i][j]) mp[i][j]=INF; } } for(int p=0;p<k+c;p++) { for(int i=0;i<k+c;i++) { for(int j=0;j<k+c;j++) mp[i][j]=min(mp[i][j],mp[i][p]+mp[p][j]); } } for(int i=0;i<c;i++) { for(int j=0;j<k;j++) g[i][j]=mp[i+k][j]; } int l=0,r=50000,ans; while(l<=r) { int mid=(l+r)>>1; if(MaxMatch(mid)>=c) {ans=mid;r=mid-1;} else l=mid+1; } printf("%d\n",ans); } return 0;}
网络流:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <stack> #include <map> #include <climits> #include <bitset> using namespace std;#define LL long long const int INF = 0x3f3f3f3f;#define MAXN 500int k, c, m;int mp[300][300];struct node{int u, v, next, cap;} edge[MAXN*MAXN];int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];int cnt;void init(){cnt = 0;memset(s, -1, sizeof(s));}void add(int u, int v, int c){edge[cnt].u = u;edge[cnt].v = v;edge[cnt].cap = c;edge[cnt].next = s[u];s[u] = cnt++;edge[cnt].u = v;edge[cnt].v = u;edge[cnt].cap = 0;edge[cnt].next = s[v];s[v] = cnt++;}bool BFS(int ss, int ee){memset(d, 0, sizeof d);d[ss] = 1;queue<int>q;q.push(ss);while (!q.empty()){int pre = q.front();q.pop();for (int i = s[pre]; ~i; i = edge[i].next){int v = edge[i].v;if (edge[i].cap > 0 && !d[v]){d[v] = d[pre] + 1;q.push(v);}}}return d[ee];}int DFS(int x, int exp, int ee){if (x == ee || !exp) return exp;int temp, flow = 0;for (int i = nt[x]; ~i; i = edge[i].next, nt[x] = i){int v = edge[i].v;if (d[v] == d[x] + 1 && (temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0){edge[i].cap -= temp;edge[i ^ 1].cap += temp;flow += temp;exp -= temp;if (!exp) break;}}if (!flow) d[x] = 0;return flow;}int Dinic_flow(int d){init();for (int i = 0; i < k; i++) add(k + c, i, m);for (int i = k; i < k + c; i++) add(i, k + c + 1, 1);for (int i = 0; i < k; i++)for (int j = k; j < k + c; j++)if (mp[i][j] <= d) add(i, j, 1);int ans = 0;while (BFS(k+c, k+c+1)){for (int i = 0; i <= k+c+1; i++) nt[i] = s[i];ans += DFS(k+c, INF, k+c+1);}return ans;}int main(){while (~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", &mp[i][j]);if (!mp[i][j]) mp[i][j] = INF;}}for (int p = 0; p<k + c; p++){for (int i = 0; i<k + c; i++){for (int j = 0; j<k + c; j++)mp[i][j] = min(mp[i][j], mp[i][p] + mp[p][j]);}}int l = 0, r = 50000, ans;while (l <= r){int mid = (l + r) >> 1;if (Dinic_flow(mid) >= c) { ans = mid; r = mid - 1; }else l = mid + 1;}printf("%d\n", ans);}return 0;}
0 0
- POJ2112 Optimal Milking
- poj2112 Optimal Milking
- POJ2112--Optimal Milking
- poj2112 Optimal Milking
- poj2112 Optimal Milking
- POJ2112 Optimal Milking
- 【POJ2112】Optimal Milking
- POJ2112-Optimal Milking
- POJ2112-Optimal Milking
- POJ2112 Optimal Milking
- POJ2112: Optimal Milking 题解
- POJ2112 Optimal Milking
- poj2112——Optimal Milking
- [网络流]poj2112 Optimal Milking
- poj2112 Optimal Milking (Dinic+Floyd+二分)
- poj2112 Optimal Milking --- 最大流,二分
- POJ2112 Optimal Milking 二分法+网络流
- POJ2112 Optimal Milking 【最大流+二分】
- 计划
- 使用TreeSet
- PAT L2-003
- [2017/3/13]阿里面试总结
- linux命令 cat <<- _EOF_ 输出多行文本
- POJ2112-Optimal Milking
- 数组冒泡
- ssh连接慢
- MQTT---HiveMQ源码详解(十七)Cluster-Consistent Hashing Ring & Node Lifecycle
- 使用及自定义泛型
- pycharm 调试 scrapy
- 35. Search Insert Position
- Java元注解
- 解决maven卡在creating maven-archetype-quickstart的问题