第十次训练 I题

问题链接:Problem I
问题简述:

有n个点,给出点与点的距离,让你构造出一幅图使得所有点都可连接起来,并且总长度最小,求出最小总长度。

问题分析:

构造出边的总权值最小的图,最小生成树。不多说了,水题。

AC通过的C语言程序如下:

#include<iostream>
#include<cstdio>
#include<cstdlib> 
#include<algorithm>
#include<queue>
#include<set>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
int root[105],n,m,ans,cnt;
struct node{
	int a,b,len;
}route[100005];
void init(){
	for(int i=1;i<=n;i++){
		root[i]=i;
	}
}
int find(int x){
	if(root[x]==x){
		return x;
	}
	else{
		return root[x]=find(root[x]);
	}
}
void unions(int x,int y,int i){
	int a=find(x);
	int b=find(y);
	if(a==b){
		return;
	}
	else{
		root[a]=root[b];
		cnt++;
		ans+=route[i].len;
	}
}
bool cmp(node a,node b){
	return a.len<b.len;
}
int main(){
	std::ios::sync_with_stdio(false);
	while(cin>>n&&n){
		cin>>m;
		cnt=0;
		ans=0;
		init();
		int a,b;
		for(int i=0;i<m;i++){
			cin>>route[i].a>>route[i].b>>route[i].len;
		}
		sort(route,route+m,cmp);
		for(int i=0;i<m;i++){
			unions(route[i].a,route[i].b,i);
			if(cnt==n-1){
				break;
			}
		}
		cout<<ans<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写一个 Java 程序,在控制台输出“Hello World!” ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } ``` 2. 编写一个 Java 程序,求一个整数数组中的最大值和最小值 ```java public class MaxMin { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int max = arr[0]; int min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } System.out.println("最大值:" + max); System.out.println("最小值:" + min); } } ``` 3. 编写一个 Java 程序,计算从 1 到 100 的所有整数的和 ```java public class Sum { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } System.out.println("总和:" + sum); } } ``` 4. 编写一个 Java 程序,判断一个整数是否为质数 ```java public class PrimeNumber { public static void main(String[] args) { int num = 7; boolean isPrime = true; for (int i = 2; i < num; i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(num + "是质数"); } else { System.out.println(num + "不是质数"); } } } ``` 5. 编写一个 Java 程序,判断一个字符串是否为回文字符串 ```java public class Palindrome { public static void main(String[] args) { String str = "level"; boolean isPalindrome = true; for (int i = 0; i < str.length() / 2; i++) { if (str.charAt(i) != str.charAt(str.length() - 1 - i)) { isPalindrome = false; break; } } if (isPalindrome) { System.out.println(str + "是回文字符串"); } else { System.out.println(str + "不是回文字符串"); } } } ``` 6. 编写一个 Java 程序,将一个字符串中的所有空格替换为“%20” ```java public class ReplaceSpace { public static void main(String[] args) { String str = "Hello World"; String newStr = str.replaceAll(" ", "%20"); System.out.println(newStr); } } ``` 7. 编写一个 Java 程序,将一个字符串反转 ```java public class ReverseString { public static void main(String[] args) { String str = "abcdefg"; StringBuilder sb = new StringBuilder(str); sb.reverse(); System.out.println(sb.toString()); } } ``` 8. 编写一个 Java 程序,将一个数组中的元素顺序颠倒 ```java public class ReverseArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] newArr = new int[arr.length]; for (int i = 0; i < arr.length; i++) { newArr[arr.length - 1 - i] = arr[i]; } System.out.println(Arrays.toString(newArr)); } } ``` 9. 编写一个 Java 程序,输出九九乘法表 ```java public class MultiplicationTable { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "×" + i + "=" + j * i + "\t"); } System.out.println(); } } } ``` 10. 编写一个 Java 程序,求斐波那契数列中的第 n 项 ```java public class Fibonacci { public static void main(String[] args) { int n = 7; int[] arr = new int[n]; arr[0] = 1; arr[1] = 1; for (int i = 2; i < arr.length; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } System.out.println("第" + n + "项:" + arr[n - 1]); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值