“浪潮杯”第九届山东省ACM大学生程序设计竞赛(Promble C ,Cities)

探讨了在一个特定问题中如何计算连接多个城市所需的最小成本。通过分析发现直接使用最小生成树算法会超时,最终采用了一种贪心策略,选择价值最小的城市作为中心节点来连接其他所有城市。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

题目描述

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.

        蒟蒻开始认为是最小生成树模板,但10的五次方的数据量无论Prim还是Kruscal都会超时。

        正解是贪心思想,题目没有规定在哪些城市间修建道路,所以我们取权值最小的那座城市作为中心,用它将所有城市连接起来。

        自己还差的太远。。。

AC代码:

#include<bits/stdc++.h>
#define INF 999999999;
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
       long long s=0,tot=0,minn=INF;
       for(int i=0;i<n;++i)
       {
          long long x;
          cin>>x;
          tot+=x;
          if(x<minn)minn=x;

       }
       if(n==0)cout<<"0"<<endl;
       else cout<< tot-minn+minn*(n-1)<<endl;
    }
    return 0;
}


加油吧!!!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值