Jungle Roads(HDU 1301)---最小生成树模板题(Prim算法和Kruskal算法)

题目链接

题目描述

在这里插入图片描述

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

输入样例

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

输出样例

216
30

分析

题目大意是给定一个无向图,求图的最小生成树。
注意在用scanf函数读入数据时,需要注意读入换行符。以下是用堆优化的Prim算法和Kruskal算法源代码:

源程序

Prim算法

#include <bits/stdc++.h>
#define MAXN 30
using namespace std;
struct Edge{
	int v,w;
	bool operator <(const Edge a)const{
		return w>a.w;
	}
}; 
int n,m,dis[MAXN],g[MAXN][MAXN];
bool vis[MAXN];
int prim()
{
	priority_queue<Edge> q;
	memset(dis,0x3f,sizeof(dis));
	memset(vis,false,sizeof(vis));
	dis[1]=0;
	q.push(Edge{1,0});
	while(!q.empty()){
		int u=q.top().v;q.pop();
		if(vis[u])continue;	//已加入最小生成树 
		vis[u]=true;
		for(int i=1;i<=n;i++){
			if(!vis[i]&&dis[i]>g[u][i]){	//还未加入生成树且有更小值 
				dis[i]=g[u][i];
				q.push(Edge{i,dis[i]}); 
			}
		}
	}
	int ans=0;	//最小生成树权值
	for(int i=1;i<=n;i++)
		ans+=dis[i];
	return ans;
}
int main()
{
	while(scanf("%d",&n)&&n){
		memset(g,0x3f,sizeof(g));
		getchar();	//读入换行符 
		for(int i=1;i<=n-1;i++){
			char c;
			int u,v,w;
			scanf("%c %d",&c,&m);
			u=c-'A'+1;	//将编号A-Z转为1-26
			for(int j=1;j<=m;j++){
				scanf(" %c %d",&c,&w);
				v=c-'A'+1;
				g[u][v]=g[v][u]=w;
			} 
			getchar();	//读入换行符 
		}
		printf("%d\n",prim());
	}
}

Kruskal算法

#include <bits/stdc++.h>
#define MAXN 30
using namespace std;
struct Edge{
	int u,v,w;
	bool operator <(const Edge a)const{
		return w>a.w;
	}
};
int n,m,father[MAXN];
priority_queue<Edge> q;
int find(int x)	//并查集 
{
	if(x==father[x])return x;
	return father[x]=find(father[x]);
}
void init()	//初始化 
{
	for(int i=1;i<=n;i++)father[i]=i;
	while(!q.empty())q.pop();
}
int kruskal()
{
	int ans=0,k=0;	//记录最小生成树权值和边数 
	while(k<n-1){
		int u=q.top().u,v=q.top().v,w=q.top().w;
		q.pop(); 
		if(find(u)!=find(v)){	//u,v还没连接在一起 
			ans+=w;	//更新权重 
			k++; 	//更新边数 
			father[find(u)]=find(v);	//合并 
		}
	} 
	return ans;
}
int main()
{
	while(scanf("%d",&n)&&n){
		init();	//初始化 
		getchar();	//读入换行符 
		for(int i=1;i<=n-1;i++){
			char c;
			int u,v,w;
			scanf("%c %d",&c,&m);
			u=c-'A'+1;
			for(int j=1;j<=m;j++){
				scanf(" %c %d",&c,&w);
				v=c-'A'+1;
				q.push(Edge{u,v,w});
			}
			getchar();	//读入换行符 
		}
		printf("%d\n",kruskal());
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CVE-2021-35394是Realtek Jungle SDK中的一个操作系统命令注入漏洞。该漏洞使攻击者能够通过特制的网络请求向受影响的设备发送恶意命令,从而执行任意操作系统命令。这可能导致远程执行代码、敏感信息泄露或设备完全被接管。 为了保护受影响的设备免受此漏洞的影响,建议采取以下措施: 1. 更新固件:请查找设备制造商的官方网站,了解是否提供了修复此漏洞的安全补丁或更新固件。及时应用这些更新可以修复漏洞并提高设备的安全性。 2. 网络隔离:将受影响的设备与其他重要设备隔离开来,以减少潜在攻击者利用该漏洞对其他系统造成的风险。 3. 最小权限原则:为设备配置最小权限,确保只有必要的功能和服务可用。这可以减少攻击者利用漏洞所获得的权限和影响范围。 4. 安全审查:对于使用Realtek Jungle SDK的应用程序或设备,进行安全审查以识别潜在的漏洞和安全风险。这可以帮助您及时采取措施来修复和保护系统。 5. 安全意识培训:为设备使用者和网络管理员提供相关的安全意识培训,教育他们识别和防范网络攻击,并强调及时报告任何可疑活动。 请注意,这只是一些建议措施,具体的防护方法可能因设备和环境而异。如果您是受影响的设备的用户或管理员,请及时联系设备制造商获取更多的安全建议和支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值