初学并查集-2:最优布线问题

本文讨论了如何通过间接数据传输在给定数量的计算机之间建立连接,以实现任意两台计算机间连通的同时,使得总连接费用最低。通过输入计算机总数、连接个数以及每条连接的费用,该文提供了一种算法计算最优连接方案的总费用。

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

题目描述  Description

学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的。为了节省费用,我们考虑采用间接数据传输结束,就是一台计算机可以间接地通过其他计算机实现和另外一台计算机连接。

为了使得任意两台计算机之间都是连通的(不管是直接还是间接的),需要在若干台计算机之间用网线直接连接,现在想使得总的连接费用最省,让你编程计算这个最小的费用。

输入描述  Input Description

输入第一行为两个整数n,m(2<=n<=100000,2<=m<=100000),表示计算机总数,和可以互相建立连接的连接个数。接下来m行,每行三个整数a,b,c 表示在机器a和机器b之间建立连接的话费是c。(题目保证一定存在可行的连通方案, 数据中可能存在权值不一样的重边,但是保证没有自环)

输出描述  Output Description

输出只有一行一个整数,表示最省的总连接费用。

样例输入  Sample Input

3 3

1 2 1

1 3 2

2 3 1

样例输出  Sample Output

2

数据范围及提示  Data Size & Hint

最终答案需要用long long类型来保存

type r=record
       x,y,v:longint;
       end;

var n,m:longint; total:int64;
    used:array[1..100000]of boolean;
    cost:array[1..100000]of r;
    father:array[1..100000]of longint;
    i,j,k,l:longint;
    line:longint;

procedure qsort(i,j:longint);
          var head,tail,k:longint;
              temp:r;
          begin head:=i; tail:=j;
                k:=cost[(head+tail) div 2].v;
                while head<tail do
                      begin while cost[head].v<k do inc(head);
                            while k<cost[tail].v do dec(tail);
                            if head<=tail
                               then begin temp:=cost[head];
                                          cost[head]:=cost[tail];
                                          cost[tail]:=temp;
                                          inc(head);
                                          dec(tail);
                                    end;
                      end;
                if head<j then qsort(head,j);
                if i<tail then qsort(i,tail);
          end;

function find(x:longint):longint;
         begin if father[x]=x
                  then exit(x);
               father[x]:=find(father[x]);
               exit(father[x]);
         end;

procedure union(x,y:longint);
          begin father[find(x)]:=find(father[y]);
          end;

begin total:=0;
      readln(n,m);
      fillchar(used,sizeof(used),true);
      fillchar(cost,sizeof(cost),0);
      for i:=1 to n do
          father[i]:=i;
      for i:=1 to m do
          readln(cost[i].x,cost[i].y,cost[i].v);
      qsort(1,m);
      line:=0;
      i:=0;
      while i<=m do
            begin inc(i);
                  if find(cost[i].x)<>find(cost[i].y)
                     then begin inc(line);
                                union(cost[i].x,cost[i].y);
                                inc(total,cost[i].v);
                          end;
            end;
      writeln(total);
end.

转载于:https://www.cnblogs.com/spiderKK/p/4221900.html

### 如何在 IntelliJ IDEA 中配置和使用 Swagger #### 添加 Maven 依赖 为了使 Swagger 能够工作,在 `pom.xml` 文件中需加入特定的依赖项。这可以通过编辑项目的构建文件来完成: ```xml <dependencies> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <!-- swagger ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> </dependencies> ``` 这些依赖会引入必要的库用于生成 API 文档以及提供交互式的 UI 页面[^4]。 #### 创建 Swagger 配置类 接着创建一个新的 Java 类用来初始化并配置 Swagger 实例。通常命名为类似于 `SwaggerConfig.java` 的名称,并放置于合适的位置,比如 `config` 包内: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder().title("API文档").description("").termsOfServiceUrl("") .contact(new Contact("", "", "")) .license("").licenseUrl("").version("1.0") .build(); } } ``` 这段代码定义了一个 Spring Bean 来设置 Swagger 的基本信息和其他选项。 #### 启动应用测试 当上述步骤完成后,启动应用程序即可访问默认路径 `/swagger-ui.html` 查看自动生成的 RESTful 接口文档界面。通过浏览器打开该链接可以浏览到所有已暴露出来的 HTTP 请求方法及其参数说明等信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值