IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    poj 2455 Secret Milking Machine——图的边连通度,二分边值求最大流

    火碳黑发表于 2011-04-21 13:47:00
    love 0
    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.

    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.
    题意:fj想从一个点a到另外一个点b,在每条边只走一次的情况下,走k次从a到b,使得经过的边最大的尽量小。
    分析:又是二分,这个题就是图的边连通度,其每条通过流量的路径被称为弱独立轨。原图中u->v,如果是有向图,则从u到v一条容量为1的边。如果是无向图,则从u到v一条容量为1的边,从v到u一条容量为1的边。这样保证每条边只经过一个流量,而每个点可以被通关过多次,只要这个点有多个入度跟出度。就这样了,最大流。
    #include <stdio.h>
    #include
    <string.h>
    #include
    <algorithm>
    #include
    <iostream>
    #define Min(a, b) (a) < (b) ? a : b
    #define Max(a, b) (a) > (b) ? a : b
    using namespace std;
    const int MAXN = 1005;
    const int MAXM = 210000;
    const int INF = 1100000000;
    struct Edge
    {
    int st, ed;
    int next;
    int flow;
    int cap;
    }edge[MAXM];
    int head[MAXN], level[MAXN], que[MAXN], E;
    struct List
    {
    int u, v, w;
    }list[
    40000];
    void add(int u, int v, int w)
    {
    //printf("add %d %d %d\n", u, v, w);
    edge[E].flow = 0;
    edge[E].cap
    = w;
    edge[E].st
    = u;
    edge[E].ed
    = v;
    edge[E].next
    = head[u];
    head[u]
    = E++;
    edge[E].flow
    = 0;
    edge[E].cap
    = 0;
    edge[E].st
    = v;
    edge[E].ed
    = u;
    edge[E].next
    = head[v];
    head[v]
    = E++;
    }
    int dinic_bfs(int src, int dest, int ver)
    {
    int i, j;
    for (i = 0; i <= ver; i++)
    {
    level[i]
    = -1;
    }
    int rear = 1;
    que[
    0] = src; level[src] = 0;
    for(i = 0; i < rear; i++)
    {
    for(j = head[que[i]]; j != -1; j = edge[j].next)
    {
    if(level[edge[j].ed] == -1 && edge[j].cap > edge[j].flow)
    {
    level[edge[j].ed]
    = level[que[i]]+1;
    que[rear
    ++] = edge[j].ed;
    }
    }
    }
    return level[dest] >= 0;
    }

    int dinic_dfs(int src, int dest, int ver)
    {
    int stk[MAXN], top = 0;
    int ret = 0, cur, ptr, pre[MAXN], minf, i;
    int del[MAXN];
    for (i = 0; i <= ver; i++)
    {
    del[i]
    = 0;
    }
    stk[top
    ++] = src;
    pre[src]
    = src;
    cur
    = src;
    while(top)
    {
    while(cur != dest && top)
    {
    for(i = head[cur]; i != -1; i = edge[i].next)
    {
    if(level[edge[i].ed] == level[cur] + 1 && edge[i].cap > edge[i].flow && !del[edge[i].ed])
    {
    stk[top
    ++] = edge[i].ed;
    cur
    = edge[i].ed;
    pre[edge[i].ed]
    = i;
    break;
    }
    }
    if(i == -1)
    {
    del[cur]
    = 1;
    top
    --;
    if(top) cur = stk[top-1];
    }
    }
    if(cur == dest)
    {
    minf
    = INF;
    while(cur != src)
    {
    cur
    = pre[cur];
    if(edge[cur].cap - edge[cur].flow < minf) minf = edge[cur].cap - edge[cur].flow;
    cur
    = edge[cur].st;
    }
    cur
    = dest;
    while(cur != src)
    {
    cur
    = pre[cur];
    edge[cur].flow
    += minf;
    edge[cur
    ^1].flow -= minf;
    if(edge[cur].cap - edge[cur].flow == 0)
    {
    ptr
    = edge[cur].st;
    }
    cur
    = edge[cur].st;
    }
    while(top > 0&& stk[top-1] != ptr) top--;
    if(top) cur = stk[top-1];
    ret
    += minf;
    }
    }
    return ret;
    }
    int Dinic(int src, int dest, int ver)
    {
    int ret = 0, t;
    while(dinic_bfs(src, dest, ver))
    {
    t
    = dinic_dfs(src, dest, ver);
    if(t) ret += t;
    else break;
    }
    return ret;
    }
    void build (int length, int n, int m)
    {
    E
    = 0;
    int i, u, v, w;
    int s = 0, t = n + 1, ver = t + 1;
    for (i = 0; i <= ver; i++)
    {
    head[i]
    = -1;
    }
    for (i = 0; i < m; i++)
    {
    u
    = list[i].u, v = list[i].v, w = list[i].w;
    if (w <= length)
    {
    add(u, v,
    1);
    add(v, u,
    1);
    }
    }
    add(s,
    1, INF);
    add(n, t, INF);
    }
    int main()
    {
    int n, m, k, i, max, s, t, ver, l, r, mid, flow;
    scanf(
    "%d%d%d", &n, &m, &k);
    for (i = 0, max = 0; i < m; i++)
    {
    scanf(
    "%d%d%d", &list[i].u, &list[i].v, &list[i].w);
    max
    = Max(max, list[i].w);
    }
    s
    = 0, t = n + 1, ver = t + 1;
    l
    = 0, r = max + 1;
    while (l < r)
    {
    mid
    = (l + r) >> 1;
    build(mid, n, m);
    flow
    = Dinic(s, t, ver);
    if (flow >= k)
    {
    r
    = mid;
    }
    else
    {
    l
    = mid + 1;
    }
    }
    printf(
    "%d\n", r);
    return 0;
    }
    /*

    */




    火碳黑 2011-04-21 21:47 发表评论


沪ICP备19023445号-2号
友情链接