SB题,求最小生成树,其中有些边已经给您建好啦。

随意暴力即可。

 #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int rank[],fa[],n,m,a[][],q,x,y,f1,f2,ans;
void init(){for(int i=;i<=n;i++) fa[i]=i;}
int findroot(int x)
{
if(fa[x]==x) return x;
int rt=findroot(fa[x]);
fa[x]=rt;
return rt;
}
void Union(int U,int V)
{
if(rank[U]<rank[V]) fa[U]=V;
else
{
fa[V]=U;
if(rank[U]==rank[V]) rank[U]++;
}
}
struct Edge{int u,v,w;Edge(const int &a,const int &b,const int &c){u=a;v=b;w=c;}Edge(){}};
bool cmp(const Edge &a,const Edge &b){return a.w<b.w;}
Edge edges[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
edges[++m]=Edge(i,j,a[i][j]);
sort(edges+,edges+m+,cmp);
scanf("%d",&q);
init();
for(int i=;i<=q;i++)
{
scanf("%d%d",&x,&y);
f1=findroot(x); f2=findroot(y);
if(f1!=f2) Union(f1,f2);
}
for(int i=;i<=m;i++)
{
f1=findroot(edges[i].u); f2=findroot(edges[i].v);
if(f1!=f2)
{
Union(f1,f2);
ans+=edges[i].w;
}
}
printf("%d\n",ans);
return ;
}

【kruscal】【最小生成树】poj2421 Constructing Roads的更多相关文章

  1. HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏

    Constructing Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  2. POJ2421 Constructing Roads

    Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...

  3. POJ2421 Constructing Roads【最小生成树】

    题意: 有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少. 思路: 最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树. 代码: prime: #inc ...

  4. POJ2421 Constructing Roads 最小生成树

    修路 时限: 2000MS   内存限制: 65536K 提交总数: 31810   接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...

  5. 最小生成树 D - Constructing Roads

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  6. (最小生成树)Constructing Roads -- poj -- 2421

    链接: http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2113 ...

  7. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  8. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  9. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

随机推荐

  1. POJ3259:Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 68097   Accepted: 25374 题目链接: ...

  2. Java多线程调试如何完成信息输出处理

    转载自:http://developer.51cto.com/art/201003/189078.htm Java多线程调试是很繁琐的,但是还是需要我们不断进行相关的学习.下面我们就来看看在Java多 ...

  3. tomcat编码配置

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" ...

  4. struts2和jstl有关循环的写法

    一:前言 其实觉得自己现在就是个码农啊,对于struts2的标签和jstl的标签我一直都是只会用,但是觉得自己老是会混淆这种概念性的问题.所以我自己在代码里面就试着用了几种方式,实现同一种效果,下面就 ...

  5. 【BZOJ3675】【APIO2014】序列分割 [斜率优化DP]

    序列分割 Time Limit: 40 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 小H最近迷上了一个分隔序列的游戏. ...

  6. CentOS 7 主机加固手册-中

      CentOS 7 主机加固手册-上 CentOS 7 主机加固手册-中 CentOS 7 主机加固手册-下 0x0c 设置/boot/grub2/grub.cfg权限 Set grub.conf ...

  7. CSS3 渐变(Gradients)

    参考: http://www.runoob.com/css3/css3-gradients.html CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必 ...

  8. linux基础的基础命令操作

    一.开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 操作:su - root 二.使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 操作:ctrl ...

  9. syntax error near unexpected token `then'问题的解决

    http://blog.csdn.net/gongmin856/article/details/7690917 #!/bin/bash #if program test echo 'a:' read ...

  10. Atos cannot get symbols from dSYM of archived application

    http://stackoverflow.com/questions/7675863/atos-cannot-get-symbols-from-dsym-of-archived-application ...