UVA 1151 买还是建(最小生成树+子集枚举+贪心)

IFIX5.8在Win10系统下的安装避坑指南 本文提供了IFIX5.8在Windows 10系统下的详细安装教程与避坑指南。针对常见的驱动缺失、UAC拦截及兼容性报错等问题,文章从安装前准备、分步安装流程到安装后配置与驱动修复,给出了系统性的解决方案,帮助工控从业者顺利完成部署。 阅读详情

World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to setup a new network in Borduria, a nice country that recently managed to get rid of its military dictator Kurvi-Tasch and which is now seeking for investments of international companies (for a complete description of Borduria, have a look to the following Tintin albums “King Ottokar’s Sceptre”, “The Calculus Affair” and “Tintin and the Picaros”). You are requested to help WWN todecide how to setup its network for a minimal total cost.
There are several local companies running small networks (called subnetworks in the following) that partially cover the n largest cities of Borduria. WWN would like to setup a network that connects all n cities. To achieve this, it can either build edges between cities from scratch or it can buy one or several subnetworks from local companies. You are requested to help WWN to decide how to setup its network for a minimal total cost. • All n cities are located by their two-dimensional Cartesian coordinates. • There are q existing subnetworks. If q ≥ 1 then each subnetwork c (1 ≤ c ≤ q) is defined by a set of interconnected cities (the exact shape of a subnetwork is not relevant to our problem). • A subnetwork c can be bought for a total cost wc and it cannot be split (i.e., the network cannot be fractioned). • To connect two cities that are not connected through the subnetworks bought, WWN has to build an edge whose cost is exactly the square of the Euclidean distance between the cities.
You have to decide which existing networks you buy and which edges you setup so that the total cost is minimal. Note that the number of existing networks is always very small (typically smaller than 8).
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Each test case is described by one input file that contains all the relevant data. The first line contains the number n of cities in the country (1 ≤ n ≤ 1000) followed by the number q of existing subnetworks (0 ≤ q ≤ 8). Cities are identified by a unique integer value ranging from 1 to n. The first line is followed by q lines (one per subnetwork), all of them following the same pattern: The first integer is the number of cities in the subnetwork. The second integer is the the cost of the subnetwork (not greater than 2×106). The remaining integers on the line (as many as the number of cities in the subnetwork) are the identifiers of the cities in the subnetwork. The last part of the file contains n lines that provide the coordinates of the cities (city 1 on the first line, city 2 on the second one, etc). Each line is made of 2 integer values (ranging from 0 to 3000) corresponding to the integer coordinates of the city.
Output
For each test case, your program has to write the optimal total cost to interconnect all cities. The outputs of two consecutive cases will be separated by a blank line.
A 115 Cities Instance Consider a 115 cities instance of the problem with 4 subnetworks (the 4 first graphs in Figure 1). As mentioned earlier the exact shape of a subnetwork is not relevant still, to keep figures easy to read, we have assumed an arbitrary tree like structure for each subnetworks. The bottom network in Figure 1 corresponds to the solution in which the first and the third networks have been bought. Thin edges correspond to edges build from scratch while thick edges are those from one of the initial networks.
Figure 1: A 115 Cities Instance and a Solution (Buying the First and the Third Network)
Sample Explanation: The sample input instance is shown in Figure 2. An optimal solution is described in Figure 3 (thick edges come from an existing network while thin edges have been setup from scratch).
Figure 2: The 7 City instance of the sample input
Figure 3
An optimal solution of the 7 City instance in which which the first and second existing networkshave been bought while two extra edges (1,5) and (2,4) have been setup
Sample Input
1
7 3 2 4 1 2 3 3 3 6 7 3 9 2 4 5 0 2 4 0 2 0 4 2 1 3 0 5 4 4
Sample Output
17 

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 1000005
using namespace std;

int n,m,x,k;
struct edge
{
    int a,b;
    double w;
    edge(int x=0,int y=0,double  z=0)
    {
        a=x;
        b=y;
        w=z;
    }
};

bool cmp(edge e1,edge e2)
{
    return e1.w<e2.w;
}

/*
题目大意:给定n个点的坐标,
和m种套餐策略,要求如何建立最优的连通方案。

不难想到子集枚举套餐,
然后把套餐中的点边全部用权重为0的边代替,
然后和对于每个套餐的枚举,
再遍历事先得到的最小生成树的边集合,累加答案,
就可以得到最优的套餐策略。
*/

