蓝桥杯 算法训练 ALGO-6 安慰奶牛
2016-02-16 01:14
489 查看
#include<iostream> #include<algorithm> #include<math.h> using namespace std; int pre[11000]; int point[11000]; struct Edge { int x,y,w; }edge[110000]; int n,m; int cmp(Edge a,Edge b) { return a.w<b.w; } int find(int x) { if(pre[x]==x) return x; int t=find(pre[x]); pre[x]=t; return t; } int main() { int i; while( scanf("%d%d",&n,&m)!=EOF ) { int mmin=10000; for(i=1;i<=n;i++) { pre[i]=i; scanf("%d",&point[i]); mmin=min(mmin,point[i]); } for(i=1;i<=m;i++) { scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w); edge[i].w=edge[i].w*2+point[edge[i].x]+point[edge[i].y]; } sort(edge+1,edge+m+1,cmp); int sum=0; for(i=1;i<=m;i++) { int fx=find(edge[i].x); int fy=find(edge[i].y); if(fx!=fy) { sum=sum+edge[i].w; pre[fx]=fy; } } printf("%d\n",sum+mmin); } return 0; }
相关文章推荐
- algorithm库介绍之---- stable_sort()方法 与 sort()方法
- sogou.com的问题
- HDU——1397Goldbach's Conjecture(二分查找+素数打表)
- POJ 2195 Going Home(二分图最优匹配)
- 【Django】Django命令(Manager.py)
- Google GFS文件系统深入分析
- golang协程——通道channel阻塞
- 迪克斯特拉算法-- Dijkstra's Algorithm
- 异常com.google.gson.internal.StringMap cannot be cast to XXX解决方案
- import pymongo exceptions.ImportError: No module named pymongo
- Go conversion between struct and byte
- 利用Google浏览器自定义搜索
- nanyangoj 7 街区最短路径问题
- adaboost的过程要点理解
- usaco 1.3.1 Greedy Algorithm
- rust的应用程序和cargo 的基本使用
- Intellij之Go插件安装
- Lingo中集与数据的设置
- mongo学习笔记 -1
- mongo学习笔记 -索引 -主键