C - Cities -“浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛

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

There are cities in Byteland, and the city has a value . The cost of building a bidirectional road between two cities is the sum of their values. Please calculate the minimum cost of connecting these cities, which means any two cities can reach each other.
输入描述:
The first line is an integer
representing the number of test cases.

For each test case, the first line is an integer , representing the number of cities, the
second line are positive integers ,
representing their values.
输出描述:
For each test case, output an integer, which is the
minimum cost of connecting these cities.
示例1
输入

2
4
1 2 3 4
1
1
输出

12
0

[题意]
有n个节点,每个有权值,两个相连的消费是连个节点的权值和,让所有点连接起来。

[分析]
就很无脑啊,所有节点都和最小的点连接嘛。为了写代码方便我就直接用排序了。

[代码]

#include<cstdio>
#include<algorithm>
using namespace std;

int a[100005];

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }
        long long ans = 0;
        sort(a, a + n);
        for (int i = 1; i < n; i++)
        {
            ans += a[i] + a[0];
        }
        printf("%lld\n", ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值