图论trainning-part-1 F. Highways
F. Highways
64-bit integer IO format: %lld Java class name: Main
Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.
Output
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2
Sample Output
1 6
3 7
4 9
5 7
8 3 解题:找出最小生成树的父节点。。。。。。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
int mp[maxn][maxn];
struct pos {
int x,y;
} p[maxn];
int n,m,d[maxn],pre[maxn];
int dis(const pos &a,const pos &b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
void prim(int src) {
int i,j,index,theMin;
for(i = ; i <= n; i++) {
pre[i] = src;
d[i] = mp[src][i];
}
pre[src] = -;
for(i = ; i < n; i++) {
theMin = INF;
index = -;
for(j = ; j <= n; j++) {
if(pre[j] != - && d[j] < theMin) theMin = d[index = j];
}
if(theMin != ) printf("%d %d\n",index,pre[index]);
pre[index] = -;
for(j = ; j <= n; j++) {
if(pre[j] != - && d[j] > mp[index][j]) {
d[j] = mp[index][j];
pre[j] = index;
}
}
}
}
int main() {
int i,j,u,v;
scanf("%d",&n);
for(i = ; i <= n; i++) scanf("%d %d",&p[i].x,&p[i].y);
for(i = ; i <= n; i++) {
for(j = i+; j <= n; j++)
mp[i][j] = mp[j][i] = dis(p[i],p[j]);
}
scanf("%d",&m);
while(m--) {
scanf("%d%d",&u,&v);
mp[u][v] = mp[v][u] = ;
}
prim();
return ;
}
图论trainning-part-1 F. Highways的更多相关文章
- 图论介绍(Graph Theory)
1 图论概述 1.1 发展历史 第一阶段: 1736:欧拉发表首篇关于图论的文章,研究了哥尼斯堡七桥问题,被称为图论之父 1750:提出了拓扑学的第一个定理,多面体欧拉公式:V-E+F=2 第二阶段( ...
- Mysql_以案例为基准之查询
查询数据操作
- [ACM_图论] Highways (变形说法的最小生成树)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28972#problem/C 题目给出T种情况,每种情况有n个城镇,接下来每一行是第i个城 ...
- 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs
题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...
- 【图论好题】ABC #142 Task F Pure
题目大意 给定一个 $N$ 个点 $M$ 条边的有向图 $G$,无重边.自环.找出图 $G$ 的一个导出子图(induced subgraph) $G'$,且 $G'$ 中的每个点的入度和出度都是 1 ...
- SPOJ HIGH Highways
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- F, A, MS, QM, RF的OFFER和经历 -- Final update
昨天收到FB的电话,我的OFFER已经批下来了,这也意味着我的JOB HUNTING结束了,下 面是我这两个月来申请结果汇总: Applications (7): Facebook, Google, ...
- CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)
CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...
随机推荐
- 基于Matlab的标记分水岭分割算法
转自:http://blog.sina.com.cn/lyqmath 1 综述 Separating touching objects in an image is one of the more d ...
- 一个普通Java程序包含哪些线程??
package com.java.threads; import java.lang.management.ManagementFactory; import java.lang.management ...
- ios,弹窗遮罩滚动穿透解决方案
- CF1081C Colorful Bricks
思路: dp[i][j]表示到第i个砖块为止共计有j个砖块和它左边的砖块颜色不同. 实现: #include <bits/stdc++.h> using namespace std; ty ...
- 在每天黄金时刻将数据库中数据获取包装成Excel表
过程: 1.由Timer对象实现安排指定的任务在指定的时间进行重复的固定的延迟操作 a.设定时间间隔24小时:PERIOD_DAY = 24 * 60 * 60 * 100; b.指定每天执行操作的时 ...
- 精仿百思不得姐客户端应用iOS源码
XFBaiSiBuDeJie 高仿百思不得姐客户端 初次学习使用RAC,还不是怎么熟悉,使用的仍是MVC模式,MVVM还在摸索中... 如果大家觉得还不错,请给颗星星支持下~~~ 程序中使用到的库 A ...
- 关于HTML5中Video标签无法播放mp4的解决办法
1.首先先排除掉代码问题.路径问题.浏览器不支持问题等常规问题,这些问题另行百度. <video width="500px" height="300px" ...
- Codeforces Round #318 (Div. 2) C Bear and Poker (数学)
简单题,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. 求lcm,爆了long long.我得好好反省一下,对连乘不敏感 #include<bits/stdc++.h&g ...
- C#中Json进行序列化时去掉值为null的节点
当我们用json文件为数据源时,并对json数据进行操作时可能会产生一些数值为null的节点生成,想要去掉null的节点需要一些操作 本文用一个简单的工具对json进行操作 工具:Newtonsoft ...
- C++基础:虚函数、重载、覆盖、隐藏<转>
转自:http://www.2cto.com/kf/201404/291772.html 虚函数总是跟多态联系在一起,引入虚函数可以使用基类指针对继承类对象进行操作! 虚函数:继承接口(函数名,参数, ...