H - Highways - poj 1751(prim)
某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就什么都不输出。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std; #define maxn 1005 int f[maxn];
struct node{
int u, v;double len;
friend bool operator < (node a, node b){
return a.len > b.len;
}
};
struct point{int x, y;}p[maxn], ans[maxn];
double Len(point a, point b){
int x = a.x-b.x;
int y = a.y-b.y; return sqrt(x*x+y*y);
}
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int Union(int x, int y)
{
x = Find(x);
y = Find(y); if(x != y)
{
f[x] = y;
return 1;
} return 0;
}
bool cmp(point a, point b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int main()
{
int N; scanf("%d", &N); int M, u, v, t=0, k=0;
node s;
priority_queue<node> Q; for(s.u=1; s.u<=N; s.u++)
{
f[s.u] = s.u;
scanf("%d%d", &p[s.u].x, &p[s.u].y);
for(s.v=1; s.v<s.u; s.v++)
{
s.len = Len(p[s.u], p[s.v]);
Q.push(s);
}
} scanf("%d", &M); while(M--)
{
scanf("%d%d", &u, &v);
t += Union(u, v);
} while(Q.size() && t < N)
{
s = Q.top();Q.pop(); if(Union(s.u, s.v) == 1)
{
t += 1;
ans[k].x = s.v;
ans[k++].y = s.u;
}
} for(int i=0; i<k; i++)
printf("%d %d\n", ans[i].x, ans[i].y); return 0;
}
****************************************************************************
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std; #define maxn 1005
#define oo 0xfffffff double G[maxn][maxn];
struct point{int x, y;}p[maxn], ans[maxn];
struct node{int v;double len;}dist[maxn];
double Len(point a, point b){
int x = a.x-b.x;
int y = a.y-b.y; return sqrt(x*x+y*y);
} void prim(int N)
{
int i, use[maxn] = {0, 1}; for(i=1; i<=N; i++)
dist[i].v = 1, dist[i].len = G[1][i]; for(i=1; i<N; i++)
{
int k=0;double Min = oo; for(int j=1; j<=N; j++)
{
if(!use[j] && Min > dist[j].len)
Min = dist[j].len, k = j;
} use[k] = true; for(int j=1; j<=N; j++)
{
if(!use[j] && k!=j && G[k][j] < dist[j].len)
dist[j].len = G[k][j], dist[j].v = k;
}
}
} int main()
{
int i, j, N, M, u, v; scanf("%d", &N); for(i=1; i<=N; i++)
scanf("%d%d", &p[i].x, &p[i].y); for(i=1; i<=N; i++)
for(j=1; j<i; j++)
{
G[j][i] = G[i][j] = Len(p[i], p[j]);
} scanf("%d", &M); while(M--)
{
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = -1;
} prim(N); for(int i=2; i<=N; i++)
{
if(dist[i].len > 0)
printf("%d %d\n", i, dist[i].v);
} return 0;
}
H - Highways - poj 1751(prim)的更多相关文章
- POJ 2253-Frogger (Prim)
题目链接:Frogger 题意:两仅仅青蛙,A和B,A想到B哪里去,可是A得弹跳有限制,所以不能直接到B,可是有其它的石头作为过渡点,能够通过他们到达B,问A到B的全部路径中.它弹跳最大的跨度的最小值 ...
- c/c++ 用普利姆(prim)算法构造最小生成树
c/c++ 用普利姆(prim)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路.这时 ...
- HDU.1233 还是畅通工程(Prim)
HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...
- linux内核中链表代码分析---list.h头文件分析(一)【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637596.html linux内核中链表代码分析---list.h头文件分析(一) 16年2月27日17 ...
- linux内核中链表代码分析---list.h头文件分析(二)【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637598.html linux内核中链表代码分析---list.h头文件分析(二) 16年2月28日16 ...
- ZOJ - 1586 QS Network (Prim)
ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...
- 普利姆算法(prim)
普利姆算法(prim)求最小生成树(MST)过程详解 (原网址) 1 2 3 4 5 6 7 分步阅读 生活中最小生成树的应用十分广泛,比如:要连通n个城市需要n-1条边线路,那么怎么样建设才能使工程 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
随机推荐
- Word 查找替换,通配符一览表
Word查找替换详细用法及通配符一览表 使用通配符要查找“?”或者“*”,可输入“\?”和“\*”,\1\2\3依次匹配数对括号内容查找(a)12(b) 替换\2XY\1 结果:bXYa ([ ...
- 应用app首次进入导航页动画
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActi ...
- Android VideoView
这两天公司要让做一个播放视频的小Demo,于是网上学习了下VideoView的使用方法. 先看布局文件,很简单 就是一个VideoView和两个ImageView <RelativeLayout ...
- 解决SQL Server的TEXT、IMAGE类型字段的长度限制
更多资讯.IT小技巧.疑难杂症等等可以关注 艾康享源 微信公众号. 来自为知笔记(Wiz)
- Swift - 26 - 函数的基础写法
//: Playground - noun: a place where people can play import UIKit // 无参无返回 // -> Void可以省略不写, 或者写成 ...
- VBoxManage命令详解
转自:http://zhang-ly520.iteye.com/blog/300606 由于最近工作对vbox有一定涉猎,发现这个写的比较好,先转来,稍有空时再根据自己的心得整理一下. VBoxMan ...
- 自动Telnet远程登陆命令
有些时候,在面对开发机的时候,不断的telnet和不断的command自己的命令确实非常麻烦,需要一些自动测试或者自动部署的需求.然而面对telnet很多同学都跟我一样一开始觉得无法通过管道等传用户名 ...
- locate 不能使用
当需要查找一个文件,只知道文件名不知道路径,我们通常用locate,由于公司的服务器使用最小化安装的所以当locate 文件名,报错,提 示-bash: locate: command not fou ...
- python设计模式之装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- border做箭头的例子
<style> .test{ margin:0 auto; width:100px; height:100px; background:lightskyblue; position:rel ...