HDU2122 Ice_cream’s world III【Kruskal】

2024-06-15 05:38
文章标签 hdu2122 ice cream world iii kruskal

本文主要是介绍HDU2122 Ice_cream’s world III【Kruskal】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Ice_cream’s world III


Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 997    Accepted Submission(s): 321

Problem Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
 
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.

Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
 
Sample Input
2 1
0 1 10

4 0
 
Sample Output
10

impossible
 
Author
Wiskey
 
Source

HDU 2007-10 Programming Contest_WarmUp


题目大意:给你N个点(编号为0~N-1),M条路,问最小生成树是多少,如果不能生成

小生成树,则输出impossible

思路:用Kruskal来做,如果最后得不到N-1条路,就输出impossible,否则就输出结果。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;const int MAXN = 1100;
const int MAXM = 10010;
int N,M,father[MAXN];int find(int x)
{if(x != father[x])father[x] = find(father[x]);return father[x];
}
struct EdgeNode
{int from;int to;int w;
}Edges[MAXM];int cmp(EdgeNode a, EdgeNode b)
{return a.w < b.w;
}
void Kruskal()
{int ans = 0;int Count = 0;for(int i = 0; i < M; ++i){int u = find(Edges[i].from);int v = find(Edges[i].to);if(u != v){ans += Edges[i].w;father[v] = u;Count++;if(Count == N-1)break;}}if(Count == N-1)printf("%d\n\n",ans);elseprintf("impossible\n\n");
}
int main()
{while(~scanf("%d%d",&N,&M)){memset(Edges,0,sizeof(Edges));for(int i = 0; i <= N; ++i)father[i] = i;for(int i = 0; i < M; ++i){scanf("%d%d%d",&Edges[i].from,&Edges[i].to,&Edges[i].w);}sort(Edges,Edges+M,cmp);Kruskal();}return 0;
}


这篇关于HDU2122 Ice_cream’s world III【Kruskal】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062572

相关文章

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例

leetcode刷题(94)——337. 打家劫舍 III

在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 示例 1: 输入: [3,2,

力扣SQL50 销售分析III having + 条件计数

Problem: 1084. 销售分析III 👨‍🏫 参考题解 Code select s.product_id,p.product_namefrom sales s left join product pon s.product_id = p.product_idgroup by product_idhaving count(if(sale_date between

代码随想录算法训练营第四十六天 | 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II、123.买卖股票的最佳时机III

121. 买卖股票的最佳时机 题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/ 文档讲解:https://programmercarl.com/0121.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4… 视频讲解:https://www.bil

代码随想录算法训练营第四十六天| 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II、123.买卖股票的最佳时机III

LeetCode 121. 买卖股票的最佳时机 题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/ 文章链接:https://programmercarl.com/0121.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%B

AJAX:如何编写一个关于AJAX的Hello World?(ajax发送异步请求(四步操作))

用到的一个Servlet类: package cn.edu.web.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;impor

oracle学习之第一个存储过程:打印Hello World

数据库对象:表、视图、索引、序列、同义词、存储过程、存储函数 存储过程:指的是存储在数据库中供所有用户程序调用的子程序叫存储过程、存储函数 存储过程和存储函数的相同点:完成特定功能的程序 存储过程和存储函数的区别:是否用return语句返回值(存储函数可以,但是存储过程不行) --第一个存储过程:打印Hello World/*调用存储过程2种方式:1、exec sayhellow

在Mac OS上使用Visual Studio Code创建C++ Qt的Hello World应用

引言 Qt是一个跨平台的应用程序和用户界面框架,而Visual Studio Code是一个功能强大的编辑器,两者结合可以极大地提升开发效率。本文将指导你在Mac OS上使用Visual Studio Code创建一个简单的Qt 'Hello World'窗口应用。 环境准备 确保你的MacBook OS运行最新的操作系统。安装Homebrew,Mac OS的包管理器。通过Homebrew安装

关于FPGA的浮点数处理 III

关于FPGA的浮点数处理 III 语言 :System Verilg EDA工具:ISE、Vivado、Quartus II 关于FPGA的浮点数处理 III一、引言二、单精度浮点数运算的FPGA实现1. 单精度浮点数的加法FPGA实现(1)实现代码(FpAdd模块)(2)代码分析 2. 单精度浮点数的乘法FPGA实现(1)实现代码(FpMul模块)(2)代码分析 三、结尾

SpringBoot (一) :入门篇 Hello World

什么是SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。 SpringBoot有什么