题目

比我想象地要容易很多。。一开始想得太复杂了,本来想试一下kruskal算法的,嫌麻烦。。还是用了之前1203的prim算法。。。以为要注意这道题的输出顺序,结果不用,直接输出就可以了,就是注意一下空行的输出就行了

//#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include <math.h>
using namespace std;
int town[750][2];
double dis[750][750];
double ans;
int n;
void prim()
{
int temp[750]; //存放已经加入的结点
int size; // 已加入的结点个数
int i, j, k;
int lastnode1 = 0, curnode, pos2;
double min; temp[0] = 0;
size = 1; dis[0][0] = 1; for (i = 0; i < n - 1; i++)//执行n-1次将所有的点访问完
{
min = 32767; // 极大值
for (j = 0; j < size; j++)
{
curnode = temp[j];
for (k = 0; k < n; k++)
if (dis[curnode][k] <= min && dis[k][k] == 0) //min 为当前最小值,为0表示没有访问过,如果新加入结点后有再小的边就将对应的点加入
{
min = dis[curnode][k];
lastnode1 = curnode; pos2 = k;
}
}
if (min != 0)
{ cout << lastnode1 + 1 << " " << pos2 + 1 << endl;//ans += min;
}
dis[pos2][pos2] = 1;//表示已经访问过
temp[size] = pos2; size++; }
}
int main()
{
int T, M = -1, E;
cin >> T;
while (T--)
{
memset(dis, 0, sizeof(dis));
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> town[i][0] >> town[i][1];//输入town 的坐标
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)//计算每两个点之前的距离
{ dis[j][i] = dis[i][j] = sqrt(pow(town[i][0] - town[j][0], 2) + pow(town[i][1] - town[j][1], 2)); //计算两点间的距离
}
}
cin >> E;
int x, y;
while (E--)
{
cin >> x >> y;
dis[x - 1][y - 1] = 0;
dis[y - 1][x - 1] = 0; } prim();
if (T)
cout << endl;
}
return 0;
}

ZOJ 2048 highways的更多相关文章

  1. POJ 1751 Highways (ZOJ 2048 ) MST

    http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...

  2. ZOJ 2048(Prim 或者 Kruskal)

    Highways Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The island nation of F ...

  3. Easy 2048 Again - ZOJ 3802 像缩进dp

    Easy 2048 Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Dark_sun knows that on a single-tr ...

  4. ZOJ 3802 Easy 2048 Again 像缩进DP

    链接:problemId=5334">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5334 题意:一个长度为5 ...

  5. ZOJ 3802 Easy 2048 Again 状态DP

    zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧 有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方 所以用状态压缩表示前面有的序列 ...

  6. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  7. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. ZOJ 4114 Detect the Virus(AC自动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

随机推荐

  1. Linux中的命令 make -f 是什么意思

    出处:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422460 ...

  2. iOS的生命周期

    iOS应用程序一般都是由自己编写的代码和系统框架组成.系统框架提供了一些基本的infrastructure给APP来运行,而开发者则自己编写代码定制APP的外观和行为,了解iOS infrastruc ...

  3. Codefroces 750D:New Year and Fireworks(BFS)

    http://codeforces.com/contest/750/problem/D 题意:烟花会绽放n次,每次会向前推进t[i]格,每次绽放会向左右45°绽放,问有烟花的格子数. 思路:n = 3 ...

  4. 判断浏览器是pc端还是手机端

    1. 判断浏览器是pc端还是手机端 <script type="text/javascript"> var browser = { versions: function ...

  5. 20160712001 SQL server R2 更名

    use mastergoselect @@servername;select serverproperty('servername') sp_dropserver 'BPM-SERVER'gosp_a ...

  6. 【RabbitMQ】CentOS安装RabbitMQ,及简单的Java客户端连接

    在CentOS安装 因Rabbit MQ使用Erlang,所以需要先安装Erlang,安装过程中可能会遇到种种问题,可参考CentOS 6.5安装Erlang/OTP 17.0.然后就可以安装MQ了. ...

  7. 问题-MyBatis不识别Integer值为0的数据

    问题-MyBatis不识别Integer值为0的数据 问题:使用MyBatis的过程中,发现一个值为0的数据,Mybatis所识别,最后定位才发现,是自己的写法有问题, <if test=&qu ...

  8. angularJS: shop chart

    <!DOCTYPE html> <html ng-app="app">   <head>     <meta charset=" ...

  9. PB的datawindow导出到excel文件(使用saveasascii)

    **********************************************************//*函数名称:uf_dwsaveas_excel功能:将数据窗口数据导出EXCEL ...

  10. boost学习笔记(七)---date_time库

    date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...