题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1879

这题做的我好尴尬,虽然自己做出来了,感觉也不难,不过怎觉得,

对这个最小生成树的理解,好像总隔了一成迷雾,

题目不难,只要那个排序先优先,修建状态:1表示已建,的1,即已修建的,

再考虑按成本成小到大排序,即可

我的AC代码

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

struct node
{
int x,y,d,f;
}s[5000];

int father[111];
int sum;

bool cmp(const node &a,const node &b)
{
if(a.f!=b.f)
return a.f>b.f;
else
return a.d<b.d;
}

int Find(int x)
{
if(x==father[x]) return x;
father[x]=Find(father[x]);
return father[x];
}

void Union(int x,int y,int d,int f)
{
x=Find(x);
y=Find(y);
if(x!=y)
{
father[x]=y;
if(f==0)
sum=sum+d;
}
}

int main(void)
{
int n,m,i,j,k,l;
while(scanf("%d",&n)==1&&n)
{
m=n*(n-1)/2;
for(i=0;i<m;i++)
scanf("%d%d%d%d",&s[i].x,&s[i].y,&s[i].d,&s[i].f);
sort(s,s+m,cmp);
for(i=1;i<=n;i++)
father[i]=i;
sum=0;
for(i=0;i<m;i++)
{
Union(s[i].x,s[i].y,s[i].d,s[i].f);
}
printf("%d\n",sum);
}
return 0;
}

HDU-1879-继续畅通工程(并查集)的更多相关文章

  1. hdu 1233 还是畅通工程 并查集or最小生成树

    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...

  2. hdu 1879 继续畅通工程

    /************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...

  3. hdu 1879 继续畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    ...

  4. hdu 1863 畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    M ...

  5. HDU - 1232 畅通工程-并查集模板

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  6. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

  7. HDU 1232 畅通工程(并查集)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  8. HDU 1232 (畅通工程) 并查集经典模板题

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...

  9. hdu 1863 畅通工程 (并查集 、 kruskal)

    畅通工程Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. [HDOJ1232]畅通工程(并查集)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1232 题目描述 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表, ...

随机推荐

  1. Kali-linux安装之后的简单设置(转)

    1.更新软件源: 先备份软件源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak 修改sources.list文件: leafpad /etc/ ...

  2. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

  3. Python3基础 函数 默认值参数示例

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  4. vs2005中分割线怎么插入

    用Label控件,将Label控件的AutoSize设为False,然后清除Text属性,再将BorderStyle属性设为Fixed3D,宽度设为2个像素,就可以成为分割线. 效果:

  5. (中等) CF 585D Lizard Era: Beginning,中途相遇。

    In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...

  6. svn branching and merging

    the svn switch command is an alternative way to creating a working copy of a branch :) You can merge ...

  7. Transport layer and Network layer

    http://stackoverflow.com/questions/13333794/networking-difference-between-transport-layer-and-networ ...

  8. C#设置word段落首行缩进为0

    PublicVar.m_WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = ; PublicVar.m_WordApp.S ...

  9. CSS——京东首页实战总结

    第一天成果 1.浮动的盒子不要给宽,内容撑起盒子的宽 在前端设计中,一般不给浮动的盒子设置宽,让其用内容撑起一个高度. 2.小三角的表示 ◇用一个盒子(盒子宽为字的宽,高度为字高度的一半)去截取这个菱 ...

  10. andorid之摄像头驱动流程--MTK平台

    原文地址:andorid之摄像头驱动流程--MTK平台 作者:守候心田 camera成像原理: 景物通过镜头生产光学图像投射到sensor表面上,然后转为模拟电信号,经过数模变成数字图像信号,在经过D ...