CF1245D: Shichikuji and Power Grid
CF1245D: Shichikuji and Power Grid
题意描述:
- 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点需要\((|x_i-x_j|+|y_i-y_j|)*(k_i+k_j)\)的代价。对于一个点要么建立基站,要么连接建立基站的点。问最小代价是多少,同时输出建立基站的点和线路。
思路:
- 建立一个超级根,对于每个城市建立发电站,连接一条权值为\(c_i\)的边,再把每个城市之间连接电线的花费算出来,跑\(kruskal\)。
- 在跑的过程中记录哪些城市建立了发电站,哪些城市建立了电线。
- \(hint:\)注意开\(long\ long\)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000 + 10;
int n;
ll ans, x[maxn], y[maxn], k[maxn], c[maxn];
struct Node{
int x, y;
ll z;
}p[maxn*maxn];
int tot;
void add_edge(int x, int y, ll z){
p[++tot].x = x, p[tot].y = y, p[tot].z = z;
}
int fa[maxn];
bool cmp(Node a, Node b){
return a.z < b.z;
}
int get_fa(int x)
{
if(x == fa[x]) return x;
return fa[x] = get_fa(fa[x]);
}
int ans1[maxn], cnt1;
int ax[maxn], ay[maxn], cnt2;
void kruskal()
{
for(int i = 0; i <= n; i++) fa[i] = i;
sort(p+1, p+1+tot, cmp);
int cnt = 0;
for(int i = 1; i <= tot; i++)
{
int x = p[i].x, y = p[i].y;
ll z = p[i].z;
int fx = get_fa(x), fy = get_fa(y);
if(fx != fy)
{
if(x == 0 || y == 0) ans1[++cnt1] = x + y;
else ax[++cnt2] = x, ay[cnt2] = y;
ans += z; fa[fx] = fy; cnt++;
}
if(cnt == n) break;
}
cout << ans << endl;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &x[i], &y[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &c[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &k[i]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
if(i != j)
{
ll w = (k[i] + k[j]) * (abs(x[i]-x[j]) + abs(y[i]-y[j]));
add_edge(i, j, w);
}
}
for(int i = 1; i <= n; i++)
add_edge(0, i, c[i]), add_edge(i, 0, c[i]);
kruskal();
cout << cnt1 << endl;
for(int i = 1; i <= cnt1; i++)
printf("%d ", ans1[i]); puts("");
cout << cnt2 << endl;
for(int i = 1; i <= cnt2; i++)
printf("%d %d\n", ax[i], ay[i]);
return 0;
}
CF1245D: Shichikuji and Power Grid的更多相关文章
- 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) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...
- [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
链接: https://codeforces.com/contest/1245/problem/D 题意: Shichikuji is the new resident deity of the So ...
- Codeforces 1245 D. Shichikuji and Power Grid
传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...
- CodeForces 1245D Shichikuji and Power Grid
cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...
- 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 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 Round #597 (Div. 2)
A - Good ol' Numbers Coloring 题意:有无穷个格子,给定 \(a,b\) ,按以下规则染色: \(0\) 号格子白色:当 \(i\) 为正整数, \(i\) 号格子当 \( ...
随机推荐
- kubernetes 1.15 有哪些让人眼前一亮的新特性?
原文链接:kubernetes 1.15 有哪些让人眼前一亮的新特性? 2019 年 6 月 20 日,Kubernetes 重磅发布了 1.15 版本,不过笔者忙到现在才有空认真来看一下到底更新了哪 ...
- 每天固定备份db sqlserver
DECLARE @DBName varchar(255) DECLARE @DATABASES_Fetch int DECLARE DATABASES_CURSOR CURSOR FOR select ...
- golang --rune
rune 是int32的别名类型,专用于存储Unicode编码的单个字符 我们可以用5种方式来表示一个rune字面量: 该rune字面量所对应的字符,如'a'必须是Unicode编码规范所支持的 使用 ...
- Matlab代理模式
代理模式(Proxy)就是给一个对象提供一个代理对象,并有代理对象来控制对原有对象的引用.代理模式和装饰模式非常类似,但最主要的区别是代理模式中,代理类对被代理的对象有控制权,决定其执行或者不执行.本 ...
- Html中Css页面跳转问题
没有指定<a>的target属性值时,默认是"_blank,可以参考以下代码的设置来控制<a href="#" target="_blank& ...
- window 10 npm install node-sass报错
最近准备想用vue-cli初始化一个项目,需要sass-loader编译: 发现window下npm install node-sass和sass-loader一直报错, window 命令行中提示我 ...
- 揭秘MySQL主从数据不一致
前言: 目前MySQL数据库最常用的是主从架构,大多数高可用架构也是通过主从架构演变而来.但是主从架构运行时间长久后容易出现数据不一致的情况,比如因从库可写造成的误操作或者复制bug等,本篇文章将会详 ...
- (1)jmeter录制脚本(使用代理的方式)
(1)jmeter录制脚本(使用代理的方式) jmeter 2018年07月09日 17时27分24秒 很多APP使用badboy是无法录制的,这种情况下需要使用chrome或Firefox,如果能联 ...
- SqlServer中的更新锁(UPDLOCK和READPAST)
UPDLOCK和READPAST,通过UPDLOCK和READPAST的结合我们能够解决许多问题,比如我当前项目中对于更新预约人数,则用到了UPDLOCK和READPAST,因为考虑到并发如果固定预约 ...
- Flask-Script Manager
Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...