分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

luogu P1546 最短网络 Agri-Net

发布时间:2023-09-06 02:32责任编辑:郭大石关键词:暂无标签

emmm

题面不想放了

传送门www

一道emmm

很简单的最小生成树问题

没啥思维含量

不知道因为什么zz问题写了4遍www

#include<cstdio>#include<algorithm>using namespace std;#define maxn 20010int fa[maxn],n;int cnt,ans;struct EDGE { ???int to,from,weight;} edge[maxn];void add(int x,int y,int w) { ???edge[++cnt].to = y; ???edge[cnt].from = x; ???edge[cnt].weight = w;}int get(int x) { ???if(fa[x] == x) ???????return x; ???return fa[x] = get(fa[x]);}int cmp(EDGE x,EDGE y) { ???return x.weight < y.weight;}int kruskal() { ???for(int i = 1; i <= n; i++) ???????fa[i] = i; ???sort(edge + 1,edge + cnt + 1,cmp); ???for(int i = 1; i <= cnt; i++) { ???????int l = get(edge[i].from); ???????int r = get(edge[i].to); ???????if(l != r) { ???????????fa[l] = r; ???????????ans += edge[i].weight; ???????} ???} ???return ans;}int main() { ???scanf("%d",&n); ???for(int i = 1; i <= n; i++) ???????for(int j = 1; j <= n; j++) { ???????????int w; ???????????scanf("%d",&w); ???????????if(j > i) ???????????add(i,j,w); ???????} ???printf("%d",kruskal()); ???return 0;}

总之,做最小生成树的时候还是要注意细节啊

luogu P1546 最短网络 Agri-Net

原文地址:https://www.cnblogs.com/sevenyuanluo/p/10353638.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved