zoukankan      html  css  js  c++  java
  • 1349:【例4-10】最优布线问题

    【题目描述】
    学校有n台计算机,为了方便数据传输,现要将它们用数据线连接起来。两台计算机被连接是指它们有数据线连接。由于计算机所处的位置不同,因此不同的两台计算机的连接费用往往是不同的。
    
    当然,如果将任意两台计算机都用数据线连接,费用将是相当庞大的。为了节省费用,我们采用数据的间接传输手段,即一台计算机可以间接的通过若干台计算机(作为中转)来实现与另一台计算机的连接。
    
    现在由你负责连接这些计算机,任务是使任意两台计算机都连通(不管是直接的或间接的)。
    
    【输入】
    第一行为整数n(2≤n≤100),表示计算机的数目。此后的n行,每行n个整数。第x+1行y列的整数表示直接连接第x台计算机和第y台计算机的费用。
    
    【输出】
    一个整数,表示最小的连接费用。
    
    【输入样例】
    3
    0 1 2
    1 0 1
    2 1 0
    【输出样例】
    2
    【提示】
    注:表示连接1和2,2和3,费用为2。
    
    # include <bits/stdc++.h>
    using namespace std ;
    const int N = 100 + 5 ;
    int n ;
    int fa[N] ;
    const int M = N * N ;
    struct node {
    	int u , v , w ;
    }edge[M] ;
    
    inline int find(int x) {
    	return x == fa[x] ? x : fa[x] = find(fa[x]) ;
    }
    inline void merge(int x , int y) {
    	fa[find(x)] = find(y) ;
    }
    inline bool cmp(node x , node y) {
    	return x.w < y.w ;
    }
    signed main() {
    	ios::sync_with_stdio(false) ;
    	cin >> n ;
    	int cnt = 0 ;
    	for(register int i=1;i<=n;i++) fa[i] = i ;
    	for(register int i=1;i<=n;i++) 
    		for(register int j=1;j<=n;j++) {
    			int w ;
    			cin >> w ;
    			edge[++ cnt].u = i , edge[cnt].v = j , edge[cnt].w = w ;
    		}
    	sort(edge + 1 , edge + cnt + 1 , cmp) ;
    	int ans = 0 ;
    	for(register int i=1;i<=cnt;i++) {
    		int fx = find(edge[i].u) , fy = find(edge[i].v) ;
    		if(fx == fy) continue ;
    		merge(edge[i].u , edge[i].v) ;
    		ans += edge[i].w ;
    	}
    	printf("%d 
    " , ans) ;
    	return 0 ;
    }
    
  • 相关阅读:
    k8s管理pod资源对象(上)
    k8spod资源的基础管理操作
    k8s名称空间资源
    bootstrap表格 之多选数据的获取
    sql server 查询表的创建时间
    C# Bootstrap table之 分页
    C# 基于Bootstrap的三级联动
    C# 后台构造json数据
    C# bootstrap之表格动态绑定值
    C# 文件的一些基本操作
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10875043.html
Copyright © 2011-2022 走看看