Sub2 #3 WA 求条
查看原帖
Sub2 #3 WA 求条
779995
VividCycle楼主2023/9/27 17:00

rt,对这个数据输出了 12。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, s, k;
struct Edge {int u, v, w;} a[500005];
int f[200005];
int find(int x) {
    return f[x] == x ? x : f[x] = find(f[x]);
}
int check(int mid) {
    for (int i=1; i<=m; i++)
        if (a[i].u == s || a[i].v == s) a[i].w += mid;
    sort(a+1, a+1+m, [](Edge x, Edge y){return x.w < y.w;});
    for (int i=1; i<=n; i++) f[i] = i;
    int summ=0, cnt=0;
    for (int i=1; i<=m; i++)
        (find(a[i].u) != find(a[i].v)) && (f[find(a[i].u)] = find(a[i].v), summ += a[i].w, cnt += a[i].u == s || a[i].v == s);
    for (int i=1; i<=m; i++)
        if (a[i].u == s || a[i].v == s) a[i].w -= mid;
    return cnt;
}
int main() {
    int l=-30005, r=30005, mid, ans=0; cin >> n >> m >> s >> k;
    for (int i=1; i<=m; i++) cin >> a[i].u >> a[i].v >> a[i].w; 
    if (check(l) < k) {cout << "Impossible"; return 0;}
    while (l <= r) {
        mid = (l + r) / 2; int q = check(mid);
        if (q >= k) l = mid + 1, q == k && (ans = mid);
        else r = mid - 1;
    }
    for (int i=1; i<=m; i++)
        if (a[i].u == s || a[i].v == s) a[i].w += ans;
    sort(a+1, a+1+m, [](Edge x, Edge y){return x.w < y.w;});
    for (int i=1; i<=n; i++) f[i] = i;
    int summ=0, cnt=0;
    for (int i=1; i<=m; i++)
        (find(a[i].u) != find(a[i].v)) && (f[find(a[i].u)] = find(a[i].v), summ += a[i].w, cnt += a[i].u == s || a[i].v == s);
    if (cnt > k) {cout << "Impossible"; return 0;}
    cout << summ - k * ans << endl;
}
2023/9/27 17:00
加载中...