hdu-1162 Eddy's picture---浮点数的MST
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1162
题目大意:
给n个点,求MST权值
解题思路:
直接prim算法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 1e9 + ;
double Map[maxn][maxn];
double lowcost[maxn];
int mst[maxn];
int n, m;
double prim(int u)
{
double ans = ;
for(int i = ; i <= n; i++)
{
lowcost[i] = Map[u][i];
mst[i] = u;
}
mst[u] = -;
for(int i = ; i < n; i++)
{
double minn = INF * 1.0;
int v = -;
//寻找lowcost数组里面的未加入mst的最小值
for(int j = ; j <= n; j++)
{
if(mst[j] != - && lowcost[j] < minn)
{
v = j;
minn = lowcost[j];
}
}
if(v != -)
{
mst[v] = -;
ans += lowcost[v];
for(int j = ; j <= n; j++)
{
if(mst[j] != - && lowcost[j] > Map[v][j])
{
lowcost[j] = Map[v][j];
mst[j] = v;
}
}
}
}
printf("%.2f\n", ans);
}
struct node
{
double x, y;
}a[maxn];
double dis(node a, node b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main()
{
while(scanf("%d", &n) != EOF)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)Map[i][j] = INF * 1.0;
memset(lowcost, , sizeof(lowcost));
for(int i = ; i <= n; i++)
{
scanf("%lf%lf", &a[i].x, &a[i].y);
}
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)
{
double d = dis(a[i], a[j]);
Map[i][j] = Map[j][i] = d;
}
}
prim(); }
return ;
}
hdu-1162 Eddy's picture---浮点数的MST的更多相关文章
- hdu 1162 Eddy's picture (Kruskal 算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1162 Eddy's picture(最小生成树算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1162 Eddy's picture (最小生成树)(java版)
Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...
- HDU 1162 Eddy's picture
坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- hdu 1162 Eddy's picture (最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1162 Eddy's picture (prim)
Eddy's pictureTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1162 Eddy's picture (最小生成树 prim)
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- HDU 1162 Eddy's picture (最小生成树 普里姆 )
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- 题解报告:hdu 1162 Eddy's picture
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- hdu 1162 Eddy's picture(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...
随机推荐
- 清北刷题冲刺 11-01 a.m
立方体 /* 输入数据中的p的位置是没有用的,而题目本质上是求C(n,k) */ #include<iostream> #include<cstdio> #define mod ...
- bzoj3681: Arietta(网络流)
传送门 主席树优化建图? 然而我连代码都看不懂 贴个题解好了->这里 //minamoto #include<iostream> #include<cstdio> #in ...
- springboot批量导入excel数据
1 背景 小白今天闲着没事,在公司摸鱼,以为今天有事无聊的一天,突然上头说小子,今天实现一下批量导入Excel数据吧,当时我的内心是拒绝的,然后默默打开idea. 2 介绍 2.1 框架 java本身 ...
- jsp内置对象request使用方法2
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.Date ...
- springboot 使用 mybatis + mapper
首先引入相关pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- oracle数据库的导入导出命令
说明:将以下命令复制到cmd命令行中运行即可,file代表文件名数据导入imp zzbweb/zzbweb@orcl file=e:\zzbweb.dmp fromuser=zzbweb touser ...
- 策略模式(Strategy
Strategy 无论什么程序,其目的都是解决问题.而为了解决问题,我们又需要编写特定的算法.使用Strategy模式可以整体地替换算法的实现部分.能够整体地替换算法,能让我们轻松地以不同的算法去解决 ...
- 【转】grunt动态生成文件名
动态生成文件名 expand 设置为true打开以下选项 cwd 所有src指定的文件相对于这个属性指定的路径 src 要匹配的路径,相对与cwd dest 生成的目标路径前缀 ext 替换所有生成的 ...
- Java文件与io——字符流
Writer写入字符流的抽象类.对文件的操作使用:FileWriter类完成 Reader读取字符的抽象类. public class CharDemo { /** * @param args */ ...
- 系统启动与Linux的初始化_1
boot 的起源: the term bootstrap refers to a person who tries to stand up by pulling her own boots. (Und ...