题目

比我想象地要容易很多。。一开始想得太复杂了,本来想试一下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. uboot补丁的分析

    接下来分析一下韦老师的uboot补丁: -------------------------------------------------------------------------------- ...

  2. Vitrualbox虚拟机网络设置

    来自http://www.douban.com/group/topic/15558388/ VirtualBox的提供了四种网络接入模式,它们分别是: 1.NAT 网络地址转换模式(NAT,Netwo ...

  3. Interpolation particles In Katana

    I write the sphere radius interpolation for katana plugin that can transfer attributes,render attrib ...

  4. UIApplication

    1.UIApplication 是 iPhone 应用程序的开始并且负责初始化并显示 UIWindow,并负责加载应用程序的第一个 UIView 到 UIWindow 窗体中. UIApplicati ...

  5. 小结一下前段时间做的rpgdemo

    虽然说已经是彻底放弃继续做那个demo了(代码结构混乱,想增加新功能非常的不方便),不过还是花了一点心血在里面的,毕竟这是我开始学习unity游戏制作的初衷,不过果然是学的越多越发现自己的不足... ...

  6. Vue 源码解析:深入响应式原理(上)

    原文链接:http://www.imooc.com/article/14466 Vue.js 最显著的功能就是响应式系统,它是一个典型的 MVVM 框架,模型(Model)只是普通的 JavaScri ...

  7. Unity2D 里的场景缩放实现

    闲时以 Unity2D 练手时想实现端游里的场景缩放功能,而网上的代码几乎全是 3D 场景缩放相关,所以我自己编写了个 2D 场景缩放脚本(C#). 代码如下: using UnityEngine; ...

  8. java中变量命名和引用变量的一个坑

    这次有两个主题,第一个太简单啦,就是java中变量的命名规则,纯记忆性东西.第二个主题,就是讨论一下对象引用变量的一个注意点.

  9. blue and red ball

    #include<iostream> #include<cstring> using namespace std; int sum; ]; int n; int head; i ...

  10. 为listview的item添加动画效果

    //动画集合 AnimationSet animationSet = new AnimationSet(true); //alpha动画 Animation animation = new Alpha ...