【NOIP2016提高A组模拟7.15】修路
2016-07-17 18:48
232 查看
Description
最小生成树??<( ̄3 ̄)> ,果断克鲁斯卡尔嘿嘿嘿
The Solution
在每条边上减去两边城市的值,题目就转变成最小生成树了。最小生成树??<( ̄3 ̄)> ,果断克鲁斯卡尔嘿嘿嘿
Code
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define fo(i,a,b) for (int i=a;i<=b;i++) #define N 100005 using namespace std; int n,m,Dad ; long long a ,ans=0; struct note { int x,y;long long z; }b ; bool cmp(note a,note b){return a.z<b.z;} int get(int x){return !Dad[x]?x:Dad[x]=get(Dad[x]);} int main() { scanf("%d%d",&n,&m); fo(i,1,n) scanf("%lld",&a[i]); fo(i,1,m) { scanf("%d%d%lld",&b[i].x,&b[i].y,&b[i].z); b[i].z-=a[b[i].x]+a[b[i].y]; } sort(b+1,b+m+1,cmp); int tot=0; fo(i,1,m) { int xx=get(b[i].x),yy=get(b[i].y); if (xx!=yy) { tot++; ans+=b[i].z; Dad[xx]=yy; } if (tot>=n-1) break; } printf("%lld",ans); return 0; }
相关文章推荐
- Prim(普里姆)算法求最小生成树的思想及C语言实例讲解
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- 最小生成树算法之Prim算法
- 使用C语言实现最小生成树求解的简单方法
- 最小生成树算法——Prim和Kruskal算法的实现
- Data Structure - Week 15
- poj 2485 Highways
- HDU1301 最小生成树kruskal裸题
- Kruskal 最小生成树
- 最小生成树
- 图的最小生成树学习笔记
- 克如斯卡尔算法--最小生成树
- 普里姆算法--最小生成树
- poj2395 解题报告
- HDU-1233 还是畅通工程(最小生成树&并查集)
- 最小生成树之prim算法
- 最小生成树 : Kruskal 算法
- 最小生成树 : Prim 算法
- java数据结构 最小生成树
- POJ1251-Jungle Roads