1002 ProblemB
2016-06-20 10:57
405 查看
题意:
给出点的坐标求连通所有的点画的最短线,结果保留两位小数。
思路:
知道坐标就可以求出两点间距离,然后用最小生成树做。
给出点的坐标求连通所有的点画的最短线,结果保留两位小数。
思路:
知道坐标就可以求出两点间距离,然后用最小生成树做。
// problemB.cpp : 定义控制台应用程序的入口点。 // //#include "stdafx.h" #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<iomanip> using namespace std; double path[102][102]; int flag[102]; double closedge[102]; double ma; double cnt; typedef struct { double x, y; }input; input temp[102]; double calculate(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); } double CreatMST(int n) { int i, j, x; double k; flag[0] = 1; for (i = 1; i<n; ++i) closedge[i] = path[0][i]; for (i = 1; i<n; ++i) { k = ma, x = 1; for (j = 1; j<n; ++j) if (!flag[j] && closedge[j] < k) x = j, k = closedge[j]; flag[x] = 1; cnt += k; for (j = 1; j<n; ++j) if (!flag[j] && closedge[j] > path[x][j]) closedge[j] = path[x][j]; } return cnt; } int main() { int i, j, k, t, x, y, n, m; while (cin>>n) { ma = 0; cnt = 0; memset(temp, 0, sizeof(input) * 102); memset(flag, 0, sizeof(flag)); memset(closedge, 0, sizeof(closedge)); memset(path, 0, sizeof(double) * 102 * 102); for (i = 0; i<n; ++i) cin>>temp[i].x>>temp[i].y; for (i = 0; i<n; ++i) for (j = 0; j<n; ++j) { path[i][j] = calculate(temp[i].x, temp[i].y, temp[j].x, temp[j].y); if (ma < path[i][j]) ma = path[i][j]; } cout << setiosflags(ios::fixed) << setprecision(2) << CreatMST(n) << endl; } return 0; }
相关文章推荐
- 最简洁的PHP程序员学习路线及建议
- github如何删除一个repository(仓库)
- 节流函数
- 获取子窗口的父窗口的兄弟窗口方法
- C# 向Http服务器送出 POST 请求
- selenium webdriver 学习总结-浏览器启动方式(二)
- 中介者模式
- 总结一下切换git地址 重合代码的过程
- 跳台阶问题:一个台阶共有N级,如果一次可以跳1级,也可跳2级,求总共有多少种跳法
- 富文本编辑器-参考
- selenium webdriver 学习总结-元素定位(一)
- 使用jQuery给input标签设置默认值
- flask学习笔记(-Web 表单)
- 【QTP】打开多个页面,多浏览器之间操作
- 三:深入理解Nginx的模块化 (结合源码详解)
- c# ReaderWriterLock类
- 工厂设计模式
- Android开发之完全隐藏软键盘的方法
- flexible.js 移动端自适应框架
- java Date获取年月日时分秒的实现方法