比赛 |
2024暑假C班集训B |
评测结果 |
AWWATTWWWWWWWWWWWWWW |
题目名称 |
赛道修建 |
最终得分 |
10 |
用户昵称 |
LikableP |
运行时间 |
4.227 s |
代码语言 |
C++ |
内存使用 |
4.04 MiB |
提交时间 |
2024-07-11 11:34:02 |
显示代码纯文本
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <random>
using namespace std;
const int MAXN = 5e4 + 10;
const int MAXM = 5e4 + 10;
struct EDGE {
int to, w, next;
} edge[2 * MAXM];
int head[MAXN], edgeNum;
void AddEdge(int from, int to, int w) {
edgeNum++;
edge[edgeNum].to = to;
edge[edgeNum].w = w;
edge[edgeNum].next = head[from];
head[from] = edgeNum;
}
int maxx = -1;
int vis[MAXN];
void dfs(int pos, int sum) {
if (!vis[pos]) {
vis[pos] = true;
for (int i = head[pos]; i; i = edge[i].next) {
dfs(edge[i].to, sum + edge[i].w);
}
maxx = max(maxx, sum);
}
}
int n, m;
int w[MAXM];
int s2w[MAXM];
int main() {
freopen("2018track.in", "r", stdin);
freopen("2018track.out", "w", stdout);
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
bool special1 = true, special2 = true;
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
::w[i] = w;
s2w[u] = w;
if (u != 1) special1 = false;
if (v != u + 1) special2 = false;
AddEdge(u, v, w);
AddEdge(v, u, w);
}
if (m == 1) {
for (int i = 1; i <= n; i++) {
memset(vis, false, sizeof(vis));
dfs(i, 0);
}
cout << maxx;
} else if (special1) {
sort(w + 1, w + n, [](int x, int y) {
return x > y;
});
for (int i = 1; i < n; i++) cout << w[i] << ' ';
cout << endl;
int chooseedge = (n - 1) / m;
int st = chooseedge * (m - 1) + 1;
int ans = 0;
for (int i = st; i < st + chooseedge; i++) ans += w[i];
cout << ans;
} else if (special2) {
int chooseedge = (n - 1) / m;
vector <int> v(m);
int idx = 1;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= chooseedge; j++) {
v[i - 1] += s2w[idx];
idx++;
}
}
sort(v.begin(), v.end());
cout << v[0];
} else {
default_random_engine engine(time(nullptr));
uniform_real_distribution <double> distribution(0, 10000);
cout << (int)distribution(engine);
}
return 0;
}