title: poj-2421-最小生成树刷题

date: 2018-11-20 20:30:29

tags:

  • acm
  • 刷题

    categories:
  • ACM-最小生成树

概述

做了几道最小生成树的题,,,都是些板子题,,,直接套板子就能过,,,有一些是在输入数据做文章,,处理一下再建图就行了,,,

这道最小生成树的题稍微需要处理一下,,不过之后也就是套板子了,,,

题意分析

大致的题意就是给出n个村庄之间的距离,,,然后再给出几个村庄之间已经存在的路径,,,然后让你再添加几条路径使得所有的路径的和最小,,,问你添加的这个值是多少,,,

之前做的那几道题都是图已经弄好,,,路径是给定的问你最小的权重之和,,,这道题相当于给你部分图问你最小的权重和,,,

其实只要在加边建图的时候把给的边的权重置为0当作这条边可以走,但我们不算权重,,这样跑一遍最小生成树就能得到答案,,,

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <algorithm> using namespace std; const int maxn = 100;
const int maxm = 1e5 + 5; int mp[maxn][maxn];
int father[maxn];
bool vis[maxn];
int n , m;
int tot; struct edge
{
int u , v , w;
bool operator < (const edge &r) const
{
return w < r.w;
}
}edge[maxm];
void addedge(int _u , int _v , int _w)
{
edge[tot].u = _u;
edge[tot].v = _v;
edge[tot++].w = _w;
}
int find(int x)
{
if(x == father[x]) return x;
else return father[x] = find(father[x]);
}
int kruskal()
{
for(int i = 1; i <= n; ++i)
father[i] = i;
sort(edge , edge + tot);
int cnt = 0;
int sum = 0;
for(int i = 1; i < tot; ++i)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if(t1 != t2)
{
father[t1] = t2;
sum += edge[i].w;
++cnt;
}
if(cnt == n - 1) break;
}
if(n < n - 1) return -1;
else return sum;
}
int main()
{
while(scanf("%d" , &n) != EOF)
{
int u , v , w;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
scanf("%d" , &w);
addedge(i , j , w);
addedge(j , i , w);
} scanf("%d" , &m);
for(int i = 1; i <= m; ++i)
{
scanf("%d%d" , &u , &v);
addedge(u , v , 0);
addedge(v , u , 0);
//无向图记得正反都要加边,,,少加了一个wa了一发,,,,QAQ
}
printf("%d\n" , kruskal());
}
}

(end)

poj-2421-最小生成树刷题的更多相关文章

  1. Constructing Roads POJ - 2421 最小生成树板子题

    #include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...

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

    思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将    所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...

  3. Networking POJ - 1287 最小生成树板子题

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  4. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  5. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  6. Poj(2421),Prim最小生成树

    题目链接:http://poj.org/problem?id=2421 最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法. #include <s ...

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

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  8. POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  9. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  10. 【刷题记录】BZOJ-USACO

    接下来要滚去bzoj刷usaco的题目辣=v=在博客记录一下刷题情况,以及存一存代码咯.加油! 1.[bzoj1597][Usaco2008 Mar]土地购买 #include<cstdio&g ...

随机推荐

  1. (转)每天一个linux命令(9):touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1 基本使用 1.命令格式: touch [选项]... 文件... 2.命令参数 ...

  2. maven构建SSM项目

    对于小型项目来说,使用默认的maven配置项目即可,可是现在分布式项目越来越多,如果巧妙的使用maven部署项目这成了关键. maven的pom.jar.war: 要正确部署得对maven的pom.w ...

  3. jquery使用ajax

    前端jquery使用ajax的几种方法: $.ajax使用: $.ajax({ url:'/test_ajax', #发送url data:{a:,b:,csrfmiddlewaretoken:'{{ ...

  4. 第11月第31天 keyboardwillshow CGAffineTransformMakeTranslation

    1. - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)registerN ...

  5. pt-table-checksum 3.0.4检测不出主从差异数据

    群里好几位同学问 pt-table-checksum 3.0.4, 主从两个表数据是不一致,为啥检测不出来?前段时间自己也测试过,只是没整理成随笔^_- 一.基本环境 VMware10.0+CentO ...

  6. ZYNQ. DMA基本用法

    DMA环路测试 vivadoblock zynq7 + dma +fifo sdk 中可以导入 demo demo 中 默认都是 一个字节8bit数据 的测试程序. 如果是其他长度的数据,不仅要修改数 ...

  7. springMVC非注解常用的"处理器映射器"、"适配器"、"处理器"

    非注解处理器映射器1. org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping url 到bean name的映射2. or ...

  8. Linux下select&poll&epoll的实现原理(一)【转】

    转自:http://www.cnblogs.com/lanyuliuyun/p/5011526.html 最近简单看了一把 linux-3.10.25 kernel中select/poll/epoll ...

  9. USB协通讯议--深入理解【转】

    转自:http://blog.csdn.net/myarrow/article/details/8484113 0. 基本概念 一个[传输](控制.批量.中断.等时):由多个[事务]组成: 一个[事务 ...

  10. 让linux中 history显示每条命令的操作时间及操作用户【转】

    一.history 中显示日期时间用户名的办法 history 命令,用来显示命令行上的操作记录 不过默认是仅显示操作命令行本身,而没有记录操作时间等细节 例如 这样,我们查找记录时很麻烦,想回顾下某 ...