hdoj1102 Constructing Roads(Prime || Kruskal)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1102
题意
有n个村庄(编号1~n),给出n个村庄之间的距离,开始时n个村庄之间已经有了q条路,现在需要修一条路,这条路连接起所有的村庄,求在已经存在的路径的基础上,最少还需要修多长的路。
思路
普通最小生成树是从零开始构造一棵最小生成树,而这题开始时图中已经有了一些路径,那这些路就不需要被修了,所以将这些路的修理长度置为0,然后使用Prime算法或者Kruskal算法求解即可。
代码
Prime算法:
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std; const int INF = 0x7fffffff;
const int N = + ;
int map[N][N];
int dist[N]; //记录从起点到其余各点的长度,不断更新
int n, q; void prime()
{
int min_edge, min_node;
for (int i = ; i <= n; i++)
dist[i] = INF;
int ans = ;
int now = ;
for (int i = ; i < n;i++)
{
min_edge = INF;
dist[now] = -;
for (int j = ; j <= n; j++)
{
if (j != now && dist[j] >= )
{
if (map[now][j] >= ) //注意是map[now][j]>=0,因为有些路已经修好了,初始化时令其长度为0
dist[j] = min(dist[j], map[now][j]);
if (dist[j] < min_edge)
{
min_edge = dist[j]; //min_edge存储与当前结点相连的最短的边
min_node = j;
}
}
}
ans += min_edge; //ans存储最小生成树的长度
now = min_node;
}
printf("%d\n", ans);
} int main()
{
//freopen("hdoj1102.txt", "r", stdin);
while (scanf("%d", &n) == )
{
memset(map, , sizeof(map));
for (int i = ; i <= n;i++)
for (int j = ; j <= n; j++)
scanf("%d", &map[i][j]);
scanf("%d", &q);
int a, b;
for (int i = ; i < q; i++)
{
scanf("%d%d", &a, &b);
map[a][b] = map[b][a] = ;
}
prime();
}
return ;
}
Kruskal算法:
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std; struct Edge
{
int a, b, dist; Edge() {}
Edge(int a, int b, int d) :a(a), b(b), dist(d) {}
bool operator < (Edge edge) //将边按边长从短到长排序
{
return dist < edge.dist;
}
}; const int N = + ;
int p[N]; //并查集使用
int map[N][N];
vector<Edge> v;
int n, q; int find_root(int x)
{
if (p[x] == -)
return x;
else return find_root(p[x]);
} void kruskal()
{
memset(p, -, sizeof(p));
sort(v.begin(), v.end());
int ans = ;
for (int i = ; i < v.size(); i++)
{
int ra = find_root(v[i].a);
int rb = find_root(v[i].b);
if (ra != rb)
{
ans += v[i].dist;
p[ra] = rb;
}
}
printf("%d\n", ans);
} int main()
{
//freopen("hdoj1102.txt", "r", stdin);
while (scanf("%d", &n) == )
{
v.clear();
int t;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
scanf("%d", &map[i][j]);
scanf("%d", &q);
int a, b;
for (int i = ; i < q; i++)
{
scanf("%d%d", &a, &b);
map[a][b] = map[b][a] = ;
} for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (j > i) v.push_back(Edge(i, j, map[i][j])); kruskal();
}
return ;
}
注意点
这题是有多组输入数据的,如果只按一组输入数据处理的话会WA.
hdoj1102 Constructing Roads(Prime || Kruskal)的更多相关文章
- poj1251 Jungle Roads(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1251 题意 有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度. 思路 最小生成树问题,使用普 ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdoj1879 继续畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1879 思路 这题和hdoj1102很像,图中的有一些路已经修好了,对于这些已经修好的路,我们令还需要修 ...
- poj1258 Agri-Net(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1258 题意 有n个农场,现在要在n个农场之间铺设光纤使得n个农场连接起来,求铺设光纤的最短距离. 思路 最小生成树问题,使用Prime ...
- hdoj1863 畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1863 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决.这题在hdoj1233的基础 ...
- hdoj1233 还是畅通工程(Prime || Kruskal)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1233 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决. 代码 Prime算法: # ...
- Constructing Roads----poj2421(最小生成树Kruskal)
题目链接: http://poj.org/problem?id=2421 想把n个村庄连接在一起:求最小生成树,不同的是已经有了m条线段链接在一起了,求剩下的: 感觉用Kruskal会简单一点 #in ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- Constructing Roads(1102 最小生成树 prim)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake
https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...
- The Ph.D. Grind
The Ph.D. Grind A Ph.D. Student Memoir Summary The Ph.D. Grind, a 122-page e-book, is the first know ...
- Java并发编程原理与实战四十四:final域的内存语义
一.final域的重排序规则 对于final域,编译器和处理器要遵循两个重拍序规则: 1.在构造函数内对一个final域的写入,与随后把这个被构造对象的引用赋值给一个引用变量,这两个操作之间不能重排序 ...
- iscroll demo
下面是自己找网上资料写的一个小demo,基础的属性和方法都有用到,但是用法还不是很标准,github上的demo用法很标准,外面一个wrapper,里面还得有一个scroller,如果要做跑马灯效果还 ...
- 阿里云的OCS缓存机制
OCS简介 OCS( Open Cache Service)为分布式高速缓存服务,主要实现热点数据的快速响应: OCS支持Key/Value的数据结构,兼容memcachebinary protoco ...
- ARC官方文档翻译! - iPhone App开发外包专区 - 威锋论坛 - 威锋网
CHENYILONG Blog ARC官方文档翻译! - iPhone App开发外包专区 - 威锋论坛 - 威锋网 http://bbs.weiphone.com/read-htm-tid-344 ...
- VUE项目用hbuilder 打包为手机APP
一.测试项目是否可以正确运行 指令:npm run dev 首先我们先建立一个vue的项目,本人用的是vue-cli随便建立的,然后运行项目 不必非得是像我这样的,这一步的目的只是测试一下咱们的 ...
- UNIX网络编程 第3章 套接字编程简介
套接字结构类型和相关的格式转换函数
- 2016.07.15——istringstream测试
istringstream测试 1.istringstream strcin(str),字符串(str)可以包括多个单词,单词之间使用空格分开 #include "stdafx.h" ...
- 2016.5.21——atoi()函数的测试
对函数atoi()函数的测试: atoi()函数将字符串型转换为整型 代码: #include "stdafx.h" #include "iostream" # ...