Bob's Problem

Bob was in trouble.He rubbed the magic ring on his finger, and you came out of the ground.
You are given an undirected graph G which contains nn vertices labelled from 11 to nn, with mm weighted edges between them coloured in black or white.You have to choose some edges in GG such that there is at least one path between any two vertices only passing by selected edges, and you can select no more than kk white edges.There may be multiple available strategies to determine these edges, and you are asked to find out the way with a maximum total weight of edges.

Input
The first line contains an integer T(1≤T≤5) indicating the number of test cases.
For each test case, the first line contains three integers n (1≤n≤50000),m and k (1≤k≤m≤500000).
Each of the following mm lines contains four integers u,v (1≤u,v≤n),w (0≤w≤100000) and c (0≤c≤1) describing an edge of weight w and colour c between the u-th vertex and the v-th vertex. Here an edge is white if c=1, or black if c=0.
Note that there may be multiple edges between some vertices, and self-loops are also allowed.

Output
For each test case, output a single line with an integer indicating the maximum total weight of selected edges, or output -1 if there is no solution for the given graph.

样例输入
1
5 6 2
1 2 0 0
1 3 5 1
1 5 1 0
2 3 6 1
2 4 2 0
3 4 7 1
样例输出
16
#include <bits/stdc++.h>

using namespace std;
int n, m, k, black_num, white_num;
int num[50050];

struct node {
    int x, y, z;
} black_edge[500050];

struct Node {
    int x, y, z;

    bool operator<(const Node &a) const {
        return z > a.z;
    }
} white_edge[500050];

int a[50050];

int gets(int x) {
    return a[x] == x ? x : (a[x] = gets(a[x]));
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        black_num = 0;
        white_num = 0;
        scanf("%d%d%d", &n, &m, &k);
        for (int i = 1; i <= n; i++) {
            a[i] = i, num[i] = 1;
            black_edge[i].z = 0;
            white_edge[i].z = 0;
        }
        for (int i = 0; i < m; i++) {
            int x, y, z, t;
            scanf("%d%d%d%d", &x, &y, &z, &t);
            if (t == 1) {
                white_edge[white_num].x = x;
                white_edge[white_num].y = y;
                white_edge[white_num].z = z;
                white_num++;
            } else if (t == 0) {
                black_edge[black_num].x = x;
                black_edge[black_num].y = y;
                black_edge[black_num].z = z;
                black_num++;
            }
        }
        sort(white_edge, white_edge + white_num);
        int max0 = 0, flag = 0;
        long long ans = 0;
        for (int i = 0; i < black_num; i++) {
            ans += (long long) black_edge[i].z;
            if (!flag) {
                int x = gets(black_edge[i].x), y = gets(black_edge[i].y);
                if (x != y) {
                    a[y] = x;
                    num[x] += num[y];
                    if (num[x] > max0)
                        max0 = num[x];
                    if (max0 == n)
                        flag = 1;
                }
            }
        }
        sort(white_edge, white_edge + white_num);
        if (flag) {
            for (int i = 0; i < k; i++)
                ans += (long long) white_edge[i].z;
            printf("%lld\n", ans);
        } else {
            int quantity = 0;
            for (int i = 0; i < white_num; i++) {
                if (max0 < n) {
                    if (quantity == k) {
                        printf("-1\n");
                        flag = 1;
                        break;
                    }
                    int x = gets(white_edge[i].x), y = gets(white_edge[i].y);
                    if (x != y) {
                        ans += (long long) white_edge[i].z;
                        white_edge[i].z = 0;
                        quantity++;
                        a[y] = x;
                        num[x] += num[y];
                        if (num[x] > max0)
                            max0 = num[x];
                    }
                } else
                    break;
            }
            if (quantity <= k && max0 < n) {
                if (!flag)
                    printf("-1\n");
            } else if (!flag) {
                sort(white_edge, white_edge + white_num);
                for (int i = 0; i < k - quantity; i++)
                    ans += (long long) white_edge[i].z;
                printf("%lld\n", ans);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值