POJ 2455--Secret Milking Machine【二分枚举 && 最大流 && 经典】

Secret Milking Machine

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10625   Accepted: 3111

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.

Sample Input

7 9 2
1 2 2
2 3 5
3 7 5
1 4 1
4 3 1
4 5 7
5 7 1
1 6 3
6 7 3

Sample Output

5

Hint

Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.

Huge input data,scanf is recommended.

题意:

题意:给你一个N个点(编号从1到N)和M条边的无向图以及每条边的权值。要求从1到N至少要有T条边不重复的路径,让你在满足这个前提下求出所有路径(当然是选出的那些边不重复的路径,不算没有选上的)上的最大边权值的 最小值。

解析:一看到最大值的最小化问题就想到用二分做,题目保证从1 到 N有 T 条无相同道路的路径,即每条边只能用一次,每个点可以多次经过。边不重复的路径数目可以用最大流求解。二分枚举最大边权值mid ,再枚举所有的边,若边的权值 <= mid, 加进网络流中,且容量为1,,然后跑最大流,看看最大流是不是>= T,符合的话取更新最大权值的最小值,取最小值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 220
#define maxm 2000000
#define INF 0x3f3f3f3f
using namespace std;
int N, M, T;

struct NODE{
    int u, v, w, next;
};

NODE map[maxm];
int head1[maxn], cnt1;

void initmap(){
    cnt1 = 0;
    memset(head1, -1, sizeof(head1));
}

void addmap(int u ,int  v, int w){
    map[cnt1].u = u;
    map[cnt1].v = v;
    map[cnt1].w = w;
    map[cnt1].next = head1[u];
    head1[u] = cnt1++;
}
int maxs = 0;
void input(){
    initmap();
    while(M--){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        maxs = max(maxs, c);
        addmap(a, b, c);
        addmap(b, a, c);
    }
}

struct node{
    int u, v, cap, flow, next;
};

node edge[maxm];
int head[maxn], cnt, cur[maxn];
int vis[maxn], dist[maxn];

void initedge(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int w){
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cap = w;
    edge[cnt].flow = 0;
    edge[cnt].next = head[u];
    head[u] = cnt++;
    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cap = 0;
    edge[cnt].flow = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}

void getmap(int ans){
    for(int i = 0; i < cnt1; ++i)
        if(map[i].w <= ans)
        addedge(map[i].u, map[i].v, 1);
}

bool BFS(int st ,int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed)
                    return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0)
                break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st,ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}

int main (){
    while(scanf("%d%d%d", &N, &M, &T) != EOF){
        input();
        int l = 0, r = maxs, mid;
        int max_min = maxs;
        while(r > l){
            mid  = (l + r) / 2;
            initedge();
            getmap(mid);
            if(maxflow(1, N) >= T){
                max_min = min(max_min, mid);
                r = mid;
            }
            else
                l = mid + 1;
        }
        printf("%d\n", max_min);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 17:13:20

POJ 2455--Secret Milking Machine【二分枚举 && 最大流 && 经典】的相关文章

POJ 2455 Secret Milking Machine (二分 + 最大流)

题目大意: 给出一张无向图,找出T条从1..N的路径,互不重复,求走过的所有边中的最大值最小是多少. 算法讨论: 首先最大值最小就提醒我们用二分,每次二分一个最大值,然后重新构图,把那些边权符合要求的边加入新图,在新图上跑网络流. 这题有许多注意的地方: 1.因为是无向图,所以我们在加正向边和反向边的时候,流量都是1,而不是正向边是1,反向边是0. 2.题目中说这样的路径可能不止t条,所以我们在最后二分判定的时候不能写 == t,而是要 >= t. 3.这题很容易T ,表示我T了N遍,弱菜啊.

POJ 2455 Secret Milking Machine(二分+最大流)

POJ 2455 Secret Milking Machine 题目链接 题意:一个无向图,要求有T条不重复道路可以从1走到t,问道路中最大边的最小值可以是多少 思路:二分+最大流,二分长度,连起边,注意是无向图,所以反向边是有容量的,然后源点和1连容量t,n和汇点连容量是t 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespa

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 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

poj 2455 Secret Milking Machine 【二分 + 最大流】 【1到N不重复路径不少于T条时,求被选中路径上的最大边权值 的最小值】

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10620   Accepted: 3110 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

poj 2455 Secret Milking Machine

[题意]:(半天没懂题目什么意思,诶..)FJ有N块地,这些地之间有P条双向路,每条路的都有固定的长度l.现在要你找出从第1块地到第n块地的T条不同路 径,每条路径上的路不能与先前的路径重复,问这些路径中的最长路的最小是多少. [思路]二分判定最长路的最小值,再用小于这个值的边建图跑最大流.建图的话,整张无向图(记得是无向图! 对一条边正向方向都要添加wa 好久  )加一个源点和点1 连边容量无穷大,n点和会汇点连边,容量无穷大,最大流是多少能够找到的不同的路径就是多少. 1 #include<

【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

题目描述 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 <=

POJ 2455-Secret Milking Machine(网络流_最大流+二分查找)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10119   Accepted: 2973 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

[bzoj1733][Usaco2005 feb]Secret Milking Machine 神秘的挤奶机_网络流

[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 题目大意:约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度L.两块区域之间可能有多条

POJ 2112 Optimal Milking (二分 + floyd + 网络流)

POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C≤200)头奶牛,在奶牛和挤奶器之间有一组不同长度的路.K个挤奶器的位置用1-K的编号标明,奶牛的位置用K+1-K+C 的编号标明.每台挤奶器每天最多能为M(1≤M≤15)头奶牛挤奶.寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小.每个测试数据中至少有一