[kuangbin带你飞]专题六 最小生成树 D
来源:互联网 发布:淘宝客刷销量有什么用 编辑:程序博客网 时间:2024/06/01 10:22
题意:
给每两个点之间的距离,然后给几个点已经相连,问最小生成树。。
tip:
显然用kru之间把已知放在一个集里面很简单。
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>const int maxm = 10010;const int maxn = 110;using namespace std;int tot,root[maxn],n,m;struct node{ int u,v,w; bool operator < (const node &b)const{ return w < b.w; }}edges[maxm];void add(int u,int v,int w){ edges[tot].u=u;edges[tot].v= v;edges[tot++].w=w;}int findroot(int x){ return x==root[x]?x:root[x]=findroot(root[x]);}void kru(){ int ans = 0; for(int i = 0 ; i < tot ;i++){ int x = findroot(edges[i].v),y = findroot(edges[i].u); if(x == y) continue; else{ ans+=edges[i].w; root[x] = root[y]; } } printf("%d\n",ans);}void init(){ tot = 0; for(int i = 1; i <= n ; i++) root[i] = i; for(int i = 1 ; i <= n ; i++) for(int j = 1; j <= n ; j++){ int k; scanf("%d",&k); if(i < j) add(i,j,k); } sort(edges,edges+tot); scanf("%d",&m); for(int i = 0 ; i < m ;i++){ int a,b; scanf("%d%d",&a,&b); int x = findroot(a),y = findroot(b); if(x == y) continue; root[x] = root[y]; }}int main(){ while(~scanf("%d",&n)){ init(); kru(); }}
0 0
- [kuangbin带你飞]专题六 最小生成树 D
- 【 题集 】 【kuangbin带你飞】专题六 最小生成树
- [kuangbin带你飞]专题六 最小生成树
- [kuangbin带你飞]专题六 最小生成树 H
- [kuangbin带你飞]专题六 最小生成树A
- [kuangbin带你飞]专题六 最小生成树 C
- [kuangbin带你飞]专题六 最小生成树 D POJ 2421
- HDU 1233 还是畅通工程 (kuangbin带你飞 专题六 最小生成树)
- hdu 1875 畅通工程再续(kuangbin带你飞 专题六:最小生成树)
- POJ 1251 HDU 1301 Jungle Roads(kuangbin带你飞 专题六:最小生成树)
- POJ 1287 Networking (kuangbin带你飞 专题六:最小生成树)
- POJ 2421 HDOJ 1102 Constructing Roads(kuangbin带你飞 专题六:最小生成树)
- POJ 1679 The Unique MST(kuangbin带你飞 专题六:最小生成树)
- POJ 1258 Agri-Net(kuangbin带你飞 专题六:最小生成树)
- POJ 2349&&ZOJ 1914 Arctic Network(kuangbin带你飞 专题六:最小生成树)
- ZOJ 1586&&FZU 1096 QS Network(kuangbin带你飞 专题六 最小生成树)
- POJ 2031 Building a Space Station(kuangbin带你飞 专题六:最小生成树)
- POJ 1789 Truck History(kuangbin带你飞 专题六:最小生成树)
- OSG-VS2015编译第三方库
- Linux网卡能接收不能发送bug eth0 tx packets:0
- 微信公众平台后台数据如何分析
- jenkins 的 Build periodically 参数配置
- linux配置java环境变量(详细)
- [kuangbin带你飞]专题六 最小生成树 D
- 前端涉及的所有知识体系
- 使用Unix Shells进行大括号扩展
- 对TexStudio配置拼写和语法检查LanguageTool功能
- RtmpServer系列
- 接口测试之基础篇2
- spring + mybatis配置多个数据源
- android中的 Otto使用
- ftp身份认证时登录框反复弹出以及ftp常用配置