[caioj 1117]路径中的最长边最小 --- 二分 + dinic最大流

【题目描述】

千万要注意两个概念:边 和 路径(路径是由多条边组成,当然可以是一条边)
给出N(2 <= N <= 200)个点和P(1 <= P <= 40,000)条双向边,每条边的长度为(0~1 000 000),现在要求选出T(1 <= T <= 200)条“1至N”的路径,任意两条路径上的边不能重复,并且要求这些路径中的最长边的长度最小。注意:两个点之间有可能多条边,出发点是1,终点是N。
【输入格式】
第一行三个整数: N, P, and T 。
下来P行,每行三个整数 x, y, L,描述一条边:从点x到y的双向边,长度为Li。
【输出格式】
求这T条路径中的的最长边的最小值。
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
提示:样例最后选择了两条路径 1 - 2 - 3 - 7 and 1 - 6 - 7.最长的路是5.
数据很多,要用scanf读,用cin会超时

分析

二分长度,建图后跑最大流判断即可。
注意无向边(某些版本的要注意,如我的。)

代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>

#define IL inline
#define open(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define close fclose(stdin); fclose(stdout);
#define INF 0x7f7f7f7f

using namespace std;

inline int read()
{
    char c=getchar();
    int sum=0,k=1;
    for(;'0'>c || c>'9';c=getchar())
        if(c=='-') k=-1;
    for(;'0'<=c && c<='9';c=getchar()) sum=sum*10+c-'0';
    return sum*k;
}

struct Edge
{
    int to, nxt;
    int cap, flow;
    Edge(int v = 0, int nxt_ = 0, int c = 0, int f = 0)
    {
        to = v;
        nxt = nxt_;
        cap = c;
        flow = f;
    }
};
Edge edge[160005];
int last[210];
int cnt;
IL void add(int u, int v, int c)
{
    edge[cnt] = Edge(v, last[u], c, 0); last[u] = cnt++;
    edge[cnt] = Edge(u, last[v], 0, 0); last[v] = cnt++;
}

struct Side
{
    int from, to, w;
}side[40005];

int dep[210];
int cur[210];

int n, m;

IL bool cmp(Side x, Side y)
{
    return x.w < y.w;
}

IL int min_(int x, int y) { return x < y ? x : y; }

IL bool Bfs()
{
    queue<int>q;
    memset(dep, 0, sizeof(dep));
    dep[1] = 1;
    q.push(1);
    for(int t = 1, u; t;)
    {
        u = q.front(); q.pop(); --t;
        for(int i = last[u]; i != -1; i = edge[i].nxt)
        if(!dep[edge[i].to] && edge[i].flow < edge[i].cap)
        {
            q.push(edge[i].to); ++t;
            dep[edge[i].to] = dep[u] + 1;
        }
    }
    return dep[n];
}

IL int Dfs(int u, int a)
{
    if(u == n || !a) return a;
    int flow = 0, f;

    for(int &i = cur[u]; i != -1; i = edge[i].nxt)
    if(dep[edge[i].to] == dep[u] + 1 && (f = Dfs(edge[i].to, min_(a, edge[i].cap - edge[i].flow))) > 0)
    {
        edge[i].flow += f;
        edge[i^1].flow -= f;
        flow += f;
        if( !(a -= f) ) break;
    }
    return flow;
}

IL int check(int k)
{
    memset(last, -1, sizeof(last)); cnt = 0;
    for(int i = 1; i <= m && side[i].w <= k; ++i)
    {
        add(side[i].from, side[i].to, 1);
        add(side[i].to, side[i].from, 1);
    }

    int flow = 0;
    for(; Bfs();)
    {
        for(int i = 1; i <= n; ++i) cur[i] = last[i];
        flow += Dfs(1, INF);
    }
    return flow;
}

int main()
{
    open("1117");

    n = read();
    m = read();
    int t = read(), l = INF, r = -INF;
    for(int i = 1; i <= m; ++i)
    {
        side[i].from = read();
        side[i].to = read();
        side[i].w = read();
        if(side[i].w > r) r = side[i].w;
        if(side[i].w < l) l = side[i].w;
    }
    sort(side + 1, side + m + 1, cmp);

    int ans = -1;
    for(int mid; l <= r;)
    {
        mid = (l + r) >> 1;
        if(check(mid) >= t)
            ans = mid, r = mid - 1;
        else
            l = mid + 1;
    }
    printf("%d\n", ans);

    close;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_27121257/article/details/79398706
今日推荐