萌新求助,滑雪那题,本地样例AC提交RE
查看原帖
萌新求助,滑雪那题,本地样例AC提交RE
362101
_TLEer_的小号楼主2021/2/1 19:10

RT.

#include <iostream>
#include <algorithm>
#include <cstdio>
#define int long long
using namespace std;
template <typename T>
inline void R(T &t)
{
    t = 0;
    register char ch = getchar();
    while (!('0' <= ch && ch <= '9'))
    {
        if (ch == '-')
            t = -1;
        ch = getchar();
    }
    while (('0' <= ch && ch <= '9'))
    {
        t = ((t << 1) + (t << 3)) + ch - '0';
        ch = getchar();
    }
}
void write(int X)
{
    if (X < 0)
    {
        X = ~(X - 1);
        putchar('-');
    }
    if (X > 9)
        write(X / 10);
    putchar(X % 10 + '0');
}
struct Union
{
    int a[100010];
    Union()
    {
        for (int i = 0; i < 100010; i++)
            a[i] = i;
    }
    int find(int x)
    {
        return x == a[x] ? x : a[x] = find(a[x]);
    }
    int merge(int x, int y)
    {
        int fx = find(x), fy = find(y);
        a[fx] = fy;
    }
    bool is_bro(int x, int y)
    {
        return find(x) == find(y);
    }
} A;
int lna, lnb;
int hda[100010], hdb[100010];
struct EDGE
{
    int u, v, w, nxt;
} a[1000010];
int sum, q[100010], ql, qr, hight[100010];
bool vis[100010];
bool cmp(EDGE a, EDGE b)
{
    if (hight[a.v] != hight[b.v])
        return hight[a.v] > hight[b.v];
    return a.w < b.w;
}
struct edge
{
    int u, v, w, nxt;
} b[1000010];
void mergea(int u, int v, int w)
{
    a[++lna].u = u;
    a[lna].v = v;
    a[lna].w = w;
    a[lna].nxt = hda[u];
    hda[u] = lna;
}
void mergeb(int u, int v, int w)
{
    b[++lnb].u = u;
    b[lnb].v = v;
    b[lnb].w = w;
    b[lnb].nxt = hdb[u];
    hdb[u] = lnb;
}
void bfs()
{ //宽搜,拓展可到达的点,建新图
    int cnt = 0;
    q[++qr] = 1;
    vis[1] = 1;
    while (ql < qr)
    {
        int now = q[++ql];
        for (int i = hdb[now]; i; i = b[i].nxt)
        {
            mergea(now, b[i].v, b[i].w);
            if (!vis[b[i].v])
            {
                vis[b[i].v] = 1;
                sum++;
                q[++qr] = (b[i].v);
            }
        }
    }
}
signed main()
{
    freopen("1.in", "r", stdin);
    int n, m;
    n = m = 0;
    R(n);
    R(m);
    for (int i = 1; i <= n; i++)
        R(hight[i]);
    int u, v, w;
    for (int i = 1; i <= m; i++)
    {
        R(u);
        R(v);
        R(w);
        if (hight[u] <= hight[v])
            mergeb(v, u, w);
        if (hight[u] >= hight[v])
            mergeb(u, v, w);
    }
    bfs();
    Union A;
    std::sort(a + 1, a + lna + 1, cmp);
    int ans = 0;
    for (int i = 1; i <= lna; i++)
    {
        int fx = A.find(a[i].u), fy = A.find(a[i].v);
        if (fx != fy)
        {
            A.merge(fx, fy);
            ans += a[i].w;
        }
    }
    write(sum + 1);
    putchar(' ');
    write(ans);
    putchar('\n');
    return 0;
}
2021/2/1 19:10
加载中...