(最小生成树 Prim) Highways --POJ --1751
链接:
http://poj.org/problem?id=1751
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 11507 | Accepted: 3279 | Special Judge |
Description
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 first line of the input file 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
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
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
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 800
#define INF 0xffffff struct node {int x, y;}a[N]; int n, m, pre[N];
bool used[N][N], vis[N];
double G[N][N], dist[N]; void prim()
{
int i, j;
for(i=; i<=n; i++)
{
dist[i] = G[][i];
pre[i] = ;
} vis[] = ; for(i=; i<n; i++)
{
int Index = ;
double Min = INF;
for(j=; j<=n; j++)
{
if(!vis[j] && Min>dist[j])
{
Min = dist[j];
Index = j;
}
} vis[Index] = ; for(j=; j<=n; j++)
{
if(!vis[j] && dist[j] > G[Index][j])
{
dist[j] = G[Index][j];
pre[j] = Index;
}
}
}
} int main()
{
while(scanf("%d", &n)!=EOF)
{
int i, j, u, v; for(i=; i<=n; i++)
scanf("%d%d", &a[i].x, &a[i].y); for(i=; i<=n; i++)
for(j=; j<=i; j++)
G[i][j] = G[j][i] = sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y)); scanf("%d", &m);
memset(used, , sizeof(used));
for(i=; i<=m; i++)
{
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = ;
used[u][v] = used[v][u] = ;
} memset(vis, , sizeof(vis));
prim(); for(i=; i<=n; i++)
{
if(!used[pre[i]][i] && !used[i][pre[i]])
printf("%d %d\n", i, pre[i]);
}
}
return ;
}
(最小生成树 Prim) Highways --POJ --1751的更多相关文章
- H - Highways - poj 1751(prim)
某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...
- Highways POJ - 1751
题目链接:https://vjudge.net/problem/POJ-1751 思路: 最小生成树板子,只需要多记录每个dis[x]的权值是从哪个点到x这个点的. #include <stdi ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- Highways POJ-1751 最小生成树 Prim算法
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- 最小生成树Prim算法(邻接矩阵和邻接表)
最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...
- 转载:最小生成树-Prim算法和Kruskal算法
本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...
- 最小生成树Prim
首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...
随机推荐
- VirtualBox“切换到无缝模式”和“自动调整显示尺寸”菜单无法使用
现象:VirtualBox“切换到无缝模式”和“自动调整显示尺寸”菜单无法使能,无法全窗口显示虚拟机桌面,菜单状态如下图所示: 原因:该功能的支持需要安装VirtualBox增强功能 方法:安装Vir ...
- HTML CSS + DIV实现整体布局 part1
HTML CSS + DIV实现整体布局 1.技术目标: 开发符合W3C标准的Web页面 理解盒子模型 实现DIV+CSS整体布局 2.什么是W3C标准? W3C:World Wide Web Con ...
- 扩展C#与元编程(一)
众所周知,Roslyn project已经开源一年多了.简单的说,Roslyn是:1)用C#/VB写的C#/VB的编译器,以及与IDE集成:2)编译器的功能以API的方式暴露出来(即一组DLL). R ...
- 关于关闭TAB,IFRAME占用的内存不能释放问题
资料来源:http://jxd-zxf.iteye.com/blog/1440611 使用TAB时注意,如果TAB是引用IFRAME,关闭TAB时IFRAME不会被销毁从而导致内存不能释放,大量使用T ...
- Cannot create inner bean '(inner bean)#67f903b5' of typ
严重: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error crea ...
- Segments(叉积)
Segments http://poj.org/problem?id=3304 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- [leetcode]199. Binary Tree Right Side View二叉树右视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Linq select 语法
文档:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b 1.可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B&q ...
- Devexpress系列一 之 ChartControl 柱形图BarSideBySideSeries2D
这一篇是讲的是简单的柱形图, 柱形图运行效果如下图: 新建WPF窗体应用程序后(WPF用户控件也可以),工程引用DevExpress.Xpf.Charts.v17.1.dll, 在XAML头部引用名称 ...