bzoj2561 最小生成树

2561: 最小生成树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2244  Solved: 1070
[Submit][Status][Discuss]

Description

 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最少多少条边,才能够使得这条边既可能出现在最小生成树上,也可能出现在最大生成树上?

 

Input

  第一行包含用空格隔开的两个整数,分别为N和M;
  接下来M行,每行包含三个正整数u,v和w表示图G存在一条边权为w的边(u,v)。
  最后一行包含用空格隔开的三个整数,分别为u,v,和 L;
  数据保证图中没有自环。
 

Output

 输出一行一个整数表示最少需要删掉的边的数量。

Sample Input

3 2
3 2 1
1 2 3
1 2 2

Sample Output

1

HINT

 

对于20%的数据满足N ≤ 10,M ≤ 20,L ≤ 20;

  对于50%的数据满足N ≤ 300,M ≤ 3000,L ≤ 200;

  对于100%的数据满足N ≤ 20000,M ≤ 200000,L ≤ 20000。

 

Source

2012国家集训队Round 1 day1

分析:老套路,考虑克鲁斯卡尔算法的流程.

         如果(u,v,l)这条边在最小生成树上,那么在加入这条边的时候,u,v一定是不连通的.那么将权值小于l的边给连起来,接下来的任务就是使得删去权值最小的边使得构出的图不连通.显然的最小割,跑一边dinic算法.对于最大生成树也这样做一遍,两次的答案相加就是问题的答案.

         边数组开小了,结果迷之TLE.这种无向边,网络流的题建图要开4倍的边数组!

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

using namespace std;

const int maxn = 400010,inf = 0x7fffffff;

int n,m,u,v,l,head[20010],to[maxn * 3],nextt[maxn * 3],tot = 2,w[maxn * 3],dist[20010],ans;

struct node
{
    int u,v,w;
} e[maxn];

bool cmp(node a,node b)
{
    return a.w < b.w;
}

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;

    w[tot] = 0;
    to[tot] = x;
    nextt[tot] = head[y];
    head[y] = tot++;
}

bool bfs()
{
    memset(dist,-1,sizeof(dist));
    dist[u] = 0;
    queue <int> q;
    q.push(u);
    while (!q.empty())
    {
        int uu = q.front();
        q.pop();
        if (uu == v)
            return true;
        for (int i = head[uu]; i; i = nextt[i])
        {
            int vv = to[i];
            if (w[i] && dist[vv] == -1)
            {
                dist[vv] = dist[uu] + 1;
                q.push(vv);
            }
        }
    }
    if (dist[v] == -1)
        return false;
    return true;
}

int dfs(int x,int flow)
{
    if (x == v)
        return flow;
    int res = 0;
    for (int i = head[x]; i; i = nextt[i])
    {
        int vv = to[i];
        if (w[i] && dist[vv] == dist[x] + 1)
        {
            int temp = dfs(vv,min(flow - res,w[i]));
            w[i] -= temp;
            w[i ^ 1] += temp;
            res += temp;
            if (res == flow)
                return res;
        }
    }
    if (res == 0)
        dist[x] = -1;
    return res;
}

void solve1()
{
    for (int i = 1; i <= m; i++)
    {
        if (e[i].w >= l)
            break;
        add(e[i].u,e[i].v,1);
        add(e[i].v,e[i].u,1);
    }
    while (bfs())
        ans += dfs(u,inf);
}

void solve2()
{
    tot = 2;
    memset(head,0,sizeof(head));
    for (int i = m; i >= 1; i--)
    {
        if (e[i].w <= l)
            break;
        add(e[i].u,e[i].v,1);
        add(e[i].v,e[i].u,1);
    }
    while (bfs())
        ans += dfs(u,inf);
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i = 1; i <= m; i++)
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    scanf("%d%d%d",&u,&v,&l);
    sort(e + 1,e + 1 + m,cmp);
    solve1();
    solve2();
    printf("%d\n",ans);

    return 0;
}

 

posted @ 2018-01-25 17:47  zbtrs  阅读(170)  评论(0编辑  收藏  举报