cf 【并查集】
http://codeforces.com/contest/1245/problem/D
题意就是:你需要让所有城市都有电,你看也在该城市建电站使他有电,同时你可以链接他与其他城市,使之有电
解决:
我们可以吧每个城市自己建电站以及自己与其他城市的费用用结构体存起来,排个序,再用并查集连起来。
#include<bits/stdc++.h>
#define numm ch-48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define debug(args...) cout<<#args<<"->"<<args<<endl
#define bug cout<<"************"
using namespace std;
template <typename T>
void read(T &res) {
bool flag=false;char ch;
while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
flag&&(res=-res);
}
template <typename T>
void write(T x) {
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
typedef long long ll;
typedef long double ld;
const int maxn=2000+10;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const double alpha=0.7;
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
struct node {
int u,v;
ll w;
node(){}
node(int u,int v,ll w):u(u),v(v),w(w){}
bool operator<(const node&a) {
return w<a.w;
}
}e[maxn*maxn+maxn];
struct a {
ll x,y;
int pos;
}a[maxn];
ll c[maxn],k[maxn];
int f[maxn];
vector<int >vec1;
vector<pii >vec2;
int getf(int v) {
return f[v]==v?v:f[v]=getf(f[v]);
}
int main()
{
int n;
read(n);
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=n;i++) {
read(a[i].x),read(a[i].y);
// a[i].pos=i;
}
for(int i=1;i<=n;i++)
read(c[i]);
for(int i=1;i<=n;i++)
read(k[i]);
int cnt=0;
for(int i=1;i<=n;i++){
e[++cnt]=node(0,i,c[i]);
for(int j=i+1;j<=n;j++)
e[++cnt]=node(i,j,(k[i]+k[j])*(abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y)));
}
sort(e+1,e+1+cnt);
int num=0;
ll sum=0;
for(int i=1;i<=cnt;i++){
int u=e[i].u,v=e[i].v;
int a=getf(u);
int b=getf(v);
if(a!=b){//父亲不同才需要计算,例如你三个点肯定三条边构架出个三角形,但实际上只需要其中两条边就行了,第三条肯定是会被认出有共同父亲的
f[b]=a;
if(u==0)
vec1.pb(v);
else {
vec2.pb(mp(u,v));
}
num++;//无论你是自己建基站还是连接,因为连接就是默认前面那个已经有电,都只加一个
sum+=e[i].w;
if(num==n) break;//保证了要n个城市都亮
}
}
write(sum);pn;
write(vec1.size());pn;
if(vec1.size()) {
for(int i=0;i<vec1.size();i++)
write(vec1[i]),pd;
pn;
}
write(vec2.size());pn;
if(vec2.size()) {
for(int i=0;i<vec2.size();i++)
write(vec2[i].fi),pd,write(vec2[i].se),pn;;
pn;
}
return 0;
}
cf 【并查集】的更多相关文章
- CF 115 A 【求树最大深度/DFS/并查集】
CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- CF 500 B. New Year Permutation 并查集
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...
- C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 && 并查集方法)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF # 296 C Glass Carving (并查集 或者 multiset)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- cf 之lis+贪心+思维+并查集
https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...
- 79: cf 444E 并查集+思维
$des$ 题面 $sol$ 把边从小到大排序,枚举每条边作为答案,然后把两个点合并,判断每条边是否可以作为答案时,$cnt_i$ 表示节点 $i$ 已经合并的 $x$ 之和$size_i$ 表示已经 ...
- CF 452E. Three strings(后缀数组+并查集)
传送门 解题思路 感觉这种题都是套路之类的??首先把三个串并成一个,中间插入一些奇怪的字符,然后跑遍\(SA\).考虑按照\(height\)分组计算,就是每个\(height\)只在最高位计算一次, ...
随机推荐
- 在IIS上部署 .Net Core 3.0 项目踩坑实录
在IIS上部署 .Net Core 3.0 项目的主要流程有: 安装并启用IIS 安装AspNetCoreModuleV2 添加.配置网站 设置应用程序池 通过VS发布 一.安装并启用IIS: 安装了 ...
- report framework
大标题 摘要: 关键字: 1 第一题:(爬虫) 1.1 问题分析及解题思路 本题需要爬取xxxx的数据,运用Python的Request爬虫框架 1.2 解题流程及代码 1.2.1定义爬虫类-构造函数 ...
- English:Root "tele"
Xx_Introduction tele mean "far" mean"faar" cognate word have tele\culture\tel\pa ...
- 发送RCS 消息摘录相关成功log
//11-25 16:48:09.612102 2175 2726 I BugleDataModel: PendingMessagesProcessor: process from InsertN ...
- 记,NSProxy需要实现哪些方法?
转注出:https://www.cnblogs.com/xiaobajiu/p/10799962.html 使用NSProxy做替身,代理,多继承,本质上都是用它来转发消息给真身. 观察头文件,NSP ...
- Keystone安装与配置
一.实验目的: 1.掌握OpenStack环境搭建的基础工作 2.掌握keystone的安装与配置方法 3.掌握keystone基础接口的调用方法 二.实验步骤: 1.利用最初创建的快照克隆两台Cen ...
- DynamicList
DynamicList设计要点——类模板 申请连续空间作为顺序存储空间 动态设置顺序存储空间的大小 保证重置顺序存储空间时的异常安全性 DynamicList设计要点——函数异常安全的概念 不泄露任何 ...
- 给OPi Zero Plus添加USB启动功能
为使OPi Zero Plus支持U盘启动,需要在板载的SPI Flash当中刷入uboot.在这个过程当中绕了很多弯路,特此记录 最终操作步骤见文末 网上的教程仅使用sudo modprobe sp ...
- Java题库——Chapter8 对象和类
1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...
- 解决NuGet下载太慢的问题
以下载CefSharp.Wpf v57.0.0版本为例: 1.打开NuGet官网:https://www.nuget.org/ 2.输入CefSharp.Wpf,点击查询,如下所示: 3.确认版本正确 ...