题目链接:

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. Explaining Delegates in C# - Part 2 (Events 1)

    In my previous post, I spoke about a few very basic and simple reasons of using delegates - primaril ...

  2. Ansible的Inventory管理

    Ansible将可管理的服务器集合成为Inventory,Inventory的管理便是服务器的管理. hosts文件的位置: /etc/ansible/hosts 在命令行通过-i参数指定 通过/et ...

  3. mysql分组查询获取组内某字段最大的记录

    id sid cid 1 1 12 1 23 2 1 以sid分组,最后取cid最大的那一条,以上要取第2.3条 1 方法一: select * from (select * from table o ...

  4. codeforces水题100道 第十六题 Codeforces Round #164 (Div. 2) A. Games (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/268/A题意:足球比赛中如果主场球队的主场球衣和客队的客队球衣颜色一样,那么要求主队穿上他们的可对 ...

  5. mysql 创建merge表方便查询

    SELECT COUNT(*) FROM `comment` SHOW CREATE TABLE `comment` CREATE TABLE `comment1` ( `id` ) NOT NULL ...

  6. Glide加载图片缓存库出现——You cannot start a load for a destroyed activity

    请记住一句话:不要再非主线程里面使用Glide加载图片,如果真的使用了,请把context参数换成getApplicationContext.

  7. MongoDB开篇

    1.安装MongoDB  官方下载地址  https://www.mongodb.com/download-center#community 这个文件下载的有些奇怪,这个zip的文件下载下来和百度出来 ...

  8. JS函数匿名替换

    //匿名替换函数 function objFunc() { var obj = new Object(); obj.JsonData = [{ aa: "}], obj.FilterData ...

  9. django进阶-1

    前言: 各位久等了,django进阶篇来了. 一.get与post 接口规范: url不能写动词,只能写名词 django默认只支持两种方式: get, post get是获取数据 ?user=zcl ...

  10. 题目1447:最短路(Floyd算法)

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