题目链接:https://pintia.cn/problem-sets/994805148990160896/problems/994805156657348608
主要考连通性 ,简单来说就是枚举删除每个点的最小生成树的最大的花费是多少,注意如果不能联通那么一定是最大的,最后在遍历一遍输出答案即可
算法:kustal最小生成树
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 1000000
#define N 550
#define MAX 300000
int n, m;
int p[N];
struct road {
int s;
int e;
int vis;
};
road use[MAX], nuse[MAX];
void init() {
for (int i = 1; i <= n; i++) {
p[i] = i;
}
}
int find(int x) {
if (x != p[x]) {
p[x] = find(p[x]);
}
else
return x;
}
int main() {
int x, y, c, s;
scanf("%d%d", &n, &m);
int ans1 = 0, ans2 = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &x, &y, &c, &s);
if (s == 1) {
use[ans1].s = x;
use[ans1].e = y;
use[ans1++].vis = c;
}
else {
nuse[ans2].s = x;
nuse[ans2].e = y;
nuse[ans2++].vis = c;
}
}
int mx = 0;
int sum[N];
memset(sum, 0, sizeof(sum));
sort(nuse, nuse + ans2);
for (int i = 1; i <= n; i++) {
init();
int num = n - 2;
for (int j = 0; j < ans1; j++) {
if (use[j].s == i || use[j].e == i)
continue;
int xx = find(use[j].s);
int yy = find(use[j].e);
if (xx != yy) {
p[xx] = yy;
num--;
}
}
for (int k = 0; k < ans2; k++) {
if (nuse[k].s == i || nuse[k].e == i)
continue;
int xx = find(nuse[k].s);
int yy = find(nuse[k].e);
if (xx != yy) {
p[xx] = yy;
num--;
sum[i] += nuse[k].vis;
}
}
if (num > 0) {
sum[i] = inf;
}
mx = max(mx, sum[i]);
}
if (mx == 0) {
cout << mx;
return 0;
}
int t = 0;
for (int i = 1; i <= n; i++) {
if (sum[i] == mx) {
if (t == 0)
{
t = 1;
cout << i;
}
else
cout << " " << i;
}
}
return 0;
}