POJ 2455 Secret Milking Machine (二分 + 最大流)
题目大意:
给出一张无向图,找出T条从1..N的路径,互不重复,求走过的所有边中的最大值最小是多少。
算法讨论:
首先最大值最小就提醒我们用二分,每次二分一个最大值,然后重新构图,把那些边权符合要求的边加入新图,在新图上跑网络流。
这题有许多注意的地方:
1、因为是无向图,所以我们在加正向边和反向边的时候,流量都是1,而不是正向边是1,反向边是0。
2、题目中说这样的路径可能不止t条,所以我们在最后二分判定的时候不能写 == t,而是要 >= t。
3、这题很容易T ,表示我T了N遍,弱菜啊。
Codes:
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue> using namespace std; struct Edge{
int from, to, cap, flow;
Edge(int _from = , int _to = , int _cap = , int _flow = ):
from(_from), to(_to), cap(_cap), flow(_flow) {}
}E[ + ]; struct Dinic{
static const int N = + ;
static const int M = + ;
static const int oo = 0x3f3f3f3f; int n, m, s, t;
vector <Edge> edges;
vector <int> G[N];
int dis[N], cur[N];
bool vi[N]; void Clear(){
for(int i = ; i <= n; ++ i) G[i].clear();
edges.clear();
} void Add(int from, int to, int cap, int flow){
edges.push_back((Edge){from, to, cap, });
edges.push_back((Edge){to, from, cap, });
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool bfs(){
for(int i = ; i <= n; ++ i) vi[i] = false;
dis[s] = ; vi[s] = true;
queue <int> q; q.push(s); while(!q.empty()){
int x = q.front(); q.pop();
for(int i = ; i < G[x].size(); ++ i){
Edge &e = edges[G[x][i]];
if(!vi[e.to] && e.cap > e.flow){
vi[e.to] = true;
dis[e.to] = dis[x] + ;
q.push(e.to);
}
}
}
return vi[t];
} int dfs(int x, int a){
if(x == t || a == ) return a;
int flw = , f;
for(int &i = cur[x]; i < G[x].size(); ++ i){
Edge &e = edges[G[x][i]];
if(dis[x] + == dis[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > ){
e.flow += f; edges[G[x][i]^].flow -= f;
a -= f; flw += f;
if(!a) break;
}
}
return flw;
} int Maxflow(int s, int t){
this->s = s;this-> t = t;
int flw = ;
while(bfs()){
memset(cur, , sizeof cur);
flw += dfs(s, oo);
}
return flw;
} }Net; int ns, ps, ts;
int l = 0x3f3f3f3f, r, mid; bool check(int mv){
for(int i = ; i < ps; ++ i){
if(E[i].cap <= mv){
Net.Add(E[i].from, E[i].to, , );
}
}
return Net.Maxflow(, Net.n) >= ts;
} int Solve(){
int ans; while(l <= r){
Net.Clear();
mid = l + (r - l) / ;
if(check(mid)){
ans = mid; r = mid - ;
}
else l = mid + ;
} return ans;
} int main(){
int x, y, z; scanf("%d%d%d", &ns, &ps, &ts);
Net.n = ns;
for(int i = ; i < ps; ++ i){
scanf("%d%d%d", &x, &y, &z);
E[i] = (Edge){x, y, z, };
l = min(l, z); r = max(r, z);
} printf("%d\n", Solve());
return ;
}
POJ 2455
让人更加不能理解的是,为什么我换了上面的邻接表的形式,用STL容器来存邻接表就AC,用数组来存就T成狗。
下面是我狂T的代码,良心网友们给查查错误呗。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; int ns, ps, ts, te;
int l=0x3f3f3f3f, r, Mid; struct Edge{
int from, to, dt;
}e[ + ]; struct Dinic{
static const int maxn = + ;
static const int maxm = + ;
static const int oo = 0x3f3f3f3f; int n,m,s,t;
int tot;
int first[maxn],next[maxm];
int u[maxm],v[maxm],cap[maxm],flow[maxm];
int cur[maxn],dis[maxn];
bool vi[maxn]; Dinic(){tot=;memset(first,-,sizeof first);}
void Clear(){tot = ;memset(first,-,sizeof first);}
void Add(int from,int to,int cp,int flw){
u[tot] = from;v[tot] = to;cap[tot] = cp;flow[tot] = ;
next[tot] = first[u[tot]];
first[u[tot]] = tot;
++ tot;
}
bool bfs(){
memset(vi,false,sizeof vi);
queue <int> q;
dis[s] = ;vi[s] = true;
q.push(s); while(!q.empty()){
int now = q.front();q.pop();
for(int i = first[now];i != -;i = next[i]){
if(!vi[v[i]] && cap[i] > flow[i]){
vi[v[i]] = true;
dis[v[i]] = dis[now] + ;
q.push(v[i]);
}
}
}
return vi[t];
}
int dfs(int x,int a){
if(x == t || a == ) return a;
int flw=,f;
int &i = cur[x];
for(i = first[x];i != -;i = next[i]){
if(dis[x] + == dis[v[i]] && (f = dfs(v[i],min(a,cap[i]-flow[i]))) > ){
flow[i] += f;flow[i^] -= f;
a -= f;flw += f;
if(a == ) break;
}
}
return flw;
}
int MaxFlow(int s,int t){
this->s = s;this->t = t;
int flw=;
while(bfs()){
memset(cur,,sizeof cur);
flw += dfs(s,oo);
}
return flw;
}
}Net; bool check(int mid){
Net.Clear();
for(int i = ; i <= ps; ++ i){
if(e[i].dt <= mid){
Net.Add(e[i].from, e[i].to, , );
Net.Add(e[i].to, e[i].from, , );
}
}
return Net.MaxFlow(, Net.n) >= ts;
}
int Solve(){
int ans;
while(l <= r){
Mid = l + (r - l) / ;
if(check(Mid)){
ans = Mid; r = Mid - ;
}
else l = Mid + ;
}
return ans;
}
int main(){
int x, y, z;
scanf("%d%d%d", &ns, &ps, &ts);
Net.n = ns;te = ;
for(int i = ; i <= ps; ++ i){
scanf("%d%d%d", &x, &y, &z);
++ te;
e[te].from = x;e[te].to = y;e[te].dt = z;
l = min(l, z); r = max(z, r);
}
printf("%d\n", Solve());
return ;
}
POJ 2455 T 版
POJ 2455 Secret Milking Machine (二分 + 最大流)的更多相关文章
- poj 2455 Secret Milking Machine 二分+最大流 sap
题目:p条路,连接n个节点,现在需要从节点1到节点n,不重复走过一条路且走t次,最小化这t次中连接两个节点最长的那条路的值. 分析:二分答案,对于<=二分的值的边建边,跑一次最大流即可. #in ...
- POJ 2455 Secret Milking Machine(最大流+二分)
Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...
- POJ 2455 Secret Milking Machine (二分+无向图最大流)
[题意]n个点的一个无向图,在保证存在T条从1到n的不重复路径(任意一条边都不能重复)的前提下,要使得这t条路上经过的最长路径最短. 之所以把"经过的最长路径最短"划个重点是因为前 ...
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9658 Accepted: ...
- POJ 2455 Secret Milking Machine 【二分】+【最大流】
<题目链接> 题目大意: FJ有N块地,这些地之间有P条双向路,每条路的都有固定的长度l.现在要你找出从第1块地到第n块地的T条不同路径,每条路径上的路段不能与先前的路径重复,问这些路径中 ...
- POJ 2455 - Secret Milking Machine
原题地址:http://poj.org/problem?id=2455 题目大意:给出一个N个点的无向图,中间有P条边,要求找出从1到n的T条通路,满足它们之间没有公共边,并使得这些通路中经过的最长的 ...
- POJ 2112 Optimal Milking(二分+最大流)
http://poj.org/problem?id=2112 题意: 现在有K台挤奶器和C头奶牛,奶牛和挤奶器之间有距离,每台挤奶器每天最多为M头奶挤奶,现在要安排路程,使得C头奶牛所走的路程中的最大 ...
- POJ - 2112 Optimal Milking (dijkstra + 二分 + 最大流Dinic)
(点击此处查看原题) 题目分析 题意:在一个农场中有k台挤奶器和c只奶牛,每个挤奶器最多只能为m只奶牛挤奶,每个挤奶器和奶牛都视为一个点,将编号1~k记为挤奶器的位置,编号k+1~k+c记为奶牛的位置 ...
- POJ 2112 Optimal Milking (Floyd+二分+最大流)
[题意]有K台挤奶机,C头奶牛,在奶牛和机器间有一组长度不同的路,每台机器每天最多能为M头奶牛挤奶.现在要寻找一个方案,安排每头奶牛到某台机器挤奶,使得C头奶牛中走过的路径长度的和的最大值最小. 挺好 ...
随机推荐
- android控制之 adb shell (已完成,不定期增加内容)
第一步:首先,下载adb1.0.32.zip,里面有如下图的内容: 第二步:解压缩,复制Adb.exe,和fastboot.exe到System32,注意AdbWinUsbApi.dll,AdbWin ...
- hdu5672 尺取法
StringTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- 判别linux机器字节序为大端还是小端
代码如下: #include <iostream> #include <arpa/inet.h> #include <cstdio> using namespace ...
- android 开发中判断网络是否连接的代码
在android的开发中,尤其是与访问网络有关的开发,都要判断一下手机是否连接上了网络,下面是一个判断是否连接网络的嗲吗片段: package cn.com.karl.util; import com ...
- 使用 OpenWrt Image Generator 为 WR703N 路由器定制固件
标题:使用 OpenWrt Image Generator 为 WR703N 路由器定制固件 之前试着自己编译固件,编译是成功了,但是在后期安装官方仓库的ipk时出现问题,因为自己编译的固件和官方固件 ...
- un ange frappe a ma porte
Un signe, une larme 魂牵 泪扰 un mot, une arme 字断 情烧 nettoyer les étoiles à l'alcool de mon ame 灵魂之酒眷洗 星 ...
- Bootstrap 实例 - 模态框(Modal)插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 有空可以对C#尝一下鲜,WCF看上去很诱人(跨进程、跨机器、跨子网,跨企业网乃至跨Internet的分布式服务)
说道底不还是要借助NGNIX实现,PHP自身呢?C#的WCF可以脱离IIS就可以实现跨进程.跨机器.跨子网,跨企业网乃至跨Internet的分布式服务,宿主可以是IIS,WinForm,WPF, Wi ...
- Windows PowerShell是啥?看完本文你就懂它了
这篇文章主要介绍了Windows PowerShell是啥?Windows PowerShell是什么?Windows PowerShell有哪些特性?Windows PowerShell有什么用?看 ...
- boost库使用:仿SGI-STL实现的一个树节点allocator
////////////////////////////////////////////////////////////////////////// //code by hzs //email: hu ...