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 ...
随机推荐
- 请输出in.txt文件中的2 4 6 8 9 10 12行
in.txt文件: 学号 姓名 性别 年龄 1001 张三 男 18 1002 赵四 男 19 1003 李丽 女 18 1004 刘芳 女 32 1005 王五 男 54 1006 小明 男 32 ...
- Android签名详解(debug和release)
Android签名详解(debug和release) 1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...
- 设计模式UML图
1.简单工厂模式 2.工厂模式 工厂模式与简单工厂模式的不同在于,每个操作类都有自己的工厂,而且把逻辑判断交给了客户端,而简单工厂的逻辑判断在工厂类里边,当增加新的操作类时,简单工厂需要修改工厂类,而 ...
- C#—集合(Collection)
1.栈(stack<T>) using System; using System.Collections.Generic; using System.Linq; using System. ...
- HDU5301
题意:给n*m的矩形区域,剔除其中1*1的方块,然后用不同矩形块填充整个矩形区域,求需要的矩形块最大面积的最小值. 思路:先判把矩形矫正,然后特殊处理边值为奇数,且在中心点的情况,最后处理障碍在其他位 ...
- Session深入理解
Session是在什么情况下产生的 客户端访问服务器端,服务器端为每个用户生成一个唯一的sessionId,是这样吗?sessionId的作用是什么? http://www.cnblogs.com/s ...
- (转)PHP自定义遍历目录下所有文件dir(),readdir()函数
方法一:使用dir()遍历目录 dir()函数,成功时返回Directory类实例 PHP dir() 语法格式为: dir(directory);//directory为需要显示文件名的目录名称,可 ...
- android - 调试
在android总调试程序,不同于普通的java程序,我们可以通过'Windows->Show View->Other->LogCat"打开'LogCat'工具,选择下拉选 ...
- iOS开发: 向右滑动手势功能实现
在navigationController中实现向右滑动 返回功能 系统提供的backbarbuttonitem,不用添加任何代码即可实现向右滑动后退功能,但是往往要对按钮修改样式等时,就需要自定义l ...
- PHP Countable接口
实现该接口可以使用count()方法来获取集合的总数