目录
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
因此,为了检索更快,可以建立一个以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);
}
}
最后的sum最大可能是: 10^9 * 10^5 = 10^14
而,
因此,sum 需要定义为long long
long long sum = 0;
因此,输出时,需要用
printf("Case #%d: %lld\n", t1+1, sum);
在定义完数组res后,对数组部分元素进行了赋值,当后面需要对其全部元素遍历加和时,不能默认其他元素就是0了,需要在定义后将所有元素初始化为0。