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.然后在那些其一个端点已在生成树里.另一端点还未在生成树里的所有边中找出权最小 ...
随机推荐
- C++标准:C++不允许修改任何基本型别(包括指针)的暂时值
从<C++标准库>一书中看到这样一句话:C++不允许修改任何基本型别(包括指针)的暂时值,想了半天,实在不理解.基本类型char,int,float等等还有暂时值?例如int a=2,那么 ...
- Wiggle Sort 解答
Question Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= num ...
- paip.输入法编程---智能动态上屏码儿长调整--.txt
paip.输入法编程---智能动态上屏码儿长调整--.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...
- 第05讲- DDMS中logcat的使用
第05讲 DDMS中logcat的使用 1.DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务.DDM ...
- 基础总结篇之九:Intent应用详解
看似尋常最奇崛,成如容易卻艱辛.北宋.王安石 看似普通的事情其实最不同寻常,并不是简简单单就可以做好的:成功看起来似乎很容易,而成功的过程却充满着艰辛. 对于我们认为很普通的事情,不屑一顾,就永远不会 ...
- 实现Android操作系统11种传感器介绍
在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...
- javascript无缝流畅动画轮播,终于让我给搞出来了。
自己一直想写一个真正能用的轮播图,以前是写过一个,但是不是无缝的轮播,感觉体验很差,这个轮播之前也搞了很多实例,看了很多代码,但是脑子总转不过弯,为什么在运动到一定距离后可以突然转回到原始位置,而没有 ...
- 是什么让我想到开发NFinal
我是从01前开始就接触.net,那时.net还是1.0时代,很多东西都没有.后来.net出了2.0版本.从vs2005开始就使用Webform做网站.当时感觉.net能够拖来拖去,很厉害.参加工作后, ...
- js如何关闭当前页,而不弹出提示框
//关闭当前页面,并且打开新页面,(不提示) function closeWinAndOpen(url) { //利用随机数处理WinName var sWinName = "LR" ...
- [C++程序设计]对“&”和“*”运算符
对“&”和“*”运算符再做些说明:(1) 如果已执行了“pointer_1=&a;”语句,请问&*pointer_1的含义是什么?“&”和“*”两个运算符的优先级别相同 ...