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. centos7下设置opencv环境变量

    最近要装YOLO,但是MAKE的时候总是找不到OPENCV的路径, 原因是:我以前卸载过一次OPENCV,然后自己重新安装了opencv2.4.10,  因为当时只在QT 中用,所以编译完也没有设置环 ...

  2. P3932 浮游大陆的68号岛

    P3932 浮游大陆的68号岛 妖精仓库的储物点可以看做在一个数轴上.每一个储物点会有一些东西,同时他们之间存在距离. 每次他们会选出一个小妖精,然后剩下的人找到区间[l,r]储物点的所有东西,清点完 ...

  3. 解决linux mysql命令 bash: mysql: command not found 的方法

    错误: root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这 ...

  4. redis添加systemctl服务

    1.编辑systemctl服务配置文件 vim /lib/systemd/system/redis.service 2.内容如下 [Unit]Description=RedisAfter=networ ...

  5. html5 canvas 圆形径向渐变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. [整理]C 内核源代码-学习资料

    GNU C gnu项目:http://www.gnu.org/software/software.html ftp:http://ftp.gnu.org/gnu/ 托管:http://savannah ...

  7. yii2框架目录

    框架目录结构 [目录] backend——后台web程序 common——公共的文件 console——控制台程序 environments——环境配置 frontend——前台web程序 [文件] ...

  8. 第12月第26天 swift 下划线

    1. The _ is used to define that the parameter is not named If you have multiple _ it states that you ...

  9. android MeasureSpec的三个测量模式

    1.MeasureSpec含义 其实可以去看MeasureSpec的文档,里面对MeasureSpec的作用介绍得很清楚.MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureS ...

  10. lucene删除索引——(五)

    增加在入门程序创建索引中,增删改用IndexWriter. 1.获取IndexWriter的代码 // public IndexWriter getIndexWriter() throws Excep ...