码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-08-27 02:13:48      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:

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 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.


好久没有1A了。。。


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


思路:二分枚举所有被选中路径上 最大边权值的最小值mid,判断在mid值的限制下从1到N是否存在至少T条边不重复的路径。

边不重复的路径数目可以用最大流求解——枚举原图中所有无向边,若边权不大于mid,就将该边加进新图且边的容量为1。最后从1到N跑一下最大流就行了。



AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 200+10
#define MAXM 1600000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM], Redge[MAXM];
int head[MAXN], edgenum, Rhead[MAXN], Redgenum;
int dist[MAXN], cur[MAXN];
bool vis[MAXN];
int N, M, T;
struct Node
{
    int from, to, val, next;
};
Node node[MAXM];
int Head[MAXN], nodenum;
void addNode(int u, int v, int w)
{
    Node E = {u, v, w, Head[u]};
    node[nodenum] = E;
    Head[u] = nodenum++;
}
int Max;//记录最大边权
void input()
{
    memset(Head, -1, sizeof(Head));
    nodenum = 0;
    Max = 0;
    int a, b, c;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d%d", &a, &b, &c);
        Max = max(Max, c);//更新
        addNode(a, b, c);
        addNode(b, a, c);
    }
}
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap(int mid)
{
    for(int i = 0; i < nodenum; i++)
    {
        if(node[i].val <= mid)//小于或等于限制
            addEdge(node[i].from, node[i].to, 1);//加入新图
    }
}
bool BFS(int s, int t)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + 1;
                vis[E.to] = true;
                if(E.to == t) return true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int t)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
        {
            edge[i].flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow(int s, int t)
{
    int flow = 0;
    while(BFS(s, t))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(s, INF, t);
    }
    return flow;
}
int main()
{
    while(scanf("%d%d%d", &N, &M, &T) != EOF)
    {
        input();
        int left = 0, right = Max, ans = Max;
        while(right >= left)//二分查找
        {
            int mid = (left + right) >> 1;
            init();
            getMap(mid);
            if(Maxflow(1, N) >= T)//判断是否存在T条边不重复的路径
            {
                ans = min(ans, mid);//更新
                right = mid - 1;
            }
            else
                left = mid + 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}


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

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

标签:

原文地址:http://blog.csdn.net/chenzhenyu123456/article/details/48017165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!