目录

P4180-[BJWC2010]严格次小生成树

P4180-[BJWC2010]严格次小生成树

题目:

题目描述:

小 C 最近学了很多最小生成树的算法,Prim 算法、Kurskal 算法、消圈算法等等。正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了。小 P 说,让小 C 求出一个无向图的次小生成树,而且这个次小生成树还得是严格次小的,也就是说:如果最小生成树选择的边集是 EM,严格次小生成树选择的边集是 ES,那么需要满足:(value(e)表示边 e 的权值) $\sum_{e \in E_M}value(e)<\sum_{e \in E_S}value(e)$

这下小 C 蒙了,他找到了你,希望你帮他解决这个问题。

输入格式:

第一行包含两个整数 N 和 M,表示无向图的点数与边数。 接下来 M 行,每行 3 个数 x y z 表示,点 x 和点 y 之间有一条边,边的权值为 z。

输出格式:

包含一行,仅一个数,表示严格次小生成树的边权和。(数据保证必定存在严格次小生成树)

样例:

样例输入 1:

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

样例输出 1:

1
11

思路:

  1. 要开long long
  2. 判自环

实现:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

// Problem : P4180 [BJWC2010]严格次小生成树
// Contest : Luogu Online Judge
// URL : https://www.luogu.com.cn/problem/P4180
// Memory Limit : 500 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#include "ybwhead/ios.h"
const int maxn = 1e5 + 100;
#define int long long
int f[maxn];
struct line
{
    int x, y, z;
} a[maxn * 3 + 5];
int cmp(line a, line b)
{
    return a.z < b.z;
}
int head[maxn];
struct edge
{
    int v, w, nxt;
} e[maxn << 1];
int tot;
void __ADD(int u, int v, int w)
{
    e[++tot].v = v;
    e[tot].w = w;
    e[tot].nxt = head[u];
    head[u] = tot;
}
void add(int u, int v, int w)
{
    // cerr << u << " " << v << " " << w << endl;
    __ADD(u, v, w);
    __ADD(v, u, w);
}
int fa[maxn][23], mx[maxn][23];
int dep[maxn];
void dfs(int u, int f)
{
    fa[u][0] = f;
    dep[u] = dep[f] + 1;
    for (int i = 1; i <= 20; i++)
    {
        fa[u][i] = fa[fa[u][i - 1]][i - 1], mx[u][i] = max(mx[u][i - 1], mx[fa[u][i - 1]][i - 1]);
    }
    for (int i = head[u]; i; i = e[i].nxt)
    {
        int v = e[i].v;
        if (v == f)
            continue;
        mx[v][0] = e[i].w;
        dfs(v, u);
    }
}
int q(int x, int y, int z)
{
    if (dep[x] < dep[y])
        swap(x, y);
    int ans = 0;
    for (int i = 20; i >= 0; i--)
    {
        if (dep[fa[x][i]] >= dep[y])
        {
            if (mx[x][i] != z)
                ans = max(ans, mx[x][i]);
            x = fa[x][i];
        }
    }
    if (x == y)
        return ans;
    for (int i = 20; i >= 0; i--)
    {
        if (fa[x][i] != fa[y][i])
        {
            if (mx[x][i] != z)
                ans = max(ans, mx[x][i]);
            if (mx[y][i] != z)
                ans = max(ans, mx[y][i]);
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return max(ans, max(mx[x][0] != z ? mx[x][0] : 0, mx[y][0] != z ? mx[y][0] : 0));
    // }
    // // yout<<ans<<endl;
    // return ans;
}
// int f[maxn];
int getf(int x)
{
    // cerr << x << endl;
    if (x == f[x])
        return x;
    return f[x] = getf(f[x]);
}
int merge(int x, int y)
{
    int sx = getf(x), sy = getf(y);
    if (sx == sy)
        return 0;
    f[sx] = sy;
    return 1;
}
int n, m;
signed main()
{
    yin >> n >> m;
    for (int i = 1; i <= n; i++)
        f[i] = i;
    for (int i = 1; i <= m; i++)
    {
        yin >> a[i].x >> a[i].y >> a[i].z;
    }
    sort(a + 1, a + m + 1, cmp);
    int ans = 0, sum = LLONG_MAX;
    for (int i = 1; i <= m; i++)
    {
        if (merge(a[i].x, a[i].y))
        {
            add(a[i].x, a[i].y, a[i].z);
            ans += a[i].z;
        }
    }
    dfs(1, 0);
    for (int i = 1; i <= m; i++)
    {
        if (fa[a[i].x][0] == a[i].y || fa[a[i].y][0] == a[i].x || a[i].x == a[i].y)
        {
            continue;
        }
        int xx;
        if ((xx = (a[i].z - q(a[i].x, a[i].y, a[i].z))) != 0)
            sum = min(xx, sum);
        // yout << xx << endl;
    }
    yout << sum + ans << endl;
    return 0;
}