题干:

Problem Description

There are nnn cities in Byteland, and the ithi_{th}ith​ city has a value aia_iai​. 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.

Input

The first line is an integer TTT(T≤105T\leq10^5T≤105), representing the number of test cases.
For each test case, the first line is an integer nnn(n≤105n\leq10^5n≤105), representing the number of cities, the second line are n positive integers aia_iai​(ai≤105a_i\leq10^5ai​≤105), representing their values.

Output

The first line is an integer TTT(T≤105T\leq10^5T≤105), representing the number of test cases.
For each test case, the first line is an integer nnn(n≤105n\leq10^5n≤105), representing the number of cities, the second line are n positive integers aia_iai​(ai≤105a_i\leq10^5ai​≤105), representing their values.

Sample Input

2
4
1 2 3 4
1
1

Sample Output

12
0

题目大意:

给定n(<=1e5)个点,告诉你每个点的点权,并告诉你一条边连接两个点u和v的话,边权是a[u]+a[v],求最小生成树。

解题报告:

直接贪心,每个点都和最小的点连起来就行了。证明如下:假设点权最小的点是x,最终答案是这一棵树G,那么对于某一条边e的两个点u,v,考虑去掉这条边之后,一定有一个和x连通,一个和x不连通,假设分别为u和v,那么我们将v和x连起来,得到的一定更优。由此归纳,每个边都可以转换成和x号点连接的边,得到最优解。证毕。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
int a[1001000];
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]);} sort(a,a+n);ll sum=0;for(int i=1;i<n;i++) {sum+=a[i];}sum+=a[0]*1ll*(n-1);printf("%lld\n",sum);}return 0;
}

【2018ACM山东省赛 - C】Cities(最小生成树变形优化,贪心思维)相关推荐

  1. 【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化)

    题干: We define an element aia_iai​ in a sequence "good", if and only if there exists a j(1≤ ...

  2. codeforces 962E Byteland, Berland and Disputed Cities 最小生成树变形

    题目 题目链接 题意 在OxOxOx轴上有一堆点,这些点有三种类型R.B.PR.B.PR.B.P型,现在要求添加一些线段把这些点连起来,使得如果去掉RRR类型点,剩下的点都是联通的.如果去掉B&quo ...

  3. 【2018ACM山东省赛 - G】Games(Nim博弈 + dp)

    题干: Problem Description Alice and Bob are playing a stone game. There are nnn piles of stones. In ea ...

  4. 【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常)

    题干: Problem Description In GGO, a world dominated by gun and steel, players are fighting for the hon ...

  5. 模板:Prime最小生成树堆优化 + Dijkstra单源最短路堆优化

    Dijkstra 单源最短路堆优化 #include<bits/stdc++.h> using namespace std; typedef pair<int, int> PI ...

  6. 2019ACM浪潮杯山东省赛参赛总结

    emmm是要记录一下生活了呢,不然以后退役了连自己经历过什么都记不住了. 5.11周六,早上5:30分,qdu集训队一行40人(左右)集合登上大巴,前往济南大学参加ACM省赛.上车清点了一下人数,然后 ...

  7. 小程序进入页面图片渲染会拉伸闪下变形优化bug

    小程序进入页面图片渲染会拉伸闪下变形优化bug image的mode图片剪裁缩放模式用mode='widthFix'(宽度不变,高度自动变化,保持原图宽高比不变),记得要在css里也加上height: ...

  8. 2016第七届ACM山东省赛

     "题目说的是输出编号.输出编号!!" "好,改好了." "交." 最后两分钟再一次提交了代码. no  -  wrong. 奋战到最后一秒 ...

  9. 武理校赛A题 ljw的剥削(思维 + map应用)

    武理校赛A题 ljw的剥削(思维 + map应用) 牛客链接 题意: 给定 a[],b[] 两个长度同为 n 的数组,经过一系列操作后, 使 p = ∑ i = 1 n m a x ( ( a i − ...

最新文章

  1. keystone连接mysql_3、KeyStone服务部署与验证
  2. java泛型反映调用方法体内类型引用问题
  3. boost::hof::unpack用法的测试程序
  4. android xml事件,安卓事件
  5. JAVA实现美团电影价格抓取(附代码)
  6. BZOJ1026: [SCOI2009]windy数(数位dp)
  7. java双机和集群的区别,java 分布式与集群的区别和联系
  8. 计算机网络概念,组成,功能和分类
  9. prim算法_数据结构 7.4.1 最小生成树 Prim
  10. Spark 0.9.1 MLLib 机器学习库简介
  11. H.264文件解析与码流分析
  12. 【SDCC 2016】微影时代、普元、亚信、Fit2Cloud、VMware、京东商城、优维科技畅聊自动化运维与容器...
  13. educoder JAVA实训答案
  14. 通过手机获取微信公众号封面
  15. 小额批量支付系统可以完成跨行资金清算吗_今天,你转账了吗?带你看懂央行小额批量支付系统...
  16. mac下Charles https抓包乱码,手机不能上网解决
  17. GUI使用2——总结NGUI、tookit2D、GUI比较
  18. 用scratch制作蓄力跳
  19. 两年多工作心得和体会
  20. 一个简洁的倒计时shell 脚本

热门文章

  1. Oracle高级SQL培训与讲解
  2. arry-718 Maximum Length of Repeated Subarray
  3. [Leetcode][第5458题][JAVA][字符串的好分割数目][双指针][HashSet]
  4. 共轭方式怎么判断_怎样判断共轭双烯
  5. linux tcp header更改,Linux Netfilter中修改TCP/UDP Payload的方法
  6. python怎么获取时间_Python:如何从datetime.timedelta对象中获取时间?
  7. n型半导体和p型半导体的区别_VNX系列大流量工业型膜堆, 为半导体等行业提供超纯水!...
  8. iphone数据迁移到新iphone_Mac专业iPhone数据恢复软件----Omni Recover
  9. 获取apk安装包sha1的值
  10. 环形队列出队的元素怎么输出出来_队列:队列在线程池等有限资源池中的应用...