poj-2421-最小生成树刷题
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-最小生成树刷题的更多相关文章
- Constructing Roads POJ - 2421 最小生成树板子题
#include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
- Networking POJ - 1287 最小生成树板子题
#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...
- 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 ...
- POJ 水题(刷题)进阶
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- Poj(2421),Prim最小生成树
题目链接:http://poj.org/problem?id=2421 最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法. #include <s ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 【刷题记录】BZOJ-USACO
接下来要滚去bzoj刷usaco的题目辣=v=在博客记录一下刷题情况,以及存一存代码咯.加油! 1.[bzoj1597][Usaco2008 Mar]土地购买 #include<cstdio&g ...
随机推荐
- LIS (DP)_代码
#include <stdio.h> #include <string.h> #include <stdlib.h> int max(int a, int b); ...
- gdb调试2—单步执行和跟踪函数
int add_range(int low, int high); int main(int argc, char *argv[]) { int result[100]; result[0] = ad ...
- Helm二:安装
目录 Helm安装 Helm client安装 Helm tiller安装 Chart仓库配置 私有chart仓库 chart仓库的组成 创建本地仓库 chart仓库基本管理 Helm安装 Helm ...
- JAVA记录-IntelliJ Idea 2017 免费激活方法(转载)
1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ http://ide ...
- 赫夫曼树JAVA实现及分析
一,介绍 1)构造赫夫曼树的算法是一个贪心算法,贪心的地方在于:总是选取当前频率(权值)最低的两个结点来进行合并,构造新结点. 2)使用最小堆来选取频率最小的节点,有助于提高算法效率,因为要选频率最低 ...
- MySql数据库资料收集
1.下载MySQL历史版本 https://downloads.mysql.com/archives/community/ https://downloads.mysql.com/archives/i ...
- 【LibreOJ】#6354. 「CodePlus 2018 4 月赛」最短路 异或优化建图+Dijkstra
[题目]#6354. 「CodePlus 2018 4 月赛」最短路 [题意]给定n个点,m条带权有向边,任意两个点i和j还可以花费(i xor j)*C到达(C是给定的常数),求A到B的最短距离.\ ...
- .gitignore 失效问题解决
对于Git,已经跟踪的文件,再加入到.gitignore中,会使忽略失效.使用下面3个命令使它重新生效 git rm -r --cached . git add . git commit -m &qu ...
- RESTful 个人理解总结【转】
转自:http://www.cnblogs.com/wang-yaz/p/9237981.html 一.什么是RESTful 面向资源 简单的说:RESTful是一种架构的规范与约束.原则,符合这种规 ...
- C#中的GetElementsByClassName方法
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public static class Spread { ...