BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案
Description
Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to the machine during its construction. He has a secret tunnel that he uses only for the return trips. The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks. To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails. Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.) It is guaranteed that FJ can make all T trips without reusing a trail.
Input
* Line 1: Three space-separated integers: N, P, and T * Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.
Output
* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John's route.
可以二分这个最长长度的上限 $mid$,每次只加入长度小于等于 $mid$ 的边
剩下的问题就是要找到 $T$ 个互不重叠的 '环'
跑一个最大流就好了,若流量大于等于 $T$,则符合要求
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define ll long long
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100000
#define inf 230
using namespace std;
int n,m,cc,S,T;
struct Dinic {
struct Edge {
int from,to,cap;
Edge(int from=0,int to=0,int cap=0):from(from),to(to),cap(cap){}
};
queue<int>Q;
vector<Edge>edges;
vector<int>G[300];
int current[300],d[300],vis[300];
void addedge(int u,int v,int c) {
edges.push_back(Edge(u,v,c));
edges.push_back(Edge(v,u,0));
int o=edges.size();
G[u].push_back(o-2);
G[v].push_back(o-1);
}
int BFS() {
memset(vis,0,sizeof(vis)), memset(d,0,sizeof(d));
vis[S]=1,d[S]=0;
Q.push(S);
while(!Q.empty()) {
int u=Q.front(); Q.pop();
for(int i=0;i<G[u].size();++i) {
Edge e=edges[G[u][i]];
if(e.cap>0 && !vis[e.to]) {
vis[e.to]=1, d[e.to]=d[u]+1;
Q.push(e.to);
}
}
}
return vis[T];
}
int dfs(int x,int cur) {
if(x==T) return cur;
int f,flow=0;
for(int i=current[x];i<G[x].size();++i) {
current[x]=i;
Edge e=edges[G[x][i]];
if(d[e.to]==d[x]+1 && e.cap > 0) {
f=dfs(e.to,min(cur, e.cap));
if(f) {
cur-=f, flow+=f, edges[G[x][i]].cap-=f, edges[G[x][i]^1].cap+=f;
if(cur<=0) return flow;
}
}
}
return flow;
}
int maxflow() {
int re=0;
while(BFS()) memset(current,0,sizeof(current)),re+=dfs(S,inf);
return re;
}
void re() {
edges.clear();
for(int i=0;i<300;++i) G[i].clear();
}
}net;
struct Node {
int u,v,c;
}nd[maxn];
bool cmp(Node a,Node b) {
return a.c < b.c;
}
int check(int t) {
net.re();
for(int i=1;i<=t;++i) {
net.addedge(nd[i].u,nd[i].v,1);
net.addedge(nd[i].v,nd[i].u,1);
}
return net.maxflow() >= cc;
}
int main() {
// setIO("input");
scanf("%d%d%d",&n,&m,&cc);
for(int i=1;i<=m;++i) scanf("%d%d%d",&nd[i].u,&nd[i].v,&nd[i].c);
sort(nd+1,nd+1+m,cmp);
int l=1,r=m,mid,ans=0;
S=1,T=n;
while(l<=r) {
mid=(l+r)>>1;
if(check(mid)) ans=mid, r=mid-1;
else l=mid+1;
}
printf("%d\n",nd[ans].c);
return 0;
}
BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案的更多相关文章
- BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
Description 约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T ...
- [bzoj1733][Usaco2005 feb]Secret Milking Machine 神秘的挤奶机_网络流
[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 题目大意:约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农 ...
- 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流
题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possi ...
- [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】
题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...
- BZOJ1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
n<=200个点m<=40000条边无向图,求 t次走不经过同条边的路径从1到n的经过的边的最大值 的最小值. 最大值最小--二分,t次不重边路径--边权1的最大流. #inclu ...
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9658 Accepted: ...
- POJ2455 Secret Milking Machine
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12324 Accepted ...
- POJ 2455 Secret Milking Machine(最大流+二分)
Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
随机推荐
- 常用获取Android崩溃日志和IOS崩溃日志的几种方法
一:前言 在日常测试app时,经常会遇到崩溃问题,测试快速抓取到崩溃日志可以有效方便开发进行定位,快速解决问题所在测试做到测试分析,定位是非常重要的,这也是判断一个测试能力指标的一大维度. 二:And ...
- Scratch少儿编程系列:(一)版本的选择及安装
工欲善其事必先利其器,为了使用Scratch,首先要到官网上下载相关软件. 官网链接地址为:https://scratch.mit.edu/download,我用的是Windows系统,下载对应的安装 ...
- 成功秀了一波scala spark ML逻辑斯蒂回归
1.直接上官方代码,调整过的,方可使用 package com.test import org.apache.spark.{SparkConf, SparkContext} import org.ap ...
- [LeetCode] 1092. Shortest Common Supersequence
LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...
- 基于weui loading插件封装
<!-- Loading.vue --> <template> <div id="loadingToast" v-if="show" ...
- 20191128 Spring Boot官方文档学习(9.9)
9.9.数据存取 Spring Boot包含许多用于处理数据源的启动器. 9.9.1.配置自定义数据源 要配置自己的DataSource,请在配置中定义该类型的@Bean.Spring Boot可以在 ...
- 使用itchat获取微信好友的男女比例
# 使用itchat获取微信好友的男女比例 import itchat itchat.auto_login(hotReload=True) friends = itchat.get_friends(u ...
- gcc 消除未使用变量的警告
我们写代码的时候经常需要遇到一些情况,参数暂时没用到.但是这个参数必须存在. 例如linux下线程实体函数void *thread_xx(void *arg)如果不处理,gcc编译时就会报" ...
- Spring Boot 2.2.0 正式发布,支持 JDK 13!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 推荐阅读: Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Centr ...
- Qt 遍历不规则树的节点
在使用Qt的GraphicsScene作图时,遇到类似这样的需求:在scene中创建节点类似下图, 现在我要把每个节点的txt保存到xml文件中,结构为 <?xml version='1.0' ...