D、Bulbasaur C语言

目录

题目

题目分析:

通过代码:

需要注意的:

C语言中整型(int)与长整型(long long)的范围

C语言中整型(int)与长整型(long long)在标准化输入输出中的占位符

数组的初始化


题目

https://ac.nowcoder.com/acm/problem/17441

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.

输出描述:

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

题目分析:

  • a={a_1...a_m}为脸集合, b={b_1...b_n}为身体集合。
  • 1张脸可以对应多个身体 => a中可以有重复的元素
  • 1个身体只能对应1张脸 => b中不可以有重复的元素
  • 最后输出最大权重之和 => 如果输入的b中有重复的,取最大的权重

因此,为了检索更快,可以建立一个以b值为索引的数组res,res最多有max(b)个。

通过代码:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int t;
    scanf("%d", &t);

    for(int t1 = 0; t1<t; t1++){
        int n, m, k;
        // 注意!
        // 最后的sum最大可能是: 10^9 * 10^5 = 10^14
        // int和long的范围为: -2147483647~2147483647( 10位数,约等于 10^10)
        // long long(与 __int64相同)为真正的长整型,范围为:9223372036854775808~+9223372036854775807(九百亿亿,约10^19)
        // 因此,sum 需要定义为long long
        long long sum = 0;
        scanf("%d %d %d", &n, &m, &k);
        // 以b为键,b最大为m。
        int res[m+1];
        for(int i=0; i<m+1; i++){
            res[i] = 0;
        }
        int count;
        for(int i=0; i<k; i++){
            int a1, b1, c1;
            scanf("%d %d %d", &a1, &b1, &c1);
            // 将b1对应的权重更新为最大的
            if(res[b1]<c1){
                res[b1] = c1;
            }
        }
        for(int i=0; i<m+1; i++){
            sum = sum +res[i];
        }
        // int 在标准化输入输出中的占位符是 %d
        // long 在标准化输入输出中的占位符是 %ld
        // long long 在标准化输入输出中的占位符是 %lld
        printf("Case #%d: %lld\n", t1+1, sum);
    }
}

需要注意的:

C语言中整型(int)与长整型(long long)的范围

最后的sum最大可能是: 10^9 * 10^5 = 10^14
而,

  • int和long的范围为: -2147483647~2147483647( 10位数,约等于 10^10)
  • long long(与 __int64相同)为真正的长整型,范围为:-9223372036854775808~9223372036854775807(九百亿亿,约10^19)

因此,sum 需要定义为long long

long long sum = 0;

C语言中整型(int)与长整型(long long)在标准化输入输出中的占位符

  • int 在标准化输入输出中的占位符是 %d
  • long 在标准化输入输出中的占位符是 %ld
  • long long 在标准化输入输出中的占位符是 %lld

因此,输出时,需要用

printf("Case #%d: %lld\n", t1+1, sum);

数组的初始化

在定义完数组res后,对数组部分元素进行了赋值,当后面需要对其全部元素遍历加和时,不能默认其他元素就是0了,需要在定义后将所有元素初始化为0。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值