链接:

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的更多相关文章

  1. 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 ...

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

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

  3. 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; //坐标 ...

  4. Codeforces Round #597 (Div. 2)

    A - Good ol' Numbers Coloring 题意:有无穷个格子,给定 \(a,b\) ,按以下规则染色: \(0\) 号格子白色:当 \(i\) 为正整数, \(i\) 号格子当 \( ...

  5. Codeforces Round #597 (Div. 2) C. Constanze's Machine

    链接: https://codeforces.com/contest/1245/problem/C 题意: Constanze is the smartest girl in her village ...

  6. 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 ...

  7. Codeforces Round #597 (Div. 2) A. Good ol' Numbers Coloring

    链接: https://codeforces.com/contest/1245/problem/A 题意: Consider the set of all nonnegative integers: ...

  8. 计算a^b==a+b在(l,r)的对数Codeforces Round #597 (Div. 2)

    题:https://codeforces.com/contest/1245/problem/F 分析:转化为:求区间内满足a&b==0的对数(解释见代码) ///求满足a&b==0在区 ...

  9. 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 ...

随机推荐

  1. [ClickOnce] - Win10 管理员模式下无法安装 ClickOnce 之解决

    Issue Windows 10 管理员模式下,点击 ClickOnce 安装程序无反应. 解决 1. 按 WIN+R 键打开“运行”窗口,输入 “gpedit.msc" 打开组策略.2.  ...

  2. 迅雷下载敏感资源 迅雷应版权方要求无法下载 μTorrent使用方法(六种方法,值得你看)(22)

    1. 解决方案1 1.1 声明 此方法只适用于迅雷极速版,迅雷X不管用. 修改后下载有些磁力链接或种子,依然无反应.不是说该方法无效,而是有些种子资源不佳,很难下载,需要等半天才能连接上开始下载.如果 ...

  3. php面向对象之封装

    OOP三大特性:封装.继承和多态,简称封继态. 封装 类2使用关键字extends继承类1,之后,类1为类2的父类,简称父类,类2是类1的子类,简称子类.使用关键字new,实例化类1,得到对象1,对象 ...

  4. python 之 数据库(字段的约束条件,表之间的关系)

    10.6 约束条件 10.61 not null .default create table t15( id int, name ) not null, sex enum('male','female ...

  5. Python学习路线2019升级版(课程大纲+视频教程+网盘资源下载)

    2019最新Python全栈+人工智能学习路线升级版 全面涵盖前端.后端.爬虫.数据挖掘.人工智能等课程(课程大纲+视频教程+网盘资源下载)! 学习路线四大亮点: 1.人工智能三大主流框架全覆盖 2. ...

  6. PB数据窗口分页

    第一步:增加一个计算列,此计算列必须放在Detail段,Expression中输入: ceiling(getrow()/500)  --这里500还可以用全局函数取代,这样可以允许用户任意设置每页多少 ...

  7. A Story of One Country (Hard) CodeForces - 1181E2 (分治)

    大意: 给定$n$个平面上互不相交的矩形. 若一个矩形区域只包含一个矩形或者它可以水平或垂直切成两块好的区域, 那么这个矩形区域是好的. 求判断整个平面区域是否是好的. 分治判断, 可以用链表实现删除 ...

  8. IO是否会一直占用CPU?(转)

    原文来自知乎:https://www.zhihu.com/question/27734728 这是一个很好的关于并发/并行系统的问题.简单回答就是:IO所需要的CPU资源非常少.大部分工作是分派给DM ...

  9. c#使用GDI进行简单的绘图

    https://www.2cto.com/database/201805/749421.html https://zhidao.baidu.com/question/107832895.html pr ...

  10. R_数据视觉化处理_初阶_02

    通过数据创建一幅简单的图像, #Crate a easy photopdf("mygraph.pdf") attach(mtcars) plot(wt,mpg) abline(lm ...