POJ 1751 Highways (kruskal)
题目链接:http://poj.org/problem?id=1751
题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树。
这里用kruskal不会超时,用prim应该会超时,特别注意在输入的时候不要多组输入,否则会TLE。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = ;
struct edge {
int u , v , cost;
}a[MAXN * MAXN];
int x[MAXN] , y[MAXN] , cont , par[MAXN] , cnt , f , ans[MAXN * ];
bool vis[MAXN][MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
par[i] = i;
}
memset(vis , false , sizeof(vis));
cont = ;
cnt = n;
} int Find(int n) {
if(n == par[n])
return n;
return (par[n] = Find(par[n]));
} bool cmp(edge a , edge b) {
return a.cost < b.cost;
} int kruskal() {
for(int i = ; i <= cont ; i++) {
if(cnt == )
break;
int u = Find(a[i].u) , v = Find(a[i].v);
if(u != v) {
ans[f++] = a[i].u;
ans[f++] = a[i].v;
par[u] = v;
cnt--;
}
}
} int main()
{
int n , m , u , v;
scanf("%d" , &n);
{
init(n);
for(int i = ; i <= n ; i++) {
scanf("%d %d" , x + i , y + i);
}
scanf("%d" , &m);
while(m--) {
scanf("%d %d" , &u , &v);
u = Find(u) , v = Find(v);
vis[u][v] = vis[v][u] = true;
if(u != v) {
par[u] = v;
cnt--;
}
}
for(int i = ; i <= n ; i++) {
for(int j = ; j < i ; j++) {
if(vis[i][j])
continue;
cont++;
a[cont].u = j , a[cont].v = i;
a[cont].cost = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);
}
}
sort(a + , a + cont + , cmp);
f = ;
kruskal();
for(int i = ; i < f ; i += ) {
printf("%d %d\n" , ans[i] , ans[i + ]);
}
}
}
POJ 1751 Highways (kruskal)的更多相关文章
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 1751 Highways (ZOJ 2048 ) MST
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
- POJ 1751 Highways(最小生成树&Prim)题解
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- (最小生成树 Prim) Highways --POJ --1751
链接: http://poj.org/problem?id=1751 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1150 ...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
随机推荐
- TCSRM 591 div2(1000)(dp)
挺好的dp 因为有一点限制 必须任意去除一个数 总和就会小于另一个总和 换句话来说就是去除最小的满足 那么就都满足 所以是限制最小值的背包 刚开始从小到大定住最小值来背 TLE了一组数据 后来发现如果 ...
- Android的计量单位px,in,mm,pt,dp,dip,sp
android中dip.dp.px.sp和屏幕密度 1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持 ...
- MSSQL中把表中的数据导出成Insert
use master go if exists (select name from sysobjects where name = 'sp_generate_insert_script') begin ...
- centos使用denyhosts的问题,会将自己的IP自动加到hosts.deny的解决办法。
先吐槽, 很多网站\博客,技术文章也玩伪原创, 害人不浅. 全TMD是COPY, COPY, COPY过来. 拷过来就算了, 你TMD还改了其中的内容... 改成错的.然后众多网站转载, 将错进行到底 ...
- 随机变量的方差variance & 随机向量的协方差矩阵covariance matrix
1.样本矩阵 如果是一个随机变量,那么它的样本值可以用一个向量表示.相对的,如果针对一个随机向量,那么就需要利用矩阵表示,因为向量中的每一个变量的采样值,都可以利用一个向量表示. 然后,一个矩阵可以利 ...
- poj 3352 Road Construction
// 只能说这题和上题一模一样// 我就直接贴上题代码了.. #include <iostream> #include <algorithm> #include <que ...
- 【JS】<select>标签小结
循环时通过<c:if>来判断是否为默认选中 <select name="select" id="month"> <c:forEac ...
- WebView 中重写javascript 常用函数
常规函数 javascript 常规函数包括以下3个函数: (1)alert函数:显示一个警告对话框,包括一个OK按钮. 对应:http://www.dreamdu.com/javascript ...
- Tour
题意: 给n个点的坐标,求形成的最短的闭合回路. 分析: 经典问题,dp[i][j]表示有1-i点再由j回到1点的最短距离,i点有两种情况,在去的路径上 dp[i][j]=min(dp[i][j],d ...
- XTUOJ 1252 Defense Tower 贪心
题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252 思路:考虑每条边对玩家的伤害 假设连接的节点是u,v,破坏 ...