# SCOI 滑雪与时间胶囊

a180285 非常喜欢滑雪。

3 3
3 2 1
1 2 1
2 3 1
1 3 10

3 2

## 思路

K r u s k a l Kruskal P r i m Prim 肯定是过不了的,因为题目中有限制.

#include <queue>
#include <vector>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXM = 5 * 1e6 + 5;

int n, m, h[MAXN], maxl, tot, cnt, fa[MAXN];
long long ans;
bool vis[MAXN];

struct node {
int u, v, w;
} dis[MAXM];
bool cmp(node x, node y) {
if (h[x.v] != h[y.v])
return h[x.v] > h[y.v];
else
return x.w < y.w;
}

struct edge {
int v, w;
edge() {}
edge(int V, int W) {
v = V;
w = W;
}
};

vector<edge> G[MAXN];

void AddEdge(int u, int v, int w) { G[u].push_back(edge(v, w)); }

int FindSet(int v) {
if (fa[v] == v)
return fa[v];
else
return fa[v] = FindSet(fa[v]);
}
bool UnionSet(int u, int v) {
int x = FindSet(u);
int y = FindSet(v);
if (x == y)
return 0;
else {
fa[x] = fa[y];
return 1;
}
}
void Kruskal() {
for (int i = 1; i <= n; i++) fa[i] = i;
sort(dis + 1, dis + 1 + cnt, cmp);
for (int i = 1; i <= cnt; i++) {
if (UnionSet(dis[i].u, dis[i].v)) {
ans += dis[i].w;
tot++;
}
if (tot == maxl - 1)
break;
}
printf("%lld\n", ans);
}

void dfs(int x) {
vis[x] = 1;
maxl++;
for (int i = 0; i < G[x].size(); i++) {
int v = G[x][i].v;
if (!vis[v]) {
dfs(v);
}
}
}

void intn() {
for (int i = 1; i <= n; i++) {
if (vis[i] == 1) {
for (int j = 0; j < G[i].size(); j++) {
if (vis[G[i][j].v] == 1) {
cnt ++;
dis[cnt].u = i;
dis[cnt].v = G[i][j].v;
dis[cnt].w = G[i][j].w;
}
}
}
}
}

int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &h[i]);
}
for (int i = 1, ui, vi, wi; i <= m; i++) {
scanf("%d %d %d", &ui, &vi, &wi);
if (h[ui] >= h[vi]) {
}
if (h[ui] <= h[vi]) {
}
}
dfs(1);
printf("%d ", maxl);
intn();
Kruskal();
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

cqbz_ChenJiage

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
12-19 1189

03-05 218
12-05 1228
10-20 73
09-24 383
08-18 171