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头奶牛中走过的路径长度的和的最大值最小. 挺好 ...
随机推荐
- 翻译一篇SpiderMonkey GC的文章
前言 这篇文章包含了对SpiderMonkey中Rooted<T>, Handle<T>的解释. 翻译自 https://developer.mozilla.org/en-US ...
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...
- open_basedir restriction in effect. File() is not within the allowed path(s)
目前发现eaccelerator安装之后如果php.ini中设置open_basedir将导致open_basedir的一些报错(open_basedir restriction in effect. ...
- kafka相关应用
一.kafka官网地址 http://kafka.apache.org 下载地址: http://kafka.apache.org/downloads.html 二.版本 0.9.0.1 is the ...
- Labeling Balls--poj3687
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12273 Accepted: 3516 D ...
- 单片微机原理P3:80C51外部拓展系统
外部拓展其实是个相对来说很好玩的章节,可以真正开始用单片机写程序了,比较重要的是外部存储器拓展,81C55拓展,矩阵键盘,动态显示,DAC和ADC. 0. IO接口电路概念与存储器拓展 1. 为什 ...
- [bzoj1002][FJOI2007 轮状病毒] (生成树计数+递推+高精度)
Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...
- Core Data数据持久性存储基础教程-备用
摘要 就像我一直说的,Core Data是iOS编程,乃至Mac编程中使用持久性数据存储的最佳方式,本质上来说,Core Data使用的就是SQLite,但是通过一系列特性避免了使用SQL的一些列的麻 ...
- Winform(C#)限制程序只运行一个实例
C#控制只运行开启一个程序 在这个例子中不需要调用ReleaseMutex,mutex会在程序结束时自动释放.为了防止mutex过早释放,在程序的最后调用下GC.KeepAlive (mutex). ...
- VS代码生成工具ReSharper使用手册:配置快捷键
原文 http://www.cnblogs.com/PHPIDE/archive/2013/05/16/3081783.html VS代码生成工具ReSharper提供了丰富的快捷键,可以极大地提高你 ...