Codeforces 1245 D. Shichikuji and Power Grid
经典的最小生成树模型
建一个点 $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的更多相关文章
- [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)
[Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...
- 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 ...
- Shichikuji and Power Grid
D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...
- CF1245D: Shichikuji and Power Grid
CF1245D: Shichikuji and Power Grid 题意描述: 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点 ...
- 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 ...
- Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 题解 最小生成树
题目链接:https://codeforces.com/contest/1245/problem/D 题目大意: 平面上有n座城市,第i座城市的坐标是 \(x[i], y[i]\) , 你现在要给n城 ...
- CodeForces 1245D Shichikuji and Power Grid
cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...
- 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; //坐标 ...
- Codeforces Beta Round #10 A. Power Consumption Calculation 水题
A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...
随机推荐
- mysql中查询某个字段重复的数据
SELECT corp_name,count(*) as sums FROM corp_tax WHERE corp_year = 2018 AND corp_month = 8 group by c ...
- RethinkDB是什么?—— 面向文档的NOSQL数据库,MVCC+Btree索引,pushes JSON to your apps in realtime采用push思路,优化的ssd存储
RethinkDB是什么? RethinkDB是新一代的面向文档的数据库存储管理系统,原本是MySQL中针对SSD优化的一个存储引擎,后来脱离了MySQL成为了独立的系统. 数据如何存储在磁盘上? 数 ...
- golang通过ssh实现远程文件传输
使用ssh远程操作文件, 主要是创建ssh, 直接上代码 import ( "fmt" "github.com/pkg/sftp" "golang.o ...
- Linux中 mkdir 创建文件夹命令
语法 mkdir (选项)(参数) 选项 -Z:设置安全上下文,当使用SELinux时有效: -m<目标属性>或--mode<目标属性>建立目录的同时设置目录的权限: -p或- ...
- 网络中的tarpit/tar pit
最近看haproxy源码,里面有个TARPIT的概念不能理解,找了很久才找到对应的意思.特此记录. tarpit 本意是“沼泽地.地洼地”,这里显然把它引申为“捕获或者困住某个物体”. 在网络语义中提 ...
- telnet nmap netstap
yum install nmap [root@10-13-109-236 ~]# nmap localhost Starting Nmap 6.40 ( http://nmap.org ) at 20 ...
- vue---vue2.x自定义plugin,给vue添加全局方法,原型上增加全局方法
1. 自定义plugin.js export default{ install(Vue,options); { Vue.prototype.toStringTwo=(str)=>( ('0000 ...
- Coherent Calculator
计算逻辑 输入想要的参数后点击以下按钮进行计算和调整: Formula Bigger N Smaller N Bigger M Smaller M 所以在这个策略中Ft被Fixed在输入的值,其他的三 ...
- android中SpannableString之富文本显示效果
SpannableString其实和String一样,都是一种字符串类型,SpannableString可以直接作为TextView的显示文本,不同的是SpannableString可以通过使用其方法 ...
- [Java复习] 缓存Cache part1
1. 在项目中是如何使用缓存的?为什么要用?不用行不行?用了可能会有哪些不良后果? 结合项目业务,主要两个目的:高性能和高并发.缓存走内存,天然支持高并发. 不良后果: 缓存与DB双写不一致 缓存雪崩 ...