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 South Black Snail Temple. Her first job is as follows:
There are n new cities located in Prefecture X. Cities are numbered from 1 to n. City i is located xi km North of the shrine and yi km East of the shrine. It is possible that (xi,yi)=(xj,yj) even when i≠j.
Shichikuji must provide electricity to each city either by building a power station in that city, or by making a connection between that city and another one that already has electricity. So the City has electricity if it has a power station in it or it is connected to a City which has electricity by a direct connection or via a chain of connections.
Building a power station in City i will cost ci yen;
Making a connection between City i and City j will cost ki+kj yen per km of wire used for the connection. However, wires can only go the cardinal directions (North, South, East, West). Wires can cross each other. Each wire must have both of its endpoints in some cities. If City i and City j are connected by a wire, the wire will go through any shortest path from City i to City j. Thus, the length of the wire if City i and City j are connected is |xi−xj|+|yi−yj| km.
Shichikuji wants to do this job spending as little money as possible, since according to her, there isn't really anything else in the world other than money. However, she died when she was only in fifth grade so she is not smart enough for this. And thus, the new resident deity asks for your help.
And so, you have to provide Shichikuji with the following information: minimum amount of yen needed to provide electricity to all cities, the cities in which power stations will be built, and the connections to be made.
If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.
思路:
最小生成树,将每个点和n+1连边为建电站,然后跑最小生成树即可。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e3+10;
struct Edge
{
int u, v;
LL w;
bool operator < (const Edge& rhs) const
{
return this->w < rhs.w;
}
}edge[MAXN*MAXN+MAXN];
int n;
int c[MAXN], k[MAXN];
int x[MAXN], y[MAXN];
int Fa[MAXN];
int GetFa(int x)
{
if (Fa[x] == x)
return x;
Fa[x] = GetFa(Fa[x]);
return Fa[x];
}
int main()
{
scanf("%d", &n);
for (int i = 1;i <= n+1;i++)
Fa[i] = i;
for (int i = 1;i <= n;i++)
scanf("%d%d", &x[i], &y[i]);
for (int i = 1;i <= n;i++)
scanf("%d", &c[i]);
for (int i = 1;i <= n;i++)
scanf("%d", &k[i]);
int tot = 0;
for (int i = 1;i <= n;i++)
{
for (int j = i+1;j <= n;j++)
{
LL len = abs(x[i]-x[j])+abs(y[i]-y[j]);
edge[++tot].u = i;
edge[tot].v = j;
edge[tot].w = len*(k[i]+k[j]);
}
}
for (int i = 1;i <= n;i++)
{
edge[++tot].u = n+1;
edge[tot].v = i;
edge[tot].w = c[i];
}
sort(edge+1, edge+1+tot);
vector<pair<int, int> > Edg;
vector<int> Pow;
LL sum = 0;
for (int i = 1;i <= tot;i++)
{
int tu = GetFa(edge[i].u);
int tv = GetFa(edge[i].v);
if (tu == tv)
continue;
sum += edge[i].w;
Fa[tu] = tv;
if (edge[i].u == n+1)
Pow.push_back(edge[i].v);
else
Edg.emplace_back(edge[i].u, edge[i].v);
}
printf("%I64d\n", sum);
printf("%d\n", (int)Pow.size());
for (auto v: Pow)
printf("%d ", v);
puts("");
printf("%d\n", Edg.size());
for (auto p: Edg)
printf("%d %d\n", p.first, p.second);
return 0;
}
Codeforces Round #597 (Div. 2) D. 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 ...
- 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\) 号格子当 \( ...
- Codeforces Round #597 (Div. 2) C. Constanze's Machine
链接: https://codeforces.com/contest/1245/problem/C 题意: Constanze is the smartest girl in her village ...
- Codeforces Round #597 (Div. 2) B. Restricted RPS
链接: https://codeforces.com/contest/1245/problem/B 题意: Let n be a positive integer. Let a,b,c be nonn ...
- Codeforces Round #597 (Div. 2) A. Good ol' Numbers Coloring
链接: https://codeforces.com/contest/1245/problem/A 题意: Consider the set of all nonnegative integers: ...
- 计算a^b==a+b在(l,r)的对数Codeforces Round #597 (Div. 2)
题:https://codeforces.com/contest/1245/problem/F 分析:转化为:求区间内满足a&b==0的对数(解释见代码) ///求满足a&b==0在区 ...
- Codeforces Round #597 (Div. 2) F. Daniel and Spring Cleaning 数位dp
F. Daniel and Spring Cleaning While doing some spring cleaning, Daniel found an old calculator that ...
随机推荐
- java23种设计模式之十:责任链模式
最近在学习netty中发现其中用到了责任链模式,然后结合自己在写代码中遇到了大量写if...else的情况,决定学习一下责任链模式. 一.什么样的场景下会选择用责任链模式 我们在进行业务逻辑判断时,需 ...
- centos 6.10 oracle 19c安装
centos 7以下版本安装oracle 19c 问题较多,centos 以上版本没有任何问题.记录如下. hosts文件,否则图形界面无法启动 127.0.0.1 localhost localho ...
- PAT(B) 1034 有理数四则运算(Java)
题目链接:1034 有理数四则运算 (20 point(s)) 题目描述 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数 ...
- 『Python基础』第7节:基本运算符
一. 基本运算符 运算按种类可以分为: 算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 今天我们只学习算数运算.比较运算.逻辑运算.赋值运算.成员运算 1.1 算数运算 以下假设 ...
- 微信公众号h5页面自定义分享
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Sonya and Matrix Beauty CodeForces - 1080E (manacher)
大意: 给定$nm$字符串矩阵. 若一个子矩形每一行重排后可以满足每行每列都是回文, 那么它为好矩形. 求所有好矩形个数. 一个矩形合法等价于每一行出现次数为奇数的最多只有一个字符, 并且对称的两行对 ...
- 今日前端框架Vue学习笔记
在线网页网址http://xingxunxinxi.com/StudentCourse/first.html代码 界面
- 【转】Java基础——面试题汇总
1.JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java Runtime Env ...
- Java 之 字节输出流[OutputStream]
一.字节输出流 java.io.OutputStream 抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地. 该类中定义了字节输出流的基本共性功能方法. 公性方法: public v ...
- 【nodejs代理服务器二】nodejs webpack打包配置踩坑总结
接着上篇用Nodejs开发web代理,防止web渗透.如果部署到正式环境,需要进行打包配置. 我在用webpack打包配置中遇到了几个错误,总结如下: webpack环境变量问题 https://ww ...