Agri-Net —poj1258
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 44670 | Accepted: 18268 |
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
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
求把所有town连接起来所需最少边权值和,把边权值按从小到大排序,如果当前边权值连接的两边没有关系就把当前边权值相加,如果有关系当前值就没有价值,不用浪费资源联通这两个town。到最后肯定会把所有town连接~
#include<iostream>
#include<algorithm> using namespace std; #define N 1000005 int k, n, a, f[N]; struct node
{
int x, y, w;
}P[N]; int cmp(const void *u, const void *b)
{
node c, d;
c = *(node *)u;
d = *(node *)b; return c.w-d.w;
} void init()
{
for(int i = ; i < N; i++)
f[i] = i;
}
int found(int a)
{
if(f[a] != a)
f[a] = found(f[a]);
return f[a];
} int main()
{
while(cin >> n)
{
k = ; init(); for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
cin >> a;
P[k].w = a;
P[k].x = i, P[k].y = j;
k++;
} qsort(P, k, sizeof(P[]), cmp); int ans = ; for(int i = ; i < k; i++)
{
int nx = found(P[i].x), ny = found(P[i].y); if(nx != ny)
{
f[nx] = ny;
ans += P[i].w;
}
}
//int ans = kuscral(1, n); cout << ans << endl;
}
return ;
}
Agri-Net —poj1258的更多相关文章
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- poj1258 Agri-Net (prim+heap)
题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...
- POJ-1258 Agri-Net---MST裸题Prim
题目链接: https://vjudge.net/problem/POJ-1258 题目大意: 求MST 思路: 由于给的是邻接矩阵,直接prim算法 #include<iostream> ...
- Poj1258 Agri-Net (最小生成树 Prim算法 模板题)
题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- poj1258 Agri-Net(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1258 题意 有n个农场,现在要在n个农场之间铺设光纤使得n个农场连接起来,求铺设光纤的最短距离. 思路 最小生成树问题,使用Prime ...
- 最小生成树 prime poj1258
题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...
- poj1258 Agri-Net 最小生成树
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44032 Accepted: 18001 Descri ...
- Prim算法POJ1258
http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, #include<stdio.h> #include<iostream> #inc ...
- POJ1258 基础最小生成树
本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...
随机推荐
- 《图解设计模式》读书笔记4-2 STRATEGY模式
目录 示例程序 角色 想法 Strategy模式即策略模式,在编程中,策略指的就是算法.利用此模式可以整体替换算法,即使用不同方式解决同一个问题.比如设计一个围棋程序,通过切换算法可以方便地切换AI的 ...
- Centos7.2命令安装图形化界面
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/liang_operations/arti ...
- CentOS 7命令行安装GNOME、KDE图形界面(成功安装验证)
来源:cnblogs.com/Amedeo 作者:Amedeo 正文 CentOS 7 默认是没有图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方 ...
- CentOS修改网络设置,不容易啊,终于可以在virtualbox里上网了
CentOS 修改网关 修改对应网卡的网关的配置文件[root@centos]# vi /etc/sysconfig/network修改以下内容NETWORKING=yes(表示系统是否使用网络,一般 ...
- MySQL点滴记录
1.查询所用引擎 show engines;
- [Linux] 018 关机重启命令
1. shutdown 命令 $ shutdown [选项] 时间 选项 -c 取消前一个关机wgwy -h 关机 -r 重启 2. 其他关机命令 $ halt $ poweroff $ init 0 ...
- Windows Server2003 关闭 关机信息、开机ctrl+alt+del
取消CTRL+ALT+DEL win+R 或从"开始"打开"运行",输入gpedit.msc打开"组策略编辑器",依次展开"计算机 ...
- MySQL-第N篇杂记
1.数据的导入导出 2.查询结果的重定向 3.ON DUPLICATE KEY UPDATE对于指定的主键或者唯一键,insert时发生冲突则进行update操作. 4.解决MySQL中问乱码问题,分 ...
- 工厂模式vs简单工厂
前言 工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断 ...
- node进程一些信号的意义
1.SIGINT这个信号是系统默认信号,代表信号中断,就是ctrl+c: 2.SIGQUIT 3.SIGTERM 4.exit