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 ...
随机推荐
- clickhouse redash---olap
curl -XPUT '10.1.193.250:9200/ngx-logstash-2019-06/_settings' -d ' { "index.mapping.total_field ...
- VUE -- iview table 组件 中使用 upload组件 上传组件 on render 事件不会触发问题
碰到的问题是: upload 组件在 on中写的监听事件不会被触发 在 props 中来监听:==>
- VS中卸载Visual Assist X
Tools=>Extensions and updates=>找到Visual Assist X 卸载:
- E: dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. 爆错解决办法
author :headsen chen date : 2019-06-06 10:09:06 root@ubuntu:~# apt-get remove java-1.8.0-openjdk E ...
- python2,socket多进程的错误pickle.PicklingError: Can't pickle
python2,socket多进程的错误pickle.PicklingError: Can't pickle 源码: #coding:utf-8 import socket import pickle ...
- Oracle查看表结构的方法【我】
Oracle查看表结构的方法 方法一: 在命令窗口下输入 DESC table_name; 回车 方法二: 在sql窗口下 SELECT DBMS_METADATA.GET_ ...
- DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名
model定义,使用DisplayAttribute public class AddressSetInfo { /// <summary> /// invoiceAddress.Id / ...
- javascript模拟生成uuid
function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r ...
- (七)UML之用例图
用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. ...
- mysql为字段添加或删除自增属性
删除自增属性: ALTER TABLE `members` CHANGE uid uid INT(10) UNSIGNED NOT NULL ; 添加自增属性: ALTER TABLE `member ...