POJ 1258 Agri-Net
题意:给一个无向图的邻接矩阵,求最小生成树。
解法:Kruskal算法。把边按边权排序,从小到大插入生成树中,如果一个边的两个点都在生成树中则不插入,用并查集维护。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
int u, v;
int num;
bool operator < (const node & tmp) const
{
return num < tmp.num;
}
}edge[10005];
int father[105];
void init()
{
for(int i = 0; i < 105; i++)
father[i] = i;
}
int Find(int x)
{
return father[x] == x ? father[x] : father[x] = Find(father[x]);
}
bool Union(int a, int b)
{
int c = Find(a), d = Find(b);
if(c == d)
return false;
father[c] = d;
return true;
}
int main()
{
int n;
while(~scanf("%d", &n))
{
init();
int cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
int x;
scanf("%d", &x);
if(j > i)
{
edge[cnt].u = i;
edge[cnt].v = j;
edge[cnt++].num = x;
}
}
sort(edge, edge + cnt);
LL ans = 0;
for(int i = 0; i < cnt; i++)
{
if(Union(edge[i].u, edge[i].v))
ans += edge[i].num;
}
printf("%lld\n", ans);
}
return 0;
}
POJ 1258 Agri-Net的更多相关文章
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- 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 ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ 1258
http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
- POJ 1258 Agri-Net(Prim算法求解MST)
题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- (最小生成树)Agri-Net -- POJ -- 1258
链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- Prim算法求权数和,POJ(1258)
题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...
- poj 1258 Agri-Net 解题报告
题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...
- POJ 1258 Agri-Net(Prim)
题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
随机推荐
- 【linQ】DataContext 入门 , 和 hql , jpql 一样好用
DataContext 和 LINQ结合后会有巨大的能量 public class UserDataContext : DataContext { public Table<User> U ...
- EXTJS 4.2 资料 控件之textfield文本框加事件的用法
{ xtype: "textfield", width: 100, id: "txtGroupName", name: "txtGroupName&q ...
- touches获得手指点击的坐标
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObjec ...
- dive into python 读笔(2)
chapter 4 自省, summary: # 用可选和命名参数定义和调用函数 # 用 str 强制转换任意值为字符串形式 # 用 getattr 动态得到函数和其它属性的引用 # 扩展列表解析语法 ...
- 【BZOJ 1084】[SCOI2005]最大子矩阵
Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤2 ...
- EF当实体模型与数据库的架构不同时要删除数据库时的报错问题
当使用的EF的时候,我们都知道EF当实体模型与数据库的架构不同时要删除数据库,这是会把错: 无法创建与 'master' 数据库之间的连接,这是因为已打开原始数据库连接,并且已从连接字符串中删除凭据. ...
- SC命令详解
我们知道在MStools SDK,也就是在Resource Kit有一个很少有人知道的命令行软件,SC.exe,这个软件向所有的Windows NT和Windows 2000要求控制他们的API函数. ...
- 理解UIEdgeInsets
供参考 iOS 的控件,只看到 UIButton 可以设置 Padding/Insets,即按钮上文字或图片与按钮边界的间隙. CSS 上叫做 Padding,在 iOS 中叫做 Insets,UIB ...
- 各系统下设置输入法按键为ctrl+shift+space
xp=====(一直找不到..原来右边是可以下拉的).. linux ibus----- 设置---直接按下Ctrl+Shift+Space
- 5.0:Spring-bean的加载
内容来自<Spring深度解析>,之后的不一一复述! 在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎 ...