Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 19347 | Accepted: 6907 | |
| Case 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. 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. Output A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
Sample Input 2 3 2 Sample Output 2 Source |
||||||
求行走距离的最远的奶牛的至少要走多远。
注意要先用Floyd求每两点之间的最短路。。。。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = , INF = 0x3f3f3f3f;
typedef long long LL; int head[maxn], d[maxn], vis[maxn], p[maxn], f[maxn], way[][];
int n, m, s, t, neng;
int cnt, flow, value; struct node{
int u, v, c, w, next;
}Node[]; void add_(int u, int v, int c, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].w = w;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c, int w)
{
add_(u, v, c, w);
add_(v, u, , -w);
} int spfa()
{
queue<int> Q;
for(int i=; i<maxn; i++) d[i] = INF;
d[s] = ;
mem(vis, );
mem(p, -);
Q.push(s);
vis[s] = ;
p[s] = ; f[s] = INF;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] > max(d[e.u], e.w) && e.c > )
{
d[e.v] = max(d[e.u], e.w);
p[e.v] = i;
f[e.v] = min(f[u], e.c);
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
}
}
}
}
if(p[t] == -) return ;
// cout<< value <<endl;
flow += f[t], value = d[t];
for(int i=t; i!=s; i=Node[p[i]].u)
{
Node[p[i]].c -= f[t];
Node[p[i]^].c += f[t];
}
return ;
} void max_flow()
{
while(spfa());
printf("%d\n",value);
} int main()
{
mem(head, -);
mem(way, INF);
cnt = ;
scanf("%d%d%d", &n, &m, &neng);
for(int i=; i<=n+m; i++)
way[i][i] = ; for(int i=; i<=n+m; i++)
{
for(int j=; j<=n+m; j++)
{
int w;
scanf("%d",&w);
if(w) way[i][j] = w; }
}
for(int k=;k<=n+m;k++)
for(int i=;i<=n+m;i++)
for(int j=;j<=n+m;j++)
way[i][j]=min(way[i][j],way[i][k]+way[k][j]);
for(int i=; i<=m; i++)
for(int j=; j<=n; j++)
if(way[n+i][j] < INF)
add(n+i, j, , way[n+i][j]); s = ; t = n + m + ;
for(int i=; i<=m; i++)
add(s, n+i, , );
for(int j=; j<=n; j++)
add(j, t, neng, );
max_flow(); return ;
}
Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)的更多相关文章
- N - Optimal Milking - POJ 2112(二分图多重匹配+Floyd+二分搜索)
题意:有K太挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 分析:应该先使用floyd求出来点之间的最短路??(不晓得给 ...
- POJ 2195:Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ 2195 Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意 : N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 3422 Kaka's Matrix Travels (最小费用最大流)
POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- poj 3422(最小费用最大流)
题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...
随机推荐
- poj 1932 XYZZY(spfa最长路+判断正环+floyd求传递闭包)
XYZZY Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4154 Accepted: 1185 Description ...
- js 函数作为参数+接受任意数量参数
javascript中的函数是“复合数据类型”,又成为“引用类型”.引用类型的变量指向存储单元中存放的是它们的实际存放地址.函数名是对函数的一种引用.var a=max_num ;a()就可以调用fu ...
- 对寄存器ESP和EBP的一些理解
PS:EBP是当前函数的存取指针.即存储或者读取数时的指针基地址:ESP就是当前函数的栈顶指针. 每一次发生函数的调用(主函数调用子函数)时,在被调用函数初始时,都会把当前函数(主函数)的EBP压栈, ...
- 大数据入门第十七天——storm上游数据源 之kafka详解(二)常用命令
一.kafka常用命令 1.创建topic bin/kafka-topics. --replication-factor --zookeeper mini1: // 如果配置了PATH可以省略相关命令 ...
- javaScript之jQuery框架
一.jQuery简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨 ...
- 20155328 《网络攻防》 实验一:PC平台逆向破解(5)M
20155328 <网络攻防> 实验一:PC平台逆向破解(5)M 实践目标 实践对象:linux可执行文件pwn1. 正常执行时,main调用foo函数,foo函数会简单回显任何用户输入的 ...
- 【转】CentOS 5 上安装git
转自 http://www.cnblogs.com/Neddy/archive/2011/02/28/1967548.html 注意安装的时候 都要以root身份 //先安装git依赖的包 yum i ...
- C++ 对引用的深入理解
观看了唐老师讲解的一节<第5课 - 引用的本质分析>感觉非常不错,有深度不废话,我喜欢--- 再此总结下,并且奉上视频下载地址--- 360网盘下载地址: https://yunpan.c ...
- Windows下面的常用的快捷键
最小化的快捷键: 最小化当前窗口:Alt+ESC 还原刚刚最小化的窗口:Alt+Tab(次快捷键组合可以在多个窗口中切换) 显示桌面,切换之前的桌面:Win+D 在浏览器页面之间切换:Ctrl+T ...
- libgdx学习记录8——对话框Dialog
Dialog在游戏中也很常用,尤其在设置.退出.商店.暂停等画面.Dialog的使用也可以通过skin实现,也可以自定义. 下面是一个简单的实例: package com.fxb.newtest; i ...