题目链接:

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. PHP代码审计笔记--文件包含漏洞

    有限制的本地文件包含: <?php include($_GET['file'].".php"); ?> %00截断: ?file=C://Windows//win.in ...

  2. SpringBoot 常见问题记录

    问题一 Error starting ApplicationContext. To display the auto-configuration report re-run your applicat ...

  3. 关于丢失或者损坏/etc/fstab文件后的一些探讨

    1.模仿,假设不小心删除了/etc/fstab文件:大家都知道,Linux系统启动的时候会读取该文件来挂载分区,如果缺失该文件,系统一般不能正常启动. 2.采用reboot命令或者alt+ctrl+d ...

  4. thinkphp 控制器unset删除对象变量失败。。

    今儿开发过程中发现 tp是unset 变量失败..具体代码 foreach( $this->menu as $k => $v){ if(0 == $v['flag']) unset($th ...

  5. Matlab练习——矩阵和数组的操作

    题目来自:<战胜MATLAB必做练习50道> 题目有更改,改成了我想写的样子. 1. 创建一个3×3矩阵,并将其扩充为4×5矩阵 clear; clc; mat1 = ones(,) ma ...

  6. Qt——文件对话框

    教程:https://www.devbean.net/2012/09/qt-study-road-2-file-dialog/ 代码如下: //mainwindow.h #ifndef MAINWIN ...

  7. 树莓派3安装opencv2程序无法运行

    在raspberry pi3 上安装opencv3已测试,没有问题,而opencv2报错如下: Xlib: extension "RANDR" missing on display ...

  8. icon VS html特殊字符

    好久没来了,最近项目很多,今天要说的是个页面上用到的icon. 话“icon” 现在有很多icon库,我们再也不用切图来适配不同的分辨率了,但是对于新手来说,查阅icon库来找到适合的icon,实在费 ...

  9. mongodb gridfs基本使用

    Mongodb GridFS图片文件存储解决方案 之前解决方案是接收图片数据后,将图片直接存储到盘阵,然后通过Apache做服务器,将图片信息存储到数据库,并且存储一个Apache的访问路径. 目前需 ...

  10. Yet another way to manage your NHibernate ISessionFactory

    So here is my current UnitOfWork implementation.  This one makes use of the somewhat new current_ses ...