URAl 1416 Confidential【次小生成树】

博客介绍了如何解决1416号问题,即在给定的图中找到最小生成树和可能存在的次小生成树的权值和。如果存在这样的次小生成树,即使其权值与最小生成树相同,也需要找出。文章提供了解题思路,建议使用特定的算法模板来求解。

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

1416. Confidential

Time limit: 2.0 second
Memory limit: 64 MB
Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.

Input

The first line contains integers  n and  m that are a number of planets in the Galaxy and an amount of passages between them (2 ≤  n ≤ 500). The next  m lines contain integers  aibi and  wi that are the numbers of the planets connected with the passage and the transition cost (1 ≤  aibi ≤  n; 0 ≤  wi≤ 1000). If an  A to  B transition is possible then a  B to  A transition is possible too, and the cost of these transitions are equal. There is no more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.

Output

You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by −1.

Samples

input output
4 6
1 2 2
2 3 2
3 4 2
4 1 2
1 3 1
2 4 1
Cost: 4
Cost: 4
3 2
1 2 2
2 3 2
Cost: 4
Cost: -1
Problem Author: Den Raskovalov
Problem Source: The Ural State University Championship, October 29, 2005

题意:

n个顶点,m 条边,给出每条边连接的顶点编号,以及边的权值,求使得所有点相连通需要的边的最小的权值和以及第二小的权值和是多少,不存在的话,输出-1


题解:

求最小生成树和次小生成树(非严格次小,也就是权值可以和最小的相等)是否存在,存在的话,输出他们的权值

模板,点这里,稍微修改下模板,就能AC了....


/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=505;
int map[maxn][maxn];
bool vis[maxn];
int low[maxn];
int mst[maxn][maxn],pre[maxn];
bool inmst[maxn][maxn];
int n,m;
void init()
{
	memset(map,inf,sizeof(map));
	for(int i=1;i<=n;++i)
	{
		map[i][i]=0;
	}
}
int mintree()
{
	int mincost=0;
	int next,Min;
	memset(inmst,0,sizeof(inmst));
	memset(mst,0,sizeof(mst));
	for(int i=1;i<=n;++i)
	{
		low[i]=map[1][i];
		vis[i]=0;
		pre[i]=1;
	}
	vis[1]=1;
	for(int i=2;i<=n;++i)
	{
		Min=inf;next=-1;
		for(int j=1;j<=n;++j)
		{
			if(!vis[j]&&Min>low[j])
			{
				next=j;
				Min=low[j];
			}
		}
		mincost+=Min;
		if(next==-1)
		{
			return -1;
		}
		vis[next]=1;
		int fa=pre[next];
		inmst[next][fa]=inmst[fa][next]=1;
		for(int j=1;j<=n;++j)
		{
			if(vis[j]&&j!=next)
			{
				mst[j][next]=mst[next][j]=max(mst[fa][j],low[next]);
			}
			if(!vis[j]&&low[j]>map[next][j])
			{
				low[j]=map[next][j];
				pre[j]=next;
			}
		}
	}
	return mincost;
}
int sectree(int x)//参数是最小生成树的权值 
{
	int ans=inf;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<i;++j)
		{
			if(map[i][j]!=inf&&!inmst[i][j])
			{
				ans=min(ans,x-mst[i][j]+map[i][j]);
			}
		}
	}
	return ans==inf?-1:ans;//不存在的为-1,否则返回权值 
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();
		for(int i=1;i<=m;++i)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			map[a][b]=map[b][a]=min(map[a][b],c);
		}
		int cost1=mintree(),cost2=cost1==-1?-1:sectree(cost1);
		printf("Cost: %d\n",cost1);
		printf("Cost: %d\n",cost2);
	}
	return 0;
}


### 关于 UniApp 框架推荐资源与教程 #### 1. **Uniapp 官方文档** 官方文档是最权威的学习资料之一,涵盖了从基础概念到高级特性的全方位讲解。对于初学者来说,这是了解 UniApp 架构技术细节的最佳起点[^3]。 #### 2. **《Uniapp 从入门到精通:案例分析与最佳实践》** 该文章提供了系统的知识体系,帮助开发者掌握 Uniapp 的基础知识、实际应用以及开发过程中的最佳实践方法。它不仅适合新手快速上手,也能够为有经验的开发者提供深入的技术指导[^1]。 #### 3. **ThorUI-uniapp 开源项目教程** 这是一个专注于 UI 组件库设计实现的教学材料,基于 ThorUI 提供了一系列实用的功能模块。通过学习此开源项目的具体实现方式,可以更好地理解如何高效构建美观且一致的应用界面[^2]。 #### 4. **跨平台开发利器:UniApp 全面解析与实践指南** 这篇文章按照章节形式详细阐述了 UniApp 的各个方面,包括但不限于其工作原理、技术栈介绍、开发环境配置等内容,并附带丰富的实例演示来辅助说明理论知识点。 以下是几个重要的主题摘选: - **核心特性解析**:解释了跨端运行机制、底层架构组成及其主要功能特点。 - **开发实践指南**:给出了具体的页面编写样例代码,展示了不同设备间 API 调用的方法论。 - **性能优化建议**:针对启动时间缩短、图形绘制效率提升等方面提出了可行策略。 ```javascript // 示例代码片段展示条件编译语法 export default { methods: { showPlatform() { console.log(process.env.UNI_PLATFORM); // 输出当前平台名称 #ifdef APP-PLUS console.log('Running on App'); #endif #ifdef H5 console.log('Running on Web'); #endif } } } ``` #### 5. **其他补充资源** 除了上述提到的内容外,还有许多在线课程视频可供选择,比如 Bilibili 上的一些免费系列讲座;另外 GitHub GitCode 平台上也有不少优质的社区贡献作品值得借鉴研究。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值