Highways
poj1751:http://poj.org/problem?id=1751
题意:给你n个城市,每个城市的坐标给你,然后为了是每个城市都连通,需要在已经建了一些街道额基础上,再次建一些街道使其连通,求使得所建街道最短的那些边。
题解:直接求一棵生成树,把已经建好的边的边权设置为0,用prim走一遍。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define INF 100000000.0
using namespace std;
struct Node{
int x;
int y;
}node[];//储存村庄
double g[][];
double lowcost[];
bool flag;
double juli(Node a,Node b){//求距离 ,注意数据类型
double xx=pow(((double)a.x-b.x),);
double yy=pow(((double)a.y-b.y),);
return sqrt(xx+yy);
}
int n,m,nearvex[],u,v1;
void prim(int v0){
flag=false;//标记是否需要建边
for(int i=;i<=n;i++){
lowcost[i]=g[v0][i];
nearvex[i]=v0;
}
nearvex[v0]=-;
for(int i=;i<n;i++){
double min=INF;//注意这里的数据类型,不能换成int
int v=-;
for(int j=;j<=n;j++){
if(nearvex[j]!=-&&lowcost[j]<min){
v=j;
min=lowcost[j];
}
}
if(v!=-){
if(lowcost[v]!=){
flag=true;
printf("%d %d\n",nearvex[v],v);//找到就输出
}
nearvex[v]=-;
for(int k=;k<=n;k++){
if(nearvex[k]!=-&&g[v][k]<lowcost[k]){
nearvex[k]=v;
lowcost[k]=g[v][k];
}
}
}
}
if(!flag)printf("\n");//没有要建的边,就输出空行
}
int main(){
while(~scanf("%d",&n)){
for(int i=;i<=n;i++)//存点
scanf("%d%d",&node[i].x,&node[i].y);
for(int i=;i<=n;i++)//建图
for(int j=i+;j<=n;j++){
g[i][j]=g[j][i]=juli(node[i],node[j]);
}
for(int i=;i<=n;i++)//初始化
for(int j=;j<=n;j++){
if(i==j)g[i][j]=;
else if(g[i][j]==)g[i][j]=INF;
}
scanf("%d",&m);
for(int i=;i<=m;i++){//处理已经不需要建的边
scanf("%d%d",&u,&v1);
g[u][v1]=g[v1][u]=;
}
prim(); }
}
Highways的更多相关文章
- H:Highways
总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...
- Highways(prim & MST)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23421 Accepted: 10826 Descri ...
- poj2485 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- UVa 1393 (容斥原理、GCD) Highways
题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
- Highways poj 2485
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
随机推荐
- TREEVIEW节点拖拽
http://files.cnblogs.com/xe2011/TreeView_Drag_and_Drop.rar 假设把A节点往B节点上拖拽 那么 A 为Node1,B为Node2 ...
- A different twist on pre-compiling JSPs--reference
I’ve blogged about this topic earlier and expressed my frustrations as to how web containers don’t p ...
- U盘安装centos 6.4教程(总算是弄好了
参考:http://blog.chinaunix.net/uid-27666459-id-3342477.html http://www.linuxidc.com/Linux/2011-05/3569 ...
- Thread Runnable 多线程
1. 实现多线程的两种方法 a) 让这个类继承java.lang.Thread,然后重写run方法 b) 让这个类实现 java.lang.Runnable接口,实现r ...
- codevs 访问艺术馆
/* codevs 1163 访问艺术馆 红果果的树形dp*/ #include<iostream> #include<cstdio> #include<cstring& ...
- JavaScript Math对象
Math对象是JavaScript的一个固有对象,其作用是执行常见的算数任务.该对象的使用并不是像Date,Array对象使用 new关键字来得到对象,而是直接 Math.[{property|met ...
- 万能的everything彻底解决mysql问题
万能的everthing D:\RECYCLER\S-1-5-21-1214440339-1123561945-1801674531-500\Dd16.0\my.ini可能之前的电脑的隐藏文件没有删除 ...
- 关于C#中的DateTime类型的技巧
* datetime.now.tostring()方法默认的你是无法得到全部的时间的格式的,只能得到日期,得不到具体时间,如果要具体时间,就应该使用 datetime的tostring()重载,dat ...
- [原创] SQLite数据库使用清单(下)
上文两章对SQLite的功能.语法.和操作进行了介绍,本文讲解SQLite的一些高级语法和操作. 3.
- SimpleDateFormat使用详解
http://blog.csdn.net/gubaohua/article/details/575488 public class SimpleDateFormat extends DateForma ...