题目

比我想象地要容易很多。。一开始想得太复杂了,本来想试一下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. JNI ReferenceTable overflow

    今天在小米设备上遇到如下问题 10-15 17:04:36.899: W/dalvikvm(2767): ReferenceTable overflow (max=512) 10-15 17:04:3 ...

  2. 基于ThinkPHP开发的PHPExcel导出

    首先我们需要去PHPExcel官网下载开源包 下载好了以后只需要把里面的classes文件夹解压出来就可以用了! 在ThinkPHP中,文件的命名空间还是很重要的,我试着按照其他的第三方命名写name ...

  3. AB窗体互传参数

    一.找了好几个,都不靠谱,不是说不靠谱,自己感觉太繁琐 二.父窗口传子窗口好传,有两种方法(自己认为比较简单的方法哈), 1第一种方法:在子窗口中新建一个属性:再新建一个方法,当然方法就是把属性个窗体 ...

  4. javascript高级程序设计第5章,引用类型

    object类型: 创建object实列的方式有两种,一种是new()方法,一种是对象字面量表示法: 第一种法方:  var obj = new object(); obj.name = 'name' ...

  5. 在SpringMVC框架下实现数据的国际化(即数据实现多国文字之间的转换)

    在eclipse中javaEE环境下:导入必要的架包 web.xml配置文件: <?xml version="1.0" encoding="UTF-8"? ...

  6. ubuntu java开发环境搭建(jdk+tomcat+eclipse)

    一.jdk的安装配置. 1.下载jdk. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...

  7. spring mvc的拦截器

    package com.tech.jin.interceptor.method; import java.util.Arrays; import java.util.Map; import java. ...

  8. AngularJS 学习

    原文链接: https://www.zybuluo.com/frank-shaw/note/509653 Promises in AngularJS, Explained as a Cartoon h ...

  9. window7 x64 path

    %SystemRoot%\system32; %SystemRoot%; %SystemRoot%\System32\Wbem; %SYSTEMROOT%\System32\WindowsPowerS ...

  10. Linux 视频设备驱动V4L2最常用的控制命令

    http://blog.csdn.net/shaolyh/article/details/6583226 Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02) 命令 功能 VIDIOC ...