Bad Cowtractors

2024-01-13 19:58
文章标签 bad cowtractors

本文主要是介绍Bad Cowtractors,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最大生成树~

Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie. 

Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Line 1: Two space-separated integers: N and M 

* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
Sample Input
5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17
Sample Output
42
Hint
OUTPUT DETAILS: 

The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.

#include<stdio.h>
#include<algorithm>
#define maxe 20005
#define maxv 1005
using namespace std;
struct list
{int a;int b;int cost;
}edge[maxe];
int pre[maxv];
int find(int a)
{int r=a;while(r!=pre[r])r=pre[r];while(a!=pre[a]){int z=a;a=pre[a];pre[z]=r;}return r;
}
bool cmp(struct list e,struct list f)
{return e.cost>f.cost;
}
void kruskal(int n,int m)
{long long ans=0;int num_edge=0;for(int i=0;i<=n;i++)pre[i]=i;sort(edge,edge+m,cmp);for(int i=0;i<m;i++){int fa,fb;fa=find(edge[i].a);fb=find(edge[i].b);if(fa!=fb){pre[fa]=fb;ans+=edge[i].cost;num_edge++;if(num_edge==n-1)break;}}if(num_edge!=n-1)printf("-1");elseprintf("%lld",ans);}
int main()
{int n,m;scanf("%d%d",&n,&m);for(int i=0;i<m;i++){scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].cost);}kruskal(n,m);return 0;
}


这篇关于Bad Cowtractors的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

【python requests错误】Caused by SSLError(SSLError(bad handshake: SysCallError(104, 'ECONNRESET')

错误描述: 在发送get请求时错误,执行下面一句时报错了: response = requests.get(image_url) 原因HTTPSConnectionPool(host='test-kkbuluo-resource.cdn.hzmltest.com', port=443): Max retries exceeded with url: /IMCORE/RESOURCE/LOG

nacos Spring cloud 报错 URI is not absolute、service not found、502 bad gateway

- 服务没找到,请加入依赖 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency> - 如果是 "URI is not absolute" , 将URL变成固定的字符串,例如

git or vscode-电脑电源断或者蓝屏-重启运行项目git报错-git : bad signnature 300000

1.场景         电脑电源断或者蓝屏-重启运行项目git报错-git : bad signnature 300000; 2.解决         2.1.git下的index文件损坏,直接删除;         2.2在.git文件目录运行重新回到上次节点或者指定版本 git reset

win10突然出现蓝屏,终止代码:BAD_POOL_CALLER

最近win10系统更新过后,出现了系统蓝屏的情况,开始只是偶尔蓝屏一下,后来开始有规律的隔个一个多小时就蓝屏。不管你是在查资料还是在听音乐,coding,突然就"滋滋…",这谁顶得住啊,不行,必须的折腾一下,给它修好。 百度,Google翻了很多的资料和blog,试遍了网上的方法,结果还是不行。 下面总结下解决的过程: 首先尝试的是在安全模式下,禁用显卡驱动,网上有这个图文教程,这个方案

iOS面试:BAD_ACCESS在什么情况下出现?

EXC_BAD_ACCESS 是一种常见的运行时错误,通常发生在 iOS 开发中。它指的是程序试图访问已释放或未分配的内存区域。具体来说,BAD_ACCESS 的出现通常与下面几种情况有关: 1. 访问已释放对象 当你尝试访问一个已经被释放的对象时,会导致 BAD_ACCESS 错误。这通常发生在以下场景: 对象未正确管理,可能是因为使用了 release 或 autorelease 移除对

Error:Unable to tunnel through proxy. Proxy returns HTTP/1.1 400 Bad Request

遇到这中问题,请参照http://www.2cto.com/kf/201608/541098.html这里 并且查看你的项目的gradle-wrapper.properties, 最后一行gradle版本改为本地的,当然.gradle文件里面的gradle版本本地也要有

两句话解决ChatGPT 502 Bad Gateway问题

一般是DNS的问题 管理员身份运行cmd输入ipconfig /flushdns输入netsh winsock reset重启电脑即可 这种问题很容易在长时间不关闭窗口,或者win发布更新补丁时出现

sh handle_data.sh: 2: handle_data.sh: Syntax error: Bad for loop variable

今天写了个简单shell处理数据,如下: #!/bin/shfor((i=1;i<220;i++));do/usr/bin/php /var/artisan handle_data 1;done; 结果报错 sh handle_data.sh: 2: handle_data.sh: Syntax error: Bad for loop variable 查询后发现是Ubun

2024.08.26【BUG报错】|GATK分析之Argument emit-ref-confidence has a bad value解决方案

GATK分析中Argument emit-ref-confidence错误解决方案 在使用GATK(Genome Analysis Toolkit)进行基因组变异分析时,我们可能会遇到一些参数错误,其中之一就是"Argument emit-ref-confidence has a bad value"。这个错误通常与Read Group的设置不当有关。本文将提供一种解决方案,通过正确设置Read

linux安装JDK:bash: ./java: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录

今天在Linux机器上安装JDK,安装完成后,查看安装版本:java -version,遇到了如下问题:          问题很简单,但确实是第一次遇到,通过查询度娘,发现也是个极其普遍的问题,so,如何解决呢?     只需一句命令:sudo yum install glibc.i686          执行过程稍长,可能需要稍作等待;