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插件-省市联动
由于项目需要需要实现一个省市联动,由于业务有一些特殊的需求,使用现有的插件略有不便,就自己实现了一个. 首先需要保存地区数据的JS数据文件,我这里命名为areaData.js,内容如下 ...
- c++ 友元类
一.友元类相关概念 要将私有成员数据或函数暴露给另一个类,必须将后者声明为友元类. 注意三点: (1)友元关系不能传递 (2)友元关系不能继承 (3)友元关系不能互通
- paip.提升性能--- mysql 建立索引 删除索引 很慢的解决.
paip.提升性能--- mysql 建立索引 删除索引 很慢的解决. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blo ...
- JAVA中的正则表达式--待续
1.关于“\”,在JAVA中的正则表达式中的不同: 在其他语言中"\\"表示为:我想要在正则表达式中插入一个普通的反斜杠: 在Java中“\\”表示为:我想要插入一个正则表达式反斜 ...
- Spark常用函数讲解之键值RDD转换
摘要: RDD:弹性分布式数据集,是一种特殊集合 ‚ 支持多种来源 ‚ 有容错机制 ‚ 可以被缓存 ‚ 支持并行操作,一个RDD代表一个分区里的数据集RDD有两种操作算子: Trans ...
- Gym Class(拓扑排序)
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER
重启oracle是提示错误ORA-00119: invalid specification for system parameter LOCAL_LISTENER. 解决方法: 命令查看错误信息:oe ...
- 为什么报错说req未定义,createServer只接受匿名函数吗?
var http = require('http');var server = new http.createServer(handlerRequest(req,res));server.listen ...
- jquery简单切换插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- mvc Html.RenderAction方法解析
@{Html.RenderAction("Listview--控制器里面方法", "Home--控制器名", new { cid = "bda347f ...