POJ 2421 Constructing Roads
来源:互联网 发布:a 1 pictures 知乎 编辑:程序博客网 时间:2024/06/09 23:53
题目链接:http://poj.org/problem?id=2421
kruskal最小生生成树+并查集,当输入已连接的点时,把他们的祖先统一
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int p[150],n,q,k;struct all{ int u,v,w;}e[10010];int cmp(all x,all y){ return x.w<y.w;}int find(int t){ if(p[t]!=t) p[t]=find(p[t]); return p[t];}void kruskal(){ int ans=0; sort(e+1,e+k+1,cmp); for(int i=1;i<=k;i++) { int X=find(e[i].u); int Y=find(e[i].v); if(X!=Y) { p[X]=Y; ans+=e[i].w; } } printf("%d\n",ans);}int main(){ while(scanf("%d",&n)!=EOF) { int i,j; k=0; for(i=1;i<=n;i++) p[i]=i; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { int a; scanf("%d",&a); k++; e[k].u=i; e[k].v=j; e[k].w=a; } scanf("%d",&q); while(q--) { int x,y; scanf("%d%d",&x,&y); p[find(x)]=p[find(y)]; } kruskal(); } return 0;}
- POJ 2421Constructing Roads
- poj 2421 Constructing Roads
- poj 2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 constructing roads
- POJ-2421-Constructing Roads
- POJ 2421 Constructing Roads
- POJ:2421 Constructing Roads
- poj 2421Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ-2421 Constructing Roads
- POJ 2421 Constructing Roads
- POJ 2421 Constructing Roads
- poj 2421 Constructing Roads
- poj 2421 Constructing Roads
- POJ - 2421 Constructing Roads
- 各位数之和
- postgre constraints
- 每天进步一点点
- android surfaceflinger研究----显示系统
- IOS设计模式之二:Delegate模式
- POJ 2421 Constructing Roads
- 编写lua的c函数是要加extern "C",我用的是vs2010
- C# 发送模板邮件并嵌入N张图片(优化) 以模版img标签为驱动
- android 系统服务大全
- win7安装svnserver服务 [sc] openSCManager 失败 5 拒绝 访问拒绝
- CentOS用yum安装中文输入法
- qt多窗口程序
- NOR型flash与NAND型flash的区别
- Mojosoft.Software.BusinessCards.MX.v4.72.Multilanguage.Win2kXPVISTA7.Regged-BLiZZARD