QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#638037#7883. Takeout Deliveringji_114514WA 117ms46508kbC++201.5kb2024-10-13 14:43:442024-10-13 14:43:44

Judging History

你现在查看的是最新测评结果

  • [2024-10-13 14:43:44]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:46508kb
  • [2024-10-13 14:43:44]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 1e6 + 10, INF = 1e9 + 7;

struct edge {
    int u, v, w;
} e[N];
int d[N][2], n, m, f[N];

vector<int>g[N];
int find(int x)
{
    if (f[x] != x)f[x] = find(f[x]);
    return f[x];
}

bool cmp(edge x, edge y) {
    return x.w < y.w;
}

void merge(int u, int v, int x, int w)
{
    if (find(u) == find(x) && find(v) == find(x))return;
    if (find(u) != find(x) && find(v) != find(x))return;
    if (find(u) != find(x))swap(u, v);
    for (auto y : g[find(v)]) {
        if (x == 1)d[y][0] = min(d[y][0], w);
        else d[y][1] = min(d[y][1], w);
    }
}


void solve()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++)cin >> e[i].u >> e[i].v >> e[i].w;
    sort(e, e + m, cmp);
    for (int i = 1; i <= n; i++)
    {
        f[i] = i, g[i].push_back(i);
        for (int j = 0; j < 2; j++)d[i][j] = INF * 2;
    }
    int res = 2 * INF;
    d[1][0] = 0, d[n][1] = 0;
    for (int i = 0; i < m; i++) {
        auto [u, v, w] = e[i];
        for (int j = 0; j < 2; j++)res = min(res, w + max(d[u][j] , d[v][j ^ 1]));
        merge(u, v, 1, w), merge(u, v, n, w);
        u = find(u), v = find(v);
        if (u == v)continue;
        if (g[u].size() < g[v].size())swap(u, v);
        for (auto y : g[v])g[u].push_back(y);
        f[v] = u;
    }
    cout << res;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    while (t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 9732kb

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: -100
Wrong Answer
time: 117ms
memory: 46508kb

input:

300000 299999
80516 80517 597830404
110190 110191 82173886
218008 218009 954561262
250110 250111 942489774
66540 66541 156425292
34947 34948 239499776
273789 273790 453201232
84428 84429 439418398
98599 98600 326095035
55636 55637 355015760
158611 158612 684292473
43331 43332 43265001
171621 171622 ...

output:

-2147475201

result:

wrong answer 1st numbers differ - expected: '1999991697', found: '-2147475201'