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. Shell命令的执行优先级

    Shell内置命令.外部命令.别名.函数.保留关键字的优先级 在Shell中,有5种可调用的东西:别名(alias).函数(function).shell保留关键字.shell内置命令.外部命令. 如 ...

  2. [转]UiPath Studio Community如何连接orchestrator (Level2讲解)

    本文转自:https://mp.weixin.qq.com/s/_IWsUB94nT0QwQ6t1IK0Dg https://blog.csdn.net/weixin_45000314/article ...

  3. UILable中划线和下划线

    //中划线 NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NS ...

  4. 日常工作中VBA代码积累

    1.超链接地址提取 Function GetURL(rng As Range) As String On Error Resume Next GetURL = rng.Hyperlinks(1).Ad ...

  5. sql server2017开启远程连接

    1.安装完SQL server2017之后,选择SQL 身份验证登录,可以先用windows身份验证登录把密码更改好了,然后服务器右键重新启动 ,再断开连接 ,选择SQL身份验证登录验证,关闭SQL ...

  6. MySQL 学习笔记 (一)

    1.InnoDB and Online DDL ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE; ...

  7. linux-认识vi vim

    vi 编译器 Linux vi 命令非常强大,熟练地使用它可以高效的编辑代码,配置系统文件等 命令:vi [文件] vim [文件] vi 分为三种模式:命令模式.文字模式.末尾模式 -------- ...

  8. Eureka集群

    Eureka集群搭建 高可用集群配置 当注册中心扛不住高并发的时候,这时候 要用集群来扛: 普通操作 我们再新建两个module  microservice-eureka-server-2002  m ...

  9. 【bzoj5339】[TJOI2018]教科书般的亵渎(拉格朗日插值/第二类斯特林数)

    传送门 题意: 一开始有很多怪兽,每个怪兽的血量在\(1\)到\(n\)之间且各不相同,\(n\leq 10^{13}\). 然后有\(m\)种没有出现的血量,\(m\leq 50\). 现在有个人可 ...

  10. spanish-1.1

    1.1. ¿Cómo te llamas?Ana : ¿Cómo te llamas?Paco: Buenos dias. Yo soy Paco. Y tú, ¿cómo te llemas?Ana ...