ZOJ 2048(Prim 或者 Kruskal)
Highways
Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The input consists of two parts. The first part describes all towns in the country,
and the second part describes all of the highways that have already been built.
The first line of the input contains a single integer N (1 <= N <= 750),
representing the number of towns. The next N lines each contain two integers,
xi and yi separated by a space. These values give the coordinates of ith town
(for i from 1 to N). Coordinates will have an absolute value no greater than
10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing
the number of existing highways. The next M lines each contain a pair of integers
separated by a space. These two integers give a pair of town numbers which are
already connected by a highway. Each pair of towns is connected by at most one
highway.
Output
Write to the output a single line for each new highway that should be built
in order to connect all towns with minimal possible total length of new highways.
Each highway should be presented by printing town numbers that this highway
connects, separated by a space.
If no new highways need to be built (all towns are already connected), then
the output should be created but it should be empty.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed
by N input blocks. Each input block is in the format indicated in the problem
description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between
output blocks.
Sample Input
1
9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2
Sample Output
1 6
3 7
4 9
5 7
8 3
收获:第一次用prim,了解了下prim模板。
方法1:prim
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 1000
struct Node
{
int x, y;
};
Node node[maxn];
int vis[maxn];
int n;
int dis[maxn];
int pre[maxn];
int map1[maxn][maxn];
void Prim(){
int i,j,k,tmp,ans;
memset(vis, 0, sizeof vis);
for(i=2;i<=n;i++)
{
dis[i] = map1[1][i];
pre[i] = 1;
}
dis[1]=0;
vis[1]=1;
for(i=1;i<n;i++){
tmp=INF; k=1;
for(j=1;j<=n;j++){
if(!vis[j]&&tmp>dis[j]){
tmp=dis[j];
k=j;
}//找出最小距离的节点
}
vis[k]=1;//把访问的节点做标记
for(j=1;j<=n;j++){
if(!vis[j]&&dis[j]>map1[k][j])
{
dis[j]=map1[k][j];
pre[j]=k;
}//更新与k相邻的最短距离
}
}
for(int i = 2; i <= n; i++)
{
if(map1[pre[i]][i] != 0)
{
printf("%d %d\n",i, pre[i]);
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d%d", &node[i].x, &node[i].y);
int a, b;
int m;
scanf("%d", &m);
memset(map1, INF,sizeof map1);
for(int i = 1; i <= n ; i++)
for(int j = i+1; j <= n; j++)
map1[i][j] = map1[j][i] = (node[i].x - node[j].x)*(node[i].x - node[j].x) + (node[i].y - node[j].y)*(node[i].y - node[j].y);
for(int i = 0; i < m; i++)
{
scanf("%d%d", &a, &b);
map1[a][b] = map1[b][a] = 0;
}
Prim();
if(t)
puts("");
}
return 0;
}
2.Kruskal
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 570000
struct Node
{
int x, y;
};
Node node[maxn];
struct Edge
{
int u, v ,w;
bool operator < (const Edge &a) const
{
return w < a.w;
}
};
Edge edge[maxn];
int root[maxn];
int num;
void addedge(int u, int v)
{
edge[num].u = u;
edge[num].v = v;
edge[num].w = (node[u].x - node[v].x)*(node[u].x - node[v].x) + (node[u].y - node[v].y)*(node[u].y - node[v].y);
num++;
}
int n;
void init_root()
{
for(int i = ; i <= n; i++)
root[i] = i;
}
int find_root(int x)
{
int k,j,r;
r=x;
while(r!=root[r])
r=root[r];
k=x;
while(k!=r)
{
j=root[k];
root[k]=r;
k=j;
}
return r;
}
void uni(int a, int b)
{
int x = find_root(a);
int y = find_root(b);
//printf("%d %d\n", x, y);
if(x == y)
return;
else
root[y] = x;
}
int cnt;
void solve()
{
for(int i = ; i < num; i++)
{
int u = find_root(edge[i].u);
int v = find_root(edge[i].v); if(u != v)
{
root[v] = u;
cnt++;
printf("%d %d\n", edge[i].u, edge[i].v);
}
if(cnt == n-)
break;
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d%d", &node[i].x, &node[i].y);
int a, b;
num = ;
int m;
scanf("%d", &m);
cnt = ;
init_root();
for(int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
if(find_root(a) != find_root(b))
{
cnt++;
uni(a, b);
}
}
for(int i = ; i <= n ; i++)
for(int j = i+; j <= n; j++)
addedge(i, j);
sort(edge, edge+num);
solve();
if(t)
puts("");
}
return ;
}
ZOJ 2048(Prim 或者 Kruskal)的更多相关文章
- Prim和Kruskal最小生成树
标题: Prim和Kruskal最小生成树时 限: 2000 ms内存限制: 15000 K总时限: 3000 ms描述: 给出一个矩阵,要求以矩阵方式单步输出生成过程.要求先输出Prim生成过程,再 ...
- 【图论】信手拈来的Prim,Kruskal和Dijkstra
关于三个简单的图论算法 prim,dijkstra和kruskal三个图论的算法,初学者容易将他们搞混,所以放在一起了. prim和kruskal是最小生成树(MST)的算法,dijkstra是单源最 ...
- 图的最小生成树的理解和实现:Prim和Kruskal算法
最小生成树 一个连通图的生成树是一个极小的连通子图,它含有图中所有的顶点,但只有足以构成一棵树的n-1条边.我们将构造连通网的最小代价生成树称为最小生成树(Minimum Cost Spanning ...
- 最小生成树(prim和kruskal)
最小生成树(prim和kruskal) 最小生成树的最优子结构性质 设一个最小生成树是T.如果选出一个T中的一条边,分裂成的两个树T1,T2依然是它们的点集组成的最小生成树.这可以用反证法来证.反着来 ...
- HDU 3080 The plan of city rebuild(prim和kruskal)
The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 最小生成树,Prim和Kruskal的原理与实现
文章首先于微信公众号:小K算法,关注第一时间获取更新信息 1 新农村建设 大清都亡了,我们村还没有通网.为了响应国家的新农村建设的号召,村里也开始了网络工程的建设. 穷乡僻壤,人烟稀少,如何布局网线, ...
- 最小生成树MST算法(Prim、Kruskal)
最小生成树MST(Minimum Spanning Tree) (1)概念 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边,所谓一个 ...
- ZOJ 2048 highways
题目 比我想象地要容易很多..一开始想得太复杂了,本来想试一下kruskal算法的,嫌麻烦..还是用了之前1203的prim算法...以为要注意这道题的输出顺序,结果不用,直接输出就可以了,就是注意一 ...
- Prim和Kruskal求最小生成树
Prim: 算法步骤: 1.任意结点开始(不妨设为v1)构造最小生成树: 2.首先把这个结点(出发点)包括进生成树里, 3.然后在那些其一个端点已在生成树里.另一端点还未在生成树里的所有边中找出权最小 ...
随机推荐
- Jquery的bootstrap在线文本编辑器插件Summernote
http://www.jqcool.net/demo/201407/bootstrap-summernote/ Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线 ...
- [转]jQuery EasyUI 扩展-- 主题(Themes)
主题(Themes)允许您改变站点的外观和感观.使用主题可以节省设计的时间,让您腾出更多的时间进行开发.您也可以创建一个已有主题的子主题. 主题生成器(Theme Builder) jQuery UI ...
- Roman to Integer && Integer to Roman 解答
Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...
- DBA 经典面试题(2)
三.备份恢复类 ================================================================================ 1:备份如何分 ...
- 第14/15讲- Android资源管理
第14/15讲 Android资源管理 Android中的资源是指非代码部分,比如图片.MP3,字符串,XML文件等.在一个android工程中,res和assets是用来保存资源文件的. res和a ...
- 前Google人谈团队管理:针对不同员工的情境管理法和如何选择合理的团队规模
本文作者Tomasz Tunguz是Redpoint Ventures的风险投资人,曾在Google担任产品经理并参与过AdSense项目. 我有一个朋友,他创立了一家很成功的公司,而且还在迅速发展. ...
- New Year Transportation(水)
New Year Transportation Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 设计一个算法,求非空二叉树中指定的第k层(k>1)的叶子节点的个数
思想:採用基于层序遍历的方法. 用level扫描各层节点,若某一层的节点出队后.rear指向该层中最右节点.则将rear赋值给last(对于第一层.last=1).在出队时,若front=last,表 ...
- PHP <<EOF EOF的使用方法
PHP <<EOF EOF的使用方法 <?php $name = '浅水游'; print <<<EOT <html& ...
- 检测浏览器是否支持AJAX
<script type="text/javascript"> function ajaxFunction() { var xmlHttp; try { // Fire ...