题目链接:

http://poj.org/problem?id=2421

想把n个村庄连接在一起;求最小生成树,不同的是已经有了m条线段链接在一起了,求剩下的;

感觉用Kruskal会简单一点

#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 110
#define INF 0xfffffff using namespace std; int f[N]; struct node
{
int x,y,d;
}a[N*N]; int cmp(node p,node q)
{
return p.d < q.d;
} int Find(int x)
{
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
} int main()
{
int n, i, j, k, m, d, x, y, px, py, ans;
while(scanf("%d",&n)!=EOF)
{
memset(a, , sizeof(a));
k = ans = ;
for(i=;i<=n;i++)
f[i]=i;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d",&d);
if(i<j)
a[k].x = i, a[k].y = j, a[k++].d = d;
}
sort(a,a+k,cmp);
scanf("%d", &m);
for(i=; i<m; i++)
{
scanf("%d%d", &x, &y);
px = Find(x);
py = Find(y);
if(px != py)
f[px] = py;
}
for(i=; i<k; i++)
{
px = Find(a[i].x);
py = Find(a[i].y);
if(px != py)
f[px] = py,ans+=a[i].d;
}
printf("%d\n",ans);
}
return ;
}

Constructing Roads----poj2421(最小生成树Kruskal)的更多相关文章

  1. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  2. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  3. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  4. hdu oj1102 Constructing Roads(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. Constructing Roads(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  6. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  7. POJ2421 Constructing Roads【最小生成树】

    题意: 有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少. 思路: 最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树. 代码: prime: #inc ...

  8. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  9. POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解

    题意: 输入n,然后接下来有n-1行表示边的加边的权值情况.如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25.求连接这棵树的最小权值. 思路: 一开始是在做莫 ...

随机推荐

  1. RAC的搭建(二)--创建ASM磁盘

     1. 规划 表决磁盘: 1Gx3(3节点以下,建议都采用这种配置,三个磁盘加起来要大于1.8G,否则会报错) 数据磁盘: 10Gx1 闪回磁盘: 5Gx1 2. 创建共享磁盘 virtualBox上 ...

  2. oracle nvl,having的用法

    select oi.order_id,opl.payment_no,opl.back_no, oi.commit_time, oi.receive_mobile, oi.receive_user, n ...

  3. XPath的初步认识

    嘿嘿,最近开始上班,不是过于太忙,而是自己一直在学习一些项目中用到的而我暂时还没接触的知识,WCF,log4等,感觉还没有总结的需要吧,虽然都了解啦,但是暂时还是初步的学习,基础的暂时是知道啦,还没有 ...

  4. [Z]修炼成C++高手必看的C++书单

    增添于网上的一些书单: C++/OPP/OOD系列: 层级一:语法/语意(C++) [Lippman2000] Essential C++ Essential C++,by Stanley B. Li ...

  5. C# EF中调用 存储过程并调回参数

    TourEntities db = new TourEntities(); List<v_product> v = new List<v_product>(); SqlPara ...

  6. 原生js(一)

    Element对象有以下重要属性: 1.style. a) Element的css样式 b) 可以通过elem.style.backgroundColor = "red"的形式才动 ...

  7. wmsys.wm_concat结果长度限制的问题

    转:http://bbs.csdn.net/topics/360059765 使用wmsys.wm_concat多列合成一列遇到问题ORA-22813: 操作数值超出系统的限制 官方文档解释是总长度超 ...

  8. 题目1452:搬寝室(dp题目)

    题目链接:http://ac.jobdu.com/problem.php?pid=1452 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  9. [转]redhat7(centos7) not registered to Red Hat Subscription Management

    [root@controller0 ~]# yum install ntp Loaded plugins: fastestmirror, product-id, search-disabled-rep ...

  10. hihoCoder挑战赛28 题目1 : 异或排序

    题目1 : 异或排序 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: ...