题面

题解

跑两遍\(Kruskal\),第一次找出\(k\)条一级公路,第二次找出\(n - k - 1\)条二级公路,直接计算\(MST\)的权值之和即可。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) using namespace std; inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
} int n, m, fa[20003], k, vis[20003];
struct OIer
{
int a, b, c1, c2, id;
} d[20003];
struct STO_Tham
{
int Id, Tham;
} ans[20003]; int getf(int u)
{
if (fa[u] == u) return u;
return fa[u] = getf(fa[u]);
} int maxx = 900000000, maxy, tot; inline bool cmp(OIer x, OIer y) {return x.c1 < y.c1;} inline bool cmp1(OIer x, OIer y) {return x.c2 < y.c2;} inline bool OrzTham(STO_Tham x, STO_Tham y) {return x.Id < y.Id;} int main()
{
//File("P2323");
n = gi(), k = gi(), m = gi(); --m;
for (int i = 1; i <= m; i+=1)
{
d[i].a = gi(), d[i].b = gi(), d[i].c1 = gi(), d[i].c2 = gi(), d[i].id = i;
}
for (int i = 1; i <= n; i+=1) fa[i] = i;
sort(d + 1, d + 1 + m, cmp);
for (int i = 1; i <= m; i+=1)
{
int u = getf(d[i].a), v = getf(d[i].b);
if (u != v)
{
fa[u] = v;
ans[++tot].Id = d[i].id, ans[tot].Tham = 1;
maxy = max(maxy, d[i].c1);
if (tot == k) break;
}
}
sort(d + 1, d + 1 + m, cmp1);
for (int i = 1; i <= m; i+=1)
{
int u = getf(d[i].a), v = getf(d[i].b);
if (u != v)
{
fa[u] = v;
ans[++tot].Id = d[i].id, ans[tot].Tham = 2;
maxy = max(maxy, d[i].c2);
if (tot == n - 1) break;
}
}
sort(ans + 1, ans + 1 + tot, OrzTham);
printf("%d\n", maxy);
for (int i = 1; i < n; i+=1) printf("%d %d\n", ans[i].Id, ans[i].Tham);
return 0;
}

题解【洛谷P2323】 [HNOI2006]公路修建问题的更多相关文章

  1. 洛谷 P2323 [HNOI2006]公路修建问题 解题报告

    P2323 [HNOI2006]公路修建问题 题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 思路: 二分答案 然后把每条能加的大边都加上,然后加小边 但在洛谷的题 ...

  2. 洛谷P2323 [HNOI2006] 公路修建问题 [二分答案,生成树]

    题目传送门 公路修建问题 题目描述 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Associa ...

  3. 洛谷 P2323 [HNOI2006]公路修建问题

    题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 3 4 4 ...

  4. 1196/P2323: [HNOI2006]公路修建问题

    1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2191  Solved: 1258 Descriptio ...

  5. 【洛谷P1265】公路修建

    公路修建 题目链接 分析题意,可以发现,在(1)的条件下,(2)的情况是不会发生的, 于是直接求MST(Min Set Tree) 然而稠密图克鲁斯卡尔会TLE,建图还会爆空间, 所以用prime,用 ...

  6. P2323 [HNOI2006]公路修建问题

    题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 输出样例# ...

  7. 【MST】P2323 [HNOI2006]公路修建问题

    Description 给定 \(n\) 个点 \(m - 1\) 条无向边,每条边有两种边权,贵一点的和便宜一点的.要求至少选择 \(k\) 条贵边使得图联通且花费最大的边权值最小. Input 第 ...

  8. BZOJ 1196: [HNOI2006]公路修建问题 Kruskal/二分

    1196: [HNOI2006]公路修建问题 Time Limit: 1 Sec  Memory Limit: 162 MB 题目连接 http://www.lydsy.com/JudgeOnline ...

  9. 【最小生成树】BZOJ 1196: [HNOI2006]公路修建问题

    1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1435  Solved: 810[Submit][Sta ...

  10. bzoj 1196: [HNOI2006]公路修建问题 二分+并查集

    题目链接 1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1576  Solved: 909[Submit ...

随机推荐

  1. Spark学习之路 (九)SparkCore的调优之数据倾斜调优[转]

    调优概述 有的时候,我们可能会遇到大数据计算中一个最棘手的问题--数据倾斜,此时Spark作业的性能会比期望差很多.数据倾斜调优,就是使用各种技术方案解决不同类型的数据倾斜问题,以保证Spark作业的 ...

  2. 【33】卷积步长讲解(Strided convolutions)

    卷积步长(Strided convolutions) 卷积中的步幅是另一个构建卷积神经网络的基本操作,让我向你展示一个例子. 如果你想用3×3的过滤器卷积这个7×7的图像,和之前不同的是,我们把步幅设 ...

  3. mssql格式化工具——SQL PRETTY PRINTER

    1.mssql版本 mssql格式化工具有4个版本 1.桌面版 2.mssql插件 3.vs插件 4.api 2.下载地址 下载地址:http://www.dpriver.com/dlaction.p ...

  4. PUT方法写shell

    前言: PUT是http的一个请求方法 PUT的前提,是了解HTTP协议.下面给出HTTP - PUT的一个模板: PUT /test.txt HTTP/1.1 Accept: */* Accept- ...

  5. linux commands - 一次性解压多个tar.gz文件

    tar -zxvf list所有tar.gz文件,然后利用xargs将其作为参数传给tar命令.-n 1表示每次传一个参数. xargs: https://www.cnblogs.com/wangqi ...

  6. Selenium3+python自动化014-自动化常用设计模式页面对象模型 (Page Object)

    一.概 念: PO(Page Object)设计模式是一种面向对象(页面对象)的设计模式,将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理. 二.优点可以使代码复用,降低维 ...

  7. Nginx出现403 forbidden

    我装在linux上的nginx版本是1.16 当我在nginx/conf/nginx.conf文件里配置完代理 location /ds { root /home/nginx; index index ...

  8. vue报错There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these mod

    今天在开发一个新项目时,当安装完依赖包启动项目后报了一个这个错 There are multiple modules with names that only differ in casing.Thi ...

  9. 常用命令 在linux下

    1.拷贝某个目录及其下的所有的文件到另外一个目录 语法:cp -r <source directory name>/ <destination directory name>/ ...

  10. 获取redis cluster master对应的slot分布情况

    需求:原生的redis-trib.rb功能是强大,但输出的内容过于繁杂,比如我需要关注哪些master对应哪些slots,不是很直观,如果集群的规模更大的话,那么输出的结果获取信息更加困难. 说明:这 ...