传送门

经典的最小生成树模型

建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right |+\left | y_i-y_j\right |)$

然后用 $prim$ 求个最小生成树即可,然后考虑一下输出方案

多维护一个 $frm[x]$ 表示当前的 $dis[x]$ 是从哪个点贡献来的就行了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=;
const ll INF=1e18;
int n,x[N],y[N],c[N],k[N];
inline ll cst(int i,int j) { return 1ll*(k[i]+k[j])*(abs(x[i]-x[j])+abs(y[i]-y[j])); }
ll dis[N],ans;
bool vis[N];
vector <int> bas;
vector <pair<int,int>> edg;
int frm[N];
void Prim()
{
for(int i=;i<=n;i++) dis[i]=c[i];
for(int I=;I<=n;I++)
{
ll mx=INF; int pos=-;
for(int i=;i<=n;i++)
if(!vis[i]&&dis[i]<mx)
mx=dis[i],pos=i;
vis[pos]=; ans+=mx;
if(!frm[pos]) bas.push_back(pos);
else edg.push_back(make_pair(frm[pos],pos));
for(int i=;i<=n;i++)
if(!vis[i]&&dis[i]>cst(pos,i))
dis[i]=cst(pos,i),frm[i]=pos;
}
}
int main()
{
n=read();
for(int i=;i<=n;i++)
x[i]=read(),y[i]=read();
for(int i=;i<=n;i++) c[i]=read();
for(int i=;i<=n;i++) k[i]=read();
Prim(); printf("%lld\n",ans);
printf("%d\n",int(bas.size()));
for(auto x: bas) printf("%d ",x); puts("");
printf("%d\n",int(edg.size()));
for(auto t: edg) printf("%d %d\n",t.first,t.second);
return ;
}

Codeforces 1245 D. Shichikuji and Power Grid的更多相关文章

  1. [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)

    [Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...

  2. Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 最小生成树

    D. Shichikuji and Power Grid</centerD.> Shichikuji is the new resident deity of the South Blac ...

  3. Shichikuji and Power Grid

    D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...

  4. CF1245D: Shichikuji and Power Grid

    CF1245D: Shichikuji and Power Grid 题意描述: 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点 ...

  5. Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

    链接: https://codeforces.com/contest/1245/problem/D 题意: Shichikuji is the new resident deity of the So ...

  6. Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 题解 最小生成树

    题目链接:https://codeforces.com/contest/1245/problem/D 题目大意: 平面上有n座城市,第i座城市的坐标是 \(x[i], y[i]\) , 你现在要给n城 ...

  7. CodeForces 1245D Shichikuji and Power Grid

    cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...

  8. codeforces Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

    #include<bits/stdc++.h> using namespace std ; int n; struct City { int id; long long x,y; //坐标 ...

  9. Codeforces Beta Round #10 A. Power Consumption Calculation 水题

    A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...

随机推荐

  1. OpenResty之ngx.var.VARIABLE

    1. ngx.var.VARIABLE syntax: ngx.var.VAR_NAME context: set_by_lua*, rewrite_by_lua*, access_by_lua*, ...

  2. 为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?解决方法如下

    为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?低版本中不是至少可以存储4个汉字吗?原因这个问题仍然与编码类型有关.ArcGIS 10.2 以及更早的版本,ArcGIS ...

  3. ArcGIS10.3_解决属性表中文乱码问题

    借鉴前辈们解决ArcMap低版本属性表乱码的问题解决方法,勇敢的尝试了一下Pro中的解决方法,其实道理都一样.先来看看第一种方法:打开CMD,如果是ArcMap,输入如下命令: reg add HKE ...

  4. perl 语法速查 | 模块安装

    perl -MCPAN -e shell install Bio::SeqIO 或者直接perl -MCPAN -e 'install Excel::Writer::XLSX' 用cpan装不上,编译 ...

  5. vue-resource在vuecli3中请求headers修改

    this.$resource.delete({ user_code: Cookie.get("empid"), date: date, file_name: file_name } ...

  6. 《高性能mysql》笔记(第一章,mysql的架构与历史)

    mysql的服务器逻辑架构图如下: 目前工作用的5.5版本,5.5版本开始mysql开始将innoDB作为默认的存储引擎,innoDB的表是基于聚簇索引建立的. mysql的存储引擎锁管理非常重要,在 ...

  7. Android插件化(三):OpenAtlas的插件重建以及使用时安装

    Android插件化(三):OpenAtlas的插件重建以及使用时安装 转 https://www.300168.com/yidong/show-2778.html    核心提示:在上一篇博客 An ...

  8. LEFT JOIN 关键字语法

    SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键 ...

  9. ABAP字符串操作1 检查字段小数位的长度

    目的: 标准值1-6检查----最多保留小数点后3位 ,如果超出3位,显示错误信息”请检查父件XXX工序XXX的标准值X 的数值XXXX超出3位 “,退出. 关键语法1. SPLIT ,        ...

  10. pycharm连接linux创建django工程

    windows中安装最新版pycharm--2018年10月的版本 点击创建工程,看下图: C:\Users\Lenovo\PycharmProjects\untitled4 这个路径不要动,一动就出 ...