Networking---poj1287最小生成树
http://poj.org/problem?id=1287
最小生成树模板题类似的还有:poj1258 hdu1233代码几乎一样;
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#define N 110
#define INF 0xfffffff
using namespace std; int ans, n, m, dist[N], maps[N][N], vis[N];//dist[i]代表当前起点到i的最小距离; void Init()
{
for(int i=; i<=n; i++)
{
dist[i] = INF;
vis[i] = ;
for(int j=; j<=n; j++)
if(i==j)
maps[i][j] = ;
else
maps[i][j] = INF;
}
} void Prim(int start)
{
for(int i=; i<=n; i++)
dist[i] = maps[start][i];
vis[start] = ;
for(int i=; i<=n; i++)
{
int Min = INF, index=-; for(int j=; j<=n; j++)
{
if(vis[j]== && Min>dist[j])
{
Min = dist[j];
index = j;
}
}
if(index == -)break;
vis[index] = ;
ans += Min;
for(int j=; j<=n; j++)
{
if(vis[j]== && maps[index][j] < dist[j])
{
dist[j] = maps[index][j];
}
}
}
} int main()
{
int a, b, c;
while(scanf("%d", &n),n)
{
ans=;
Init();
scanf("%d", &m);
for(int i=; i<m; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b] = maps[b][a] = min(maps[a][b], c);
}
Prim();
printf("%d\n", ans);
}
return ;
}
prim
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#define N 110
#define INF 0xfffffff
using namespace std; int n, f[N], m; struct node
{
int x, y, d;
}a[N*N]; int cmp(node p, node q)
{
return p.d < q.d;
} int Find(int x)
{
if(x != f[x])
f[x] = Find(f[x]);
return f[x];
} int main()
{
int ans, px, py;
while(scanf("%d", &n),n)
{
ans=;
for(int i=; i<=n; i++)
f[i] = i;
scanf("%d", &m);
for(int i=; i<m; i++)
{
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].d);
}
sort(a, a+m, cmp);
for(int i=; i<m; i++)
{
px = Find(a[i].x);
py = Find(a[i].y);
if(px != py)
{
f[px] = py;
ans+=a[i].d;
}
}
printf("%d\n", ans);
}
return ;
}
Kruskal
Networking---poj1287最小生成树的更多相关文章
- POJ1287 Networking【最小生成树】
题意: 给出n个节点,再有m条边,这m条边代表从a节点到b节点电缆的长度,现在要你将所有节点都连起来,并且使长度最小 思路: 这是个标准的最小生成树的问题,用prim的时候需要注意的是他有重边,取边最 ...
- POJ 1287 Networking (最小生成树)
Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...
- POJ 1287 Networking(最小生成树)
题意 给你n个点 m条边 求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...
- poj 1287 Networking【最小生成树prime】
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7321 Accepted: 3977 Descri ...
- POJ 1287:Networking(最小生成树Kruskal)
id=1287">Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5976 Acce ...
- POJ - 1287 Networking 【最小生成树Kruskal】
Networking Description You are assigned to design network connections between certain points in a wi ...
- POJ1287(最小生成树入门题)
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7753 Accepted: 4247 Descri ...
- POJ 1287 Networking(最小生成树裸题有重边)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- POJ - 1287 Networking (最小生成树&并查集
You are assigned to design network connections between certain points in a wide area. You are given ...
- 最小生成树练习1(克鲁斯卡尔算法Kruskal)
今天刷一下水题练手入门,明天继续. poj1861 Network(最小生成树)新手入门题. 题意:输出连接方案中最长的单根网线长度(必须使这个值是所有方案中最小的),然后输出方案. 题解:本题没有直 ...
随机推荐
- js for循环与for in循环的区别
for循环可一遍历数组,而for in循环可以遍历数组和对象 使用for in循环会将Array当成对象遍历,而Array的存取速度明显比Object要快.所以使用for循环遍历数组比for in循环 ...
- HashTable、HashMap、ConcurrentHashMap、Collections.synchronizedMap()区别
Collections.synchronizedMap()和Hashtable一样,实现上在调用map所有方法时,都对整个map进行同步,而ConcurrentHashMap的实现却更加精细,它对Ha ...
- rabbitmq queue_declare arguments参数注释
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_26656329/article/details/77891793说明官方文档 在创建queue ...
- 【laravel5.6】 laravel 接口 接管 自定义异常类
1 app\exceptions 目录下 新建 Apiexception.php <?php namespace App\Exceptions; /*** * API 自定义异常类 */ us ...
- android:listView Button 焦点问题
要想listView的item与其上的button皆能得到焦点响应: 在listView item 的布局中: 在<RelativeLayout>中 android:descendantF ...
- c++ 类内static成员初始化
类内部的static成员,除了为const static 且为整数类型(int char bool)可在类内部初始化. 其他的都建议在对应的cpp文件中进行初始化. test.h #ifndef TE ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- python之Tkinter控件学习
转载自 http://www.cnblogs.com/kaituorensheng/p/3287652.html#_label0 阅读目录 1. 产品介绍 2. 设计规划 3. 相关知识 4. 源码 ...
- [转]centos6 与 7 其中的一些区别
# vi /etc/ssh/sshd_config #将MaxAuthTries注释去掉 MaxAuthTries 5(登录次数) UseDNS no 默认是yes 的,把这个改为no,可以大大减 ...
- web开发之Cookie使用
做过web开发的小伙伴对于Cookie一定不陌生,当用户登录后将用户的账号保存到本地,密码保存时,建议使用MD5进行加密,以防止用户个人信息的泄露.今天和大家简单聊聊关于Jquer Cookie的使用 ...