博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1258 Agri-Net
阅读量:4155 次
发布时间:2019-05-26

本文共 1226 字,大约阅读时间需要 4 分钟。

poj1258 Agri-Net

标签:最小生成树


/*    题意:给出n*n的无向图的邻接矩阵,输出最小生成树的权值和。    思路:Kruskal。    优化:无向图矩阵是对称的,只保存上三角。到最后k的值就是边数,由循环n*n-->k次。*/#include 
#include
using namespace std;#define maxn 105typedef struct{ int x, y; int w;}edge;edge e[maxn * maxn / 2];int rank_[maxn], father[maxn], sum;int cmp(edge a, edge b){ //按权值非降序排序 return a.w < b.w;}void make_set(int x){ father[x] = x; rank_[x] = 0;}int find_set(int x){ return x != father[x] ? find_set(father[x]) : father[x];}void union_set(int x, int y, int w){ if(x == y) return ; if(rank_[x] > rank_[y]) father[y] = x; else{ if(rank_[x] == rank_[y]) rank_[y]++; father[x] = y; } sum += w; //}int main(){ int n, k, i, j, no; while(scanf("%d", &n) != EOF){ k = 0, sum = 0; for(i = 0; i < n; i++){ make_set(i); for(j = 0; j < n; j++) if(j < i){ //只读取下三角 e[k].x = i, e[k].y = j; scanf("%d", &e[k++].w); } else scanf("%d", &no); } sort(e, e + k, cmp); for(i = 0; i < k; i++) union_set(find_set(e[i].x), find_set(e[i].y), e[i].w); printf("%d\n", sum); } return 0;}

转载地址:http://kskxi.baihongyu.com/

你可能感兴趣的文章
visualgdb 添加预编译宏
查看>>
做嵌入式开发你不得不知的16个要点
查看>>
#if 用法
查看>>
(嵌入式)关于arm中的存储控制器
查看>>
(转+整理)Nandflash存储
查看>>
ARM指针寄存器 -程序计数器PC、堆栈指针SP
查看>>
tcpdump抓包
查看>>
SIP的re-invite和update的区别
查看>>
RTP协议--RR,SR,实例程序 并附有RTCP控制协议解释
查看>>
sdp recv/send/sendrecv
查看>>
SIP ACK Req_URI
查看>>
H264关于RTP协议的实现
查看>>
SIP中From ,Contact, Via 和 Record-Route/Route
查看>>
关于VS Code 中文显示乱码
查看>>
mongoose上传文件
查看>>
HTTP协议详解
查看>>
Visual Studio 编译jrtplib
查看>>
wireshark抓取rtp包
查看>>
VS2015编译eXosip2-5.0.0
查看>>
pthread_cond_wait()用法分析
查看>>