1640 天气晴朗的魔法
题目描述
51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动。
N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵。
魔法链是做法成功与否的关键。每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和。
由于逆天改命的魔法过于暴力,所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大。
现在给定魔法师人数N,魔法链数目M。求此魔法阵的最大效果。
输入
两个正整数N,M。(1 <= N <= 10^5, N <= M <= 2 * 10^5)
接下来M行,每一行有三个整数A, B, V。(1 <= A, B <= N, INT_MIN <= V <= INT_MAX)
保证输入数据合法。
输出
输出一个正整数R,表示符合条件的魔法阵的魔力值之和。
样例
Input示例
4 6
1 2 3
1 3 1
1 4 7
2 3 4
2 4 5
3 4 6
Output示例
12
题意
51Nod上面的图论题就是有意思, 题意就是求一个最小生成树里面的最大权值, 然后在跑一个最大生成树 生成树的边权小于最小的最大值就行
AC代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define ls st<<1
#define rs st<<1|1
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int,int>
#define VI vector<int>
#define CLR(a,b) memset(a, (b), sizeof(a))
#define ALL(x) x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+10;
const int mod = 1e9+7;
const double eps = 1e-8;
void fe() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt","w",stdout);
#endif
}
LL read()
{
LL x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
int n, m;
struct node
{
int u, v, w;
}p[MAXN];
int par[MAXN];
int maxx = -1;
void init() {
for(int i = 0; i <= n; i++)
par[i] = i;
}
int find(int x) {
if(x == par[x])
return x;
return par[x] = find(par[x]);
}
bool cmp1(node a, node b) {
return a.w < b.w;
}
bool cmp2(node a, node b) {
return a.w > b.w;
}
void find_min() {
sort(p, p+m, cmp1);
init();
LL k = 0;
for(int i = 0; i < m; i++) {
if(find(p[i].v) != find(p[i].u)) {
par[find(p[i].v)] = find(p[i].u);
if(maxx < p[i].w) {
maxx = p[i].w;
}
k++;
if(k >= n-1) break;
}
}
return ;
}
void find_max() {
find_min();
int k = 0;
init();
int ans = 0;
sort(p, p+m, cmp2);
for(int i = 0; i < m; i++) {
if(find(p[i].v)!=find(p[i].u) && p[i].w<=maxx) {
par[find(p[i].v)] = find(p[i].u);
ans += p[i].w;
k++;
if(k >= n-1)
break;
}
}
cout << ans << endl;
return ;
}
int main() {
cin >> n>> m;
for(int i = 0; i < m; i++)
cin >> p[i].u >> p[i].v >> p[i].w;
find_max();
return 0;
}