题目链接 :

       【POJ】点击打开链接

       【caioj】点击打开链接

算法 :

1:跑一遍弗洛伊德,求出点与点之间的最短路径

2:二分答案,二分”最大值最小“

3.1:建边,将原点与每头奶牛连边,流量为1,记dist[i][j]为i到j的最短路径,若dist[i][j]<=mid (K+1<=i<=K+C,1<=j<=K),则将i与j连边,流量为M,将每台挤奶机与汇点连边,流量为1

3.2 : 跑网络流,这里笔者使用的是dinic算法

3.3 : 判断最大流S是否等于K,等于K,则往小搜,否则往大搜

代码 :

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXK 30
#define MAXC 200
#define MAXM 15 typedef long long LL; LL i,j,low,high,mid,st,ed,K,C,M,tot,ans;
LL h[MAXK+MAXC+],dist[MAXK+MAXC+][MAXK+MAXC+],
U[MAXC*+],V[MAXC*+],W[MAXC*+],Head[MAXC*+],
Next[MAXC*+],other[MAXC*+]; template <typename T> inline void read(T &x) {
LL f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c=='-') f = -f; }
for (; isdigit(c); c = getchar()) x=x*+c-'';
x*=f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x % + '');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void floyed() {
LL i,j,k;
for (k = ; k <= K + C; k++) {
for (i = ; i <= K + C; i++) {
if (i == k) continue;
for (j = ; j <= K + C; j++) {
if ((k == j) || (i == j)) continue;
dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
} inline void add(LL a,LL b,LL c) {
++tot;
U[tot] = a; V[tot] = b; W[tot] = c;
Next[tot] = Head[a]; Head[a] = tot;
other[tot] = ++tot;
U[tot] = b; V[tot] = a; W[tot] = ;
Next[tot] = Head[b]; Head[b] = tot;
other[tot] = tot - ;
} inline bool BFS() {
LL i,x,y;
queue<LL> q;
memset(h,,sizeof(h));
h[st] = ; q.push(st);
while (!q.empty()) {
x = q.front(); q.pop();
for (i = Head[x]; i; i = Next[i]) {
y = V[i];
if ((W[i] > ) && (!h[y])) {
h[y] = h[x] + ;
q.push(y);
}
}
}
if (h[ed]) return true;
else return false;
} inline LL maxflow(LL x,LL f) {
LL i,t,y,sum=;
if (x == ed) return f;
for (i = Head[x]; i; i = Next[i]) {
y = V[i];
if ((W[i] > ) && (h[y] == h[x] + ) && (sum < f)) {
sum += (t = maxflow(y,min(W[i],f-sum)));
W[i] -= t; W[other[i]] += t;
}
}
if (!sum) h[x] = ;
return sum;
} inline bool check(LL ml) {
LL i,j,sum=;
tot = ;
memset(Head,,sizeof(Head));
for (i = K + ; i <= K + C; i++) {
for (j = ; j <= K; j++) {
if (dist[i][j] <= ml)
add(i,j,);
}
}
for (i = K + ; i <= K + C; i++) add(st,i,);
for (i = ; i <= K; i++) add(i,ed,M);
while (BFS()) {
sum += maxflow(st,C);
}
return sum == C;
} int main() { read(K); read(C); read(M);
st = K + C + ; ed = st + ; for (i = ; i <= K + C; i++) {
for (j = ; j <= K + C; j++) {
read(dist[i][j]);
if (!dist[i][j]) dist[i][j] = 2e9;
}
} floyed(); for (i = K + ; i <= K + C; i++) {
for (j = ; j <= K; j++) {
if (dist[i][j] != 2e9)
high = max(high,dist[i][j]);
}
} low = ; while (low <= high) {
mid = (low + high) >> ;
if (check(mid)) {
high = mid - ;
ans = mid;
} else
low = mid + ;
}
writeln(ans); return ; }

【USACO】Optimal Milking的更多相关文章

  1. 【poj2122】 Optimal Milking

    http://poj.org/problem?id=2112 (题目链接) 题意 有K个能挤M头奶牛的挤奶机和C头奶牛,告诉一些挤奶机和奶牛间距离,求最优分配方案使最大距离最小. Solution 先 ...

  2. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  3. 1642: 【USACO】Payback(还债)

    1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...

  4. 1519: 【USACO】超级书架

    1519: [USACO]超级书架 时间限制: 1 Sec 内存限制: 64 MB 提交: 1735 解决: 891 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 Farmer Jo ...

  5. Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers

    [USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...

  6. 【CPLUSOJ】【USACO】【差分约束】排队(layout)

    [题目描述] Robin喜欢将他的奶牛们排成一队.假设他有N头奶牛,编号为1至N.这些奶牛按照编号大小排列,并且由于它们都很想早点吃饭,于是就很可能出现多头奶牛挤在同一位置的情况(也就是说,如果我们认 ...

  7. 【USACO】Dining

    [题目链接] [JZXX]点击打开链接 [caioj]点击打开链接 [算法] 拆点+网络流 [代码] #include<bits/stdc++.h> using namespace std ...

  8. 【USACO】 Balanced Photo

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...

  9. 【USACO】 Balanced Lineup

    [题目链接] 点击打开链接 [算法] 这是一道经典的最值查询(RMQ)问题. 我们首先想到线段树.但有没有更快的方法呢?对于这类问题,我们可以用ST表(稀疏表)算法求解. 稀疏表算法.其实也是一种动态 ...

随机推荐

  1. mac下报错 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    如题mac下遇到错误: 解决办法:安装mac的命令行工具CommandLineTools xcode-select --install

  2. Free Pascal 的安装

    Free Pascal 的安装 https://www.cnblogs.com/cnssc/p/6110492.html https://wenku.baidu.com/view/ee80cc8eed ...

  3. Odoo电子数据交换(EDI)

    Odoo EDI功能能在odoo实例之间交换数据,可以交换哪些数据呢? 默认支持: account.invoice 发票,含发票行 res.currency res.partner purchase. ...

  4. Heap &amp; Priority Queue

    Heap & Priority Queue Definition & Description: In computer science/data structures, a prior ...

  5. Linux问题,磁盘分区打不开了

    Metadata kept in Windows cache, refused to mount. chkdsk /f http://www.bubuko.com/infodetail-1184937 ...

  6. Memory-mapped I/O vs port-mapped I/O

    关于MMIO和PIO,我看到的解释最清楚的文章,原文在这里:Memory-mapped I/O vs port-mapped I/O - 2015 Microprocessors normally u ...

  7. Jenkins 搭建

    持续集成(CI continuous integration) 可以做什么? 自动构建.定时触发,或由某个事件触发.比如可以做 daily build,或每次代码提交时触发.这样可以最早发现代码编译和 ...

  8. 【转】IDA远程调试 The debugger could not attach to the selected process. irs_recv 等待的操作过时

    IDA连接android_server 选中进程点ok之后 连接不上报错 The debugger could not attach to the selected process. This can ...

  9. 零基础学python-2.18 异常

    这一节说一下异常except 继续沿用上一节的代码.我有益把文件名称字搞错.然后在结尾部分加上异常捕捉: try: handler=open("12.txt")#在这里我特别将文件 ...

  10. C# wince 实现软件忙鼠标状态改变

    eg: Cursor.Current = Cursors.WaitCursor; dosomething(); Cursor.Current = Cursors.Default; Cursor.Cur ...