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.然后在那些其一个端点已在生成树里.另一端点还未在生成树里的所有边中找出权最小 ...
随机推荐
- hdu 4405 Aeroplane chess(概率+dp)
Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...
- web前端之 CSS
CSS概述 CSS 指层叠样式表 (Cascading Style Sheets),说白了就是给html代码穿上好看的衣服,让页面变得好看 CSS存在形式 1.在标签的属性中设置,优先级较高 代码如下 ...
- Mysql 分别按月, 日为组group,进行统计排序order
在数据库中我们经经常使用sql语句对表进行简单的统计排序,对于日期字段.我们能够简单的对其进行order. 对于复杂一点的能够按日期中的年.月,日分别进行group,order. 按年份进行group ...
- Dalvik虚拟机简要介绍和学习计划
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8852432 我们知道,Android应用程序是 ...
- iOS多线程及其感悟
感觉每天都是匆匆忙忙的,每天似乎都是时间不够用一样,可是等真的想要动手敲代码的时候才发现,原来还有好多好多的知识点不是太熟练,所以,人不可以一直感觉自我良好, 有时间就是那种自我感觉良好的心态毁了自己 ...
- photoshop动作面板批量处理图片边框技巧
1,想给图片加上边框,在不改变图片大小的前提下,可以这样做:ctrl+a,全选图片,然后“编辑”-----“描边”,在跳出来的选项卡里面可以设置边框颜色,大小,位置,及混合模式, ,我们设置好了,就可 ...
- Canvas绘图方法和图像处理方法(转)
转自:http://javascript.ruanyifeng.com/htmlapi/canvas.html 概述 Canvas API(画布)用于在网页实时生成图像,并且可以操作图像内容,基本上它 ...
- Response.ContentType 详细列表 (转载)
不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% response.ContentType =" ...
- 一个项目涉及到的50个Sql语句(整理版)
/* 标题:一个项目涉及到的50个Sql语句(整理版) 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句. */ --1.学生表Student(S,Sname,Sage,Sse ...
- UICollectionView的简单使用和常用代理方法
UICollectionView相对于UITableView有更加自由的布局,做出的界面可变性更大最近开始接触使用UICollectionView,整理了一下常用的代理方法 首先需要先添加UIColl ...