[Codeforces 1245D] Shichikuji and Power Grid (最小生成树)

题面

有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个城市建立发电站需要费用\(c_i\).如果不建立发电站,要让城市通电,就需要与有发电站的城市连通。i与j之间连一条无向的边的费用是\((k_i+k_j)\)*两个城市之间的曼哈顿距离.求让每个城市都通电的最小费用,并输出任意一个方案。

分析

把选每个点的代价转成虚拟原点到这个点的边,这个套路很常见,但在最小生成树题里还是第一次见到。

城市之间两两连边,边权按题目里提到的计算。然后建立一个虚拟源点,向每个城市\(i\)连边,边权为\(c_i\).对整个图跑一个最小生成树即可.

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 2000
using namespace std;
typedef long long ll;
int n;
struct node{
int x;
int y;
int c;
int k;
}a[maxn+5];
inline ll dist(ll x1,ll y1,ll x2,ll y2){
return abs(x1-x2)+abs(y1-y2);
} struct edge{
int from;
int to;
ll len;
int next;
}E[maxn*maxn+5];
int head[maxn+5];
int sz=0;
void add_edge(int u,int v,ll w){
sz++;
E[sz].from=u;
E[sz].to=v;
E[sz].len=w;
E[sz].next=head[u];
head[u]=sz;
}
bool cmp(edge p,edge q){
return p.len<q.len;
} int fa[maxn+5];
int find(int x){
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
} vector< pair<int,int> >res;
ll kruskal(){
ll ans=0;
for(int i=1;i<=n+1;i++) fa[i]=i;
sort(E+1,E+1+sz,cmp);
for(int i=1;i<=sz;i++){
int u=E[i].from,v=E[i].to;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=E[i].len;
fa[fu]=fv;
res.push_back(make_pair(E[i].from,E[i].to));
}
}
return ans;
} vector<int>poi;//poi~
vector< pair<int,int> >edg;
int main(){
int st;
scanf("%d",&n);
st=n+1;
for(int i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
for(int i=1;i<=n;i++){
scanf("%d",&a[i].c);
add_edge(st,i,a[i].c);
}
for(int i=1;i<=n;i++) scanf("%d",&a[i].k);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
add_edge(i,j,(a[i].k+a[j].k)*dist(a[i].x,a[i].y,a[j].x,a[j].y));
}
}
printf("%I64d\n",kruskal());
for(auto ed : res){
if(ed.first==st) poi.push_back(ed.second);
else edg.push_back(ed);
}
printf("%d\n",poi.size());
for(int x : poi) printf("%d ",x);
printf("\n");
printf("%d\n",edg.size());
for(auto x : edg) printf("%d %d\n",x.first,x.second);
}

[Codeforces 1245D] Shichikuji and Power Grid (最小生成树)的更多相关文章

  1. CodeForces 1245D Shichikuji and Power Grid

    cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...

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

  3. Shichikuji and Power Grid

    D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...

  4. CF1245D: Shichikuji and Power Grid

    CF1245D: Shichikuji and Power Grid 题意描述: 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点 ...

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

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

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

  7. Codeforces 1245 D. Shichikuji and Power Grid

    传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...

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

  9. [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

    [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...

随机推荐

  1. HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )

    题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867 ...

  2. 【杂题】[AGC034D] Manhattan Max Matching【费用流】

    Description 有一个无限大的平面,有2N个位置上面有若干个球(可能重复),其中N个位置是红球,N个位置是蓝球,红球与蓝球的总数均为S. 给出2N个位置和上面的球数,现要将红球与蓝球完美匹配, ...

  3. Springboot 默认静态路径

    springboot 默认静态路径 代码如下所示 类ResourceProperties.class private static final String[] CLASSPATH_RESOURCE_ ...

  4. Python中的不可变对象类型与可变对象类型

    https://blog.csdn.net/answer3lin/article/details/86430074 其实各个标准资料中没有说明Python有值类型和引用类型的分类,这个分类一般是C++ ...

  5. Unity3D_(游戏)卡牌02_主菜单界面

      启动屏界面.主菜单界面.选关界面.游戏界面 卡牌01_启动屏界面 传送门 卡牌02_主菜单界面 传送门 卡牌03_选关界面 传送门 卡牌04_游戏界面    传送门 主菜单界面 (选择左边图标或选 ...

  6. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

  7. SpringBoot 单文件和多文件上传

    单.多文件上传:单文件上传使用upload.html ,多文件上传使用uploads.html 创建一个Springboot application, POM 中加入 spring-boot-star ...

  8. Java连接MQTT服务-ws方式

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  9. python3笔记四:if语句

    一:学习内容 if语句 if-else语句 if-elif-else语句 if语句练习 二:if语句 1. 格式 if 表达式:    语句 2.逻辑 当程序执行到if语句时,首先计算表达式的值如果表 ...

  10. 代码实现:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据 (包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

    import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.ut ...