[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. https://stackblitz.com/github/cwiki-us-angular/cwiki-us-angular-app 导入后如何添加到自己的项目

    将 https://stackblitz.com/github/cwiki-us-angular/cwiki-us-angular-app 导入到界面后,如何将这个项目添加到自己的项目里面. 然后再自 ...

  2. CF 680D 堆塔

    D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. PyCharm中Qt Designer+PyUIC配置

    本文环境配置: 系统=>windows10:64位 语言=>Python:3.7.1 第三方库PyQT5:5.11.3pyqt5-tools:5.11.3.1.4 工具=>PyCha ...

  4. Android_(游戏)打飞机01:前言

    (游戏)打飞机01:前言 传送门 (游戏)打飞机02:游戏背景滚动 传送门 (游戏)打飞机03:控制玩家飞机 传送门 (游戏)打飞机04:绘画敌机.添加子弹   传送门 (游戏)打飞机05:处理子弹, ...

  5. 套接字之select系统调用

    select是IO多路复用的一种方式,用来等待一个列表中的多个描述符的可读可写状态: SYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_ ...

  6. LeetCode 300. 最长上升子序列(Longest Increasing Subsequence)

    题目描述 给出一个无序的整形数组,找到最长上升子序列的长度. 例如, 给出 [10, 9, 2, 5, 3, 7, 101, 18], 最长的上升子序列是 [2, 3, 7, 101],因此它的长度是 ...

  7. 异步上传&预览图片-压缩图片

    移动端普及的时代,流量是用户最关心的,手机拍出来的照片基本上都在1~2M以上,这样上传会非常耗流量,影响用户体验,此例能在保证清晰度的情况下,将4.5M的图片压缩为30K <!DOCTYPE h ...

  8. Servlet基础总结

    1.Servlet概念: Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间 ...

  9. 4.数据挖掘的数据仓库与OLAP技术

    1.什么是数据仓库 面向主题的.集成的.时变的.非易失的 2.数据仓库和异种DBMS 3.OLTP vs OLAP 4.为什么建立分离的数据仓库? 5.多维数据模型(数据仓库的概念建模)三类度量 4. ...

  10. Xpath 和Css Selector使用

    Xpath是xml的路径语言,就是通过元素的路径来查找标签元素. Xpath直接在火狐浏览器的firebug中练习,49版本一下的火狐才能用firebug插件. Xpath的使用方法 注://*    ...