牛客网暑期ACM多校训练营(第六场)Bulbasaur

链接:https://www.nowcoder.com/acm/contest/144/D
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Silph company deployed a passenger flow analysis system in a clothing store that captures photos of human faces and photos of human bodies in real time.

In order to analyze the passenger flow better, the system will associate human faces with human bodies. In other words, there are some edges connecting faces with bodies. Each edge has a positive weight.

However, due to lack of precision and accuracy of the algorithms provided by this company, these associations may not be completely correct.

In a correct relationship, one human face can associate with multiple human bodies (one person may change multiple suits of clothes), but one human body cannot associate with multiple human faces.

Now Bulbasaur works as an intern at Silph company and the boss asks him to solve this problem.

Bulbasaur is supposed to find an association relationship, such that the sum of weights of the association edges is maximum.

输入描述:

The input starts with one line containing exactly one integer T, which is the number of test cases.

For each test case, the first line contains three integers n, m and k, indicating the number of face photos, the number of body photos, and the number of existing association edges, respectively.

Then followed by k lines, each consists of three integers ai, bi and ci, representing an edge weighted ci connecting the ai-th face photo with the bi-th body photo.

- 1 ≤ T ≤ 5.
- 1 ≤ n, m, k ≤105.
- 1 ≤ ai ≤ n.
- 1 ≤ bi ≤ m.
- 1 ≤ ci ≤ 109.
- .

输出描述:

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the maximum sum of weights of the association edges.

示例1

输入

复制

1
2 3 3
1 2 1919
1 3 810
2 2 450

输出

复制

Case #1: 2729

题意:有n个头,m个身子,一个头可以和多个身子相连,但一个身子只能和一个头相连,每一条边都有一个权值,问连通块的最大权值;

思路:求出每个身子相连的头的最大的边权,相加即可;

下面附上我的代码:

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+11;
typedef long long LL;
int main()
{

    int t;
    scanf("%d",&t);
    int Case = 1;
    while (t--){
        map<LL,LL> ct;
        map<LL ,bool> vis;
        int n,m,k;
        LL a,b,c;
        LL sum = 0;
        scanf("%d%d%d",&n,&m,&k);
        for (int  i = 0;i < k; ++i){
            scanf("%lld%lld%lld",&a,&b,&c);
            if (vis[b]){
                ct[b] = max(ct[b],c);
            }else {
                ct[b] = c;
                vis[b] = true;
            }
        }
    for (int i = 1; i <= m; ++i){
        sum += ct[i];
    }
        printf("Case #%d: %lld\n",Case++,sum);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值