Networking (POJ - 1287)(并查集+最小生成树)

本文介绍了一种在给定点和路线集合中寻找连接所有点的最小生成树算法。通过实例展示了如何使用并查集实现该算法,以确保所有点被连接且总线路长度最短。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 
Output
For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.
Sample Input
1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0
Sample Output
0
17
16
26

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int f[100];//并查集 
struct note
{
	int a,b,c;
}tree[100010];
bool cmp(struct note t1,struct note t2)
{
	return t1.c<t2.c;
}

int getf(int v)//查找父节点 
{
	int r=v;
	while(r!=f[r])
	{
		r=f[r];
	}
	f[v]=r;//路径压缩 
	return r;
}
int mergef(int u,int v)//合并父节点 
{
	int t1=getf(u);
	int t2=getf(v);
	if(t1!=t2)
	{
		f[t1]=t2;
		return 1;
	}
	else
	return 0;
}

int main(void)
{
	int n,m;
	while(~scanf("%d",&n))
	{
		if(n==0)
		break; 
		scanf("%d",&m);
		int a,b,c;
		for(int i=0;i<m;i++){
			scanf("%d %d %d",&tree[i].a,&tree[i].b,&tree[i].c);
		}
		sort(tree,tree+m,cmp);//排序,使得权值小的路径在前 
		
		for(int i=1;i<=n;i++)//并查集初始化 
		f[i]=i;
		
		int sum=0;
		int step=0;
		for(int i=0;i<m;i++)//按权值从小到大枚举每一条路 
		{
			if(mergef(tree[i].a,tree[i].b))//如果这两个点之间还没有路 
			{
				sum+=tree[i].c;//更新生成树总长度 
				step++;
				if(step==n-1)//如果选够了n-1条路则退出循环 
				break;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}


### 解决 MySQL 中 `skip_networking` 参数设置为 OFF 但仍显示 ON 的问题 #### 1. **确认配置文件加载情况** 使用以下命令检查 MySQL 是否正确加载了预期的配置文件: ```sql SHOW VARIABLES LIKE '%config%'; ``` 结果中的 `Value` 字段指明当前运行实例所依赖的主要配置文件位置。如果存在多个配置文件(如 `/etc/my.cnf`, `/etc/mysql/my.cnf`, 或者用户目录下的 `.my.cnf`),需要逐一排查它们的内容,确保没有重复定义或覆盖 `skip-networking` 参数[^1]。 #### 2. **检查默认值与隐式行为** 即使在配置文件中明确设置了 `skip-networking=OFF`,某些情况下 MySQL 可能因其他因素继续禁用网络连接。例如,默认绑定地址可能限制了外部访问能力。通过以下方式验证绑定地址设置: ```sql SHOW VARIABLES LIKE 'bind_address'; ``` 如果结果显示为 `127.0.0.1` 或特定局域网 IP,则仅限本地回环接口或其他指定设备能够建立连接。此时可以尝试将绑定地址更改为通配符形式以开放所有适配器上的监听请求: ```ini [mysqld] bind-address = 0.0.0.0 ``` #### 3. **排除注释干扰** 确保配置文件内的 `skip-networking` 行未被错误地标记为注释状态。例如,“#skip-networking”表示该行已被忽略并不会生效。正确的写法应当是没有前置符号直接赋值的形式: ```ini skip-networking = OFF ``` 同样需要注意的是,有些版本支持布尔型开关表达(`ON|OFF`),但也接受完全删除关键字实现相同效果的方式[^2]。 #### 4. **验证服务重启过程** 更改完成后务必重新启动数据库引擎使得新设定得以应用: ```bash sudo systemctl restart mysql ``` 或者依据操作系统差异采用对应的服务管理工具完成同样的动作。随后再次进入客户端界面检验变量最终取值状况: ```sql SELECT @@global.skip_networking; -- 或者 SHOW GLOBAL VARIABLES WHERE Variable_name='skip_networking'; ``` #### 5. **深入诊断潜在冲突源** 假设前述步骤皆未能解决问题,那么可能存在额外的影响因子尚未识别出来。这时推荐启用详尽的日志记录机制辅助定位根本原因: ```ini [mysqld] general_log = 1 general_log_file = /path/to/your_general_query.log log_error = /path/to/your_error_message.log ``` 经过一段时间观察后审查生成的日志文档寻找异常提示信息[^3]。 --- ### 注意事项 - 调整涉及网络安全层面的关键参数时要格外小心,避免无意间引入不必要的安全隐患。 - 生产环境下实施改动前最好先备份原始配置副本以防万一发生不可逆损害。 - 测试完毕记得恢复初始安全策略以免长期暴露风险窗口。 ```python def verify_mysql_config(): import subprocess try: result = subprocess.run(['sudo', 'systemctl', 'restart', 'mysql'], check=True) print("MySQL service has been restarted successfully.") output = subprocess.check_output(["mysql", "-e", "SELECT @@global.skip_networking"]) if b'OFF' in output: print("The parameter skip_networking is now set to OFF correctly.") else: print("Failed to disable skip_networking despite configuration changes.") except Exception as e: print(f"An error occurred during verification process: {str(e)}") if __name__ == "__main__": verify_mysql_config() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值