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 【并查集】的更多相关文章

  1. CF 115 A 【求树最大深度/DFS/并查集】

    CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...

  2. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  3. [CF#250 Div.2 D]The Child and Zoo(并查集)

    题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...

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

  5. C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 &amp;&amp; 并查集方法)

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. CF # 296 C Glass Carving (并查集 或者 multiset)

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. cf 之lis+贪心+思维+并查集

    https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...

  8. 79: cf 444E 并查集+思维

    $des$ 题面 $sol$ 把边从小到大排序,枚举每条边作为答案,然后把两个点合并,判断每条边是否可以作为答案时,$cnt_i$ 表示节点 $i$ 已经合并的 $x$ 之和$size_i$ 表示已经 ...

  9. CF 452E. Three strings(后缀数组+并查集)

    传送门 解题思路 感觉这种题都是套路之类的??首先把三个串并成一个,中间插入一些奇怪的字符,然后跑遍\(SA\).考虑按照\(height\)分组计算,就是每个\(height\)只在最高位计算一次, ...

随机推荐

  1. MySQL 两张表关联更新(用一个表的数据更新另一个表的数据)

    有两张表,info1, info2 . info1: info2: 现在,要用info2中的数据更新info1中对应的学生信息,sql语句如下: UPDATE info1 t1 JOIN info2 ...

  2. golang协程同步的几种方法

    目录 golang协程同步的几种方法 协程概念简要理解 为什么要做同步 协程的几种同步方法 Mutex channel WaitGroup golang协程同步的几种方法 本文简要介绍下go中协程的几 ...

  3. vim简单操作命令

    vim简单操作命令: 开启编辑:按“i”或者“Insert”键 退出编辑:“Esc”键 退出vim:“:q” 保存vim:“:w” 保存退出vim:“:wq” 不保存退出vim:“:q!” 查看当前系 ...

  4. screen工具的安装与使用

    yum install screen    安装screen screen -S <作业名称>     创建新的页 screen -ls   查询已经存在的页面 screen -r < ...

  5. tensorflow 神经网络模型概览;熟悉Eager 模式;

    典型神经网络模型:(图片来源:https://github.com/madalinabuzau/tensorflow-eager-tutorials) 保持更新,更多内容请关注 cnblogs.com ...

  6. Nginx:反向代理

    与众不同的生活方式很累人呢,因为找不到借口     在上一章节中,我们以及了解到正向.反向代理.负载均衡和动静分离的基本概念,安装教程,而在本节中将会了解到在 本文要点: 1.理清概念 2.Linux ...

  7. Cocos2d-x3.0网络通信学习(一)

    配置:win7+Cocos2d-x.3.0+VS2012 摘要:建立基本的http通信并得到返回信息. 一.添加项目与编译库 1.添加头文件 在需要用到Http网络相关类的文件中加入头文件 #incl ...

  8. IDEA快捷键用法

    1.Ctrl+滑动滚轮调节窗口显示大小(需要设置之后方可) File->Settings->Editor->General->Change fontsize with Ctrl ...

  9. THUWC2019酱油记

    Day -1 坐了一上午动车来到帝都,晚上去了趟THU还有奥林匹克公园. 反正也是来打酱油的,颓废怎么能少呢.

  10. Cypress安装使用(E2E测试框架)

    一.简介 Cypress是为现代网络打造的下一代前端测试工具,解决了开发人员和QA工程师在测试现代应用程序时面临的关键难点问题. Cypress包含免费的.开源的.可本地安装的Test Runner  ...