int pre[maxn];
int Find(int x) { return x==pre[x]?x:pre[x]=Find(pre[x]); }
bool Union(int x,int y)
{
    int tx=Find(x),ty=Find(y);
    if(tx==ty) return false;
    pre[tx]=ty;
    return true;
}

edge e[maxn],kual[maxn],tc[maxn];///完全图的边集合
int taocan[10][1005],cost[20];

int dx[maxn],dy[maxn];
int dist(int i,int j)
{
    int ret=0;
    int x1=dx[i],y1=dy[i];
    int x2=dx[j],y2=dy[j];
    ret=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return ret;
}

int ans=0;

int main()
{
    int t;scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&k,&cost[i]);
            taocan[i][0]=k;
            for(int j=1;j<=k;j++)
            {
                scanf("%d",&x);
                taocan[i][j]=x;///记录套餐的组合
            }
        }

        for(int i=0;i<n;i++) scanf("%d%d",&dx[i],&dy[i]);

        if(ca!=1) puts("");///每个样例之间空一行

        int cnt=0,index=0;///完全图,生成树
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                e[cnt++]=edge(i+1,j+1,dist(i,j));///完全图的边集合

        sort(e,e+cnt,cmp);
        for(int i=1;i<=n;i++) pre[i]=i;
        for(int i=0;i<cnt;i++)  if(Union(e[i].a,e[i].b)) kual[index++]=e[i],ans+=e[i].w;

        ans=2e9;
        for(int i=0;i<(1<<m);i++)///枚举子集
        {
            int cst=0;
            for(int j=1;j<=n;j++) pre[j]=j;

            for(int j=0;j<m;j++)
            {
                if(i&(1<<j))
                {
                    cst+=cost[j];
                    for(int p=2;p<=taocan[j][0];p++)  Union(taocan[j][p],taocan[j][p-1]);
                }
            }

            for(int j=0;j<index;j++)   if(Union(kual[j].a,kual[j].b)) cst+=kual[j].w;
            ans=min(ans,cst);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

ChatGPT设置密钥后Codex免手机验证:配置顺序与身份凭证复用机制 API Key 是一种用于身份识别的凭证,其核心原理是通过预配置的密钥替代交互式登录流程。在开发工具中,密钥认证优先级通常高于会话认证,当本地存在有效密钥时,工具会直接复用该凭证,从而减少重复验证环节。这种机制的技术价值在于提升开发效率与配置一致性,尤其适用于多设备切换、CI/CD 流水线及编程辅助工具等场景。以 Codex 为例,在 ChatGPT 界面提前配置 API 密钥后,Codex 启动时可读取持久化存储的密钥,避免触发手机号验证,实现身份凭证的跨入口复用。 阅读详情

相关推荐

基于STM32F407与AD9910/AD9959的高频可调DDS信号源设计与实战

本文详细介绍了基于STM32F407微控制器与AD9910/AD9959高性能DDS芯片的高频可调信号源设计与实现。文章从DDS原理入手,阐述了双芯片方案的优势,并重点分享了高频电路设计中的电源、PCB布局布线等关键硬件设计技巧与避坑指南。同时,提供了通过SPI配置DDS芯片及利用Matlab生成任意波形的软件驱动方法,旨在帮助开发者打造一个覆盖10Hz至4200MHz的实用信号发生器。

weixin_36184718的博客 126

UVA1151 买还是建 Buy or Build

【代码】UVA1151 买还是建 Buy or Build。

m0_73035684的博客 119

Android14 有线网络静态IP配置:从固件烧录到系统默认设置

本文详细解析了在Android14系统中,如何通过修改系统框架层和设置应用,为设备预设有线网络静态IP地址,实现固件烧录后即具备指定网络配置的功能。文章深入探讨了从扩展SettingsProvider数据库字段、配置默认值,到在设置应用中读取和应用配置的完整流程,旨在解决工业平板、智能终端等设备在无DHCP网络环境中批量部署的痛点,提升开箱即用体验。

lake5的博客 926

uva 1151(最小生成树,枚举子集)

题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方。另外还有q(0<=q<=8)个套餐,可以购买,如果你购买了第i个套餐,该套餐中的所有结点将变得相互连通,第i个套餐的花费为ci。 kruskal: 先求一次原图的最小生成树,得到n-1条边,然后每次枚举完套餐后只考虑套餐中的边和这n-1...

weixin_30362233的博客 146

nvlddmkm事件ID14报错排查全指南:从找不到描述到显卡驱动崩溃

系统日志中的“无法找到描述”往往让人一头雾水,但这类提示其实隐藏着驱动故障的关键线索。Windows事件查看器通过事件源和ID记录系统异常,其中显卡驱动相关的内核模式驱动组件一旦超时失败,就会触发TDR机制,产生13、14、153等不同事件ID。理解这些ID的含义,比纠结“找不到描述”本身更重要。对于频繁出现的驱动崩溃问题,可以从日志频率和发生场景判断方向,再通过安全模式下彻底清理驱动、关闭冲突软件、检查供电和温度等步骤逐层定位。无论是游戏满载黑屏,还是睡眠唤醒后闪退,掌握NVIDIA驱动日志的解读逻辑,都

weixin_30545945的博客 468

RealSense D455启动教程

RealSense D455启动教程 修改rs_camera.launch

每天进步一点点 2423

uva1151(最小生成树)

题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3592 二进制枚举 1 #include<cstdio> 2 #include<cstring> 3 #incl...

weixin_30794491的博客 155

AIoT 在线工程实训平台案例——智能家居

AIoT 在线工程实训平台案例——智能家居的操作步骤

m0_53608652的博客 1438

uva1151:最小生成树+状态枚举

uva1151 题意:被题目和图给吓到了,直接上这一题很简单。就是让所有城市都连通,有两种方案,一种是两个点之间修一条边,价格为两点距离的平方。另一种方案是买一个套餐,套餐里的城市都连通。求最下的花费。 题解:二进制状态压缩,枚举套餐,然后再并查集找最小生成树。和HDU1102很像,已经构造的边就合并,让后再查找剩下的。一次AC,很舒服~~ 代码: #include &lt;cstrin...

FlyingJack 248

非结构化数据在大数据湖中的应用实践

想象一下,你正在整理一个杂乱的书房。有些书整整齐齐地放在书架上,每本书都有明确的标题、作者和出版日期——这些就是结构化数据,它们像数据库中的表格,有固定的格式和字段。但书房里还有更多东西:散落的照片、手写的笔记、录音带、视频光盘、甚至还有画作。这些物品没有固定的格式,每件都独一无二——这就是非结构化数据。在技术术语中,非结构化数据是指没有预定义数据模型或组织形式的信息。文本文档(Word、PDF、TXT)电子邮件和社交媒体内容图像和照片(JPG、PNG)音频文件(MP3、WAV)

软件工程实践的博客 365

基于电阻分压 + 电压跟随器的直流母线电压采样电路

摘要:本文介绍了一种基于电阻分压和电压跟随器的直流母线电压采样电路设计。该电路通过R38(240kΩ)和R41(20kΩ)组成的分压网络将高电压(如48V)降至3V,再经GS8331运放构成的电压跟随器进行隔离缓冲,最后通过RC滤波输出给ADC采集。重点分析了关键元件的选型要求:分压电阻需高精度、低温度系数;运放需高输入阻抗、低失调电压;滤波电容需稳定性好。该设计适用于工业电源、新能源BMS、变频器等需要高精度电压采样的应用场景。

qq_37999850的博客 910

UVA 1151 Buy or Build 最小生成树+子集枚举

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3592 题目大意:平面上有nnn个点,你的任务是让所有n个点连通,你可以新建一些边,费用等于两个端点的欧几里得距离的平方。另外还有qqq个套餐可以购买,如果你购买了第iii个套餐,该...

xiji333的博客 263

RK3588开发板NPU加速YOLO目标检测完整实战教程

🚀 RK3588开发板NPU加速YOLO目标检测实战 本文详细介绍了如何在瑞芯微RK3588开发板上利用内置NPU加速YOLO目标检测的全流程: 1️⃣ 环境准备:配置RKNPU2 SDK和aarch64交叉编译器,搭建Ubuntu开发环境 2️⃣ 模型转换:从ONNX格式转换为专用RKNN模型,适配NPU硬件特性 3️⃣ 性能对比:NPU推理将处理时间从833ms降至50ms以下,帧率提升16倍 4️⃣ 实战应用:支持图像/视频流/摄像头实时检测,提供完整编译和运行命令 关键优势:专为边缘AI设计的6T

FJN110的博客 936

UVA 1151 Buy or Build

一道很好的最小生成树的题,在紫书上找的(主要是懒得读英文题)题面大意 平面有n个点 求最小生成树 点直接的花费是 欧几里得距离的平方 另外还有p个套餐(p p很小 很容易想到暴力枚举取哪些套餐 再暴力跑(也能过) 刘汝佳的书上是加了优化 先跑一次 筛出n-1条边 其他边就不用了 的确是很屌的想法(我自己想就是不用每次都排序  其实也很快 一个是130 一个148) 接下来吐槽一下 刘汝佳的书上

weilehuozhe的专栏 734

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

UVA 1151 Buy or Build (Kruskal+二进制枚举子集)

问题 L: Buy or Build 时间限制: 20 Sec  内存限制: 128 MB 提交: 6  解决: 4 [提交][状态][讨论版] 题目描述 World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to

albertluf的博客 265

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Buy or Build UVA - 1151 (最小生成树+枚举集合)

传送门 题意:现在在一个图上有n的点,每个点给出相应的坐标,然后两个点之间的距离就是欧几里得距离,之后还有几个套餐,套餐具有相应的价钱,套餐会把套餐内所给的点联通,最后问最小花费是多少呢? 题解:如果首先枚举集合的话呢?那么每次枚举完之后都会再进行一次求解最小生成树的过程,这样复杂度很高啊,首先枚举集合就有o(n^2)的复杂度,然后求解最小生成树又是o(logn)的排序加o(n)的贪心,复杂度...

肘子的博客 348

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

UVA 1151 Buy or Build (最小生成树)

先求出原图的最小生成树,然后枚举买哪些套餐,把一个套餐内的点相互之间边权为0,直接用并查集缩点。正确性是基于一个贪心, 在做Kruskal算法是,对于没有进入最小生成树的边,排序在它前面的边不会减少。 边比较多,用prim求最小生成树,效果比Kruskal好,枚举套餐的时候在用Kruskal。 prim和dijkstra的区别在于点距离的定义。 #include<bits/std...

weixin_34072857的博客 112

11.2.2-uva1151-kruskal算法综合运用,贪心

#include #include #include #include using namespace std; const int maxn=1000+10; int p[maxn],x[maxn],y[maxn]; //x于y表示点的坐标 int n;//点的个数 vector subn[10];//用vector作容器存储套餐数组 每个数组包含该套餐所包含的点 int cost[10

qq_41093189的博客 297

UVA1151 Buy or Build 题解

题目描述 平面上有nnn个点,你的任务是让所有nnn个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方。另外还有qqq个套餐,可以购买,如果你购买了第iii个套餐,该套餐中的所有结点将变得相互连通,第iii个套餐的花费为cicici。求最小花费。 解题思路 紫书P358, ...

weixin_39412108的博客 198

ACM题题题 经典题+分类 我真的还想什么都不想只搞ACM就好了~~~

1

神思love 757

紫薯第11章 图论

最小生成树模板 krudkal(东哥) 以边贪心,每次选最小边。 在这里插入代码片 prim(东哥) 以点为基准,每次选完一个点后,用这个点取更新最小边。 在这里插入代码片 最短路模板 dij(堆优化,东哥) 先把起点入队,之后把每次出队的点 对所有与它相连的点进行松弛操作。 直到所有点都访问完。 在这里插入代码片 spfa(之前做的) 在这里插入代码片 最长路模板(反向最短路) 在这里插入代码片 关键路径(拓扑) 在这里插入代码片 ...

qq_45377553的博客 205

SOJ题型分类

转自 BoBoAn_DouYa的博客 Happy coding! SOJ题型分类 sicily 1198 ...

Jundesky的博客 457

竞赛程序设计知识要点图谱

一、基础算法 1.枚举法 POJ1248 Safecracker HDU1172 猜数字 POJ1543 Perfect Cubes POJ1046 Color Me Less 2.递归法:Hanoi塔、Fibonacci数列、整数划分问题、排列问题 POJ1664 放苹果 3.分治法:快速排序、归并排序 4.贪心法:删数问题、背包问题 POJ2287 Tian

海岛Blog 1023

Soj题目分类 python代码)

正值期末复习,刷点soj放松下 但想看看能不能在找点关于数据结构的题目来做一下。 在网上看到有不少人上传过那些关于部分SOJ题目的描述,但是说实话有些乱 不过我看到有个网页中包含的一个类似文档的东西,里面讲述的还算比较整洁。但是不巧的是,它被分成了很多页,我要看就需要不断地点下一页,这点让我感觉很难受(懒…) 于是我就研究了下那个网页的源码,开始了写了个爬虫。 为了好看,将写好的东西,做

gc.collect() 793
上一篇: HDU 5775 Bubble Sort(树状数组+规律思维)
下一篇: HDU 6336 Problem E. Matrix from Arrays (数学规律)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值