poj 1258 Agri-Net 最小生成树 kruskal
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 33733 | Accepted: 13539 |
Description
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
给n个点,求最小生成树。这次用的kruskal + 并查集 实现的
#include<stdio.h>
#include<algorithm>
using namespace std;
struct Edge
{
int s, t, pow;
};
Edge edge[25010];
int total;
int n;//输入点的个数
int father[500];
//并查集模板
int getfather(int p)
{
if(father[p] == p)
return p;
else
return father[p] = getfather(father[p]);
}
void merge_(int a, int b)
{
a = getfather(a);
b = getfather(b);
if(a == b)
return ;
else
father[a] = b;
}
void init_set()
{
int i;
for(i = 0; i <= n; i++)
father[i] = i;
}
//kruskal
bool cmp(Edge a, Edge b)
{
return a.pow < b.pow;//´ÓСµ½´óÅÅÐò
}
int kruskal()
{
sort(edge, edge + total, cmp);
bool used[500] = {0};
int i;
int count = 0;
int ans = 0;
init_set();
for(i = 1; i < total; i++)
{
if(getfather(edge[i].s) != getfather(edge[i].t))
{
merge_(edge[i].s, edge[i].t);
ans += edge[i].pow;
count ++;
if(count == n - 1)
break;
}
}
return ans;
}
void addedge(int s, int t, int pow)
{
edge[total].s = s;
edge[total].t = t;
edge[total].pow = pow;
total ++;
}
int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d", &n) != EOF)
{
int i;
total = 0;
for(i = 1; i <= n; i++)
{
int j;
for(j = 1; j <= n; j++)
{
int num;
scanf("%d", &num);
if(i == j)
continue;
addedge(i, j, num);
}
}
printf("%d\n", kruskal());
}
return 0;
}
poj 1258 Agri-Net 最小生成树 kruskal的更多相关文章
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258 Agri-Net (最小生成树)
Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...
- poj 3522 Slim Span (最小生成树kruskal)
http://poj.org/problem?id=3522 Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
- POJ - 1287 Networking 【最小生成树Kruskal】
Networking Description You are assigned to design network connections between certain points in a wi ...
- POJ 1258 Agri-Net(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...
- poj 1258 Agri-Net【最小生成树(prime算法)】
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44827 Accepted: 18351 Descri ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
随机推荐
- python数据库操作pymysql
安装数据库: pip3 install pymysql 进行数据库的更新.插入.查询等操作: #!/usr/bin/python3.4 # -*- coding: utf-8 -*- #------- ...
- WEB/HTTP 调试利器 Fiddler 的一些技巧分享
1.原理简介: Fiddler 是目前最强大最好用的 Web 调试工具之一,它能记录所有客户端和服务器的http和https请求, 允许你监视,设置 CGI 请求的断点,甚至修改输入输出数据.同类的工 ...
- Flex相关案例及资源搜集
Flex一些例子: http://blog.minidx.com/ 上千个Flex例子,对于学习者来说是一个庞大的资源宝库. http://fleksray.org/Flex_skin.html ht ...
- 访问本机的WEB API 报400错误
当时用的IP是127.0.0.1 报400错误,换成 localhost 后正常.
- LintCode "Find Peak Element II"
Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...
- 【ntp】centos7下ntp服务器设置
安装ntp #检查服务是否安装 rpm -q ntp #安装ntp服务器 yum -y install ntp 修改配置文件:/etc/ntp.conf 内容如下: restrict default ...
- MyBatis绑定错误[Invalid bound statement (not found)]
如果出现: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因是Mapper i ...
- Python 创建和发布安装函数模块
1. create dir "nester" under C:\Users\eric\AppData\Local\Programs\Python\Python35-32\ 2. c ...
- cent os下面的基本配置操作
二,修改Linux分辨率命令行 在root用户模式下,输入$ vi /boot/grub/grub.conf(路径可能会不一样,也可以是 /etc/grub.conf),打开grub.conf文件 我 ...
- linux下mongodb定时备份指定的集合
目标:把一台linux机上mongodb的数据定时备份到另一台机上: 过程: 一开始打算使用mongoexport和mongoimport,但是总是会报“\x00”字符串不能识别的问题,后来就改成了m ...