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. noip 2011观光公交

    P1315 观光公交 95通过 244提交 题目提供者该用户不存在 标签贪心递推2011NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录   题目描述 风景迷人的小城Y 市,拥有n 个美 ...

  2. codeforces 1060 A

    https://codeforces.com/contest/1060/problem/A 题意:电话号码是以8开头的11位数,给你n 个数问最多可以有多少个电话号码 题解:min(8的个数,n/11 ...

  3. 7月21号day13总结

    今天学习过程和小结 学习了hive中的数据类型以及hive的简单查询, 学习了sqoop version用sqoop导入导出数据. 主要用于在Hadoop(Hive)与传统的数据库(mysql.pos ...

  4. 程序员的那些问题---转载自veryCD

    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告   走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过 ...

  5. 【bzoj1911-[Apio2010]特别行动队】斜率优化

    [题目描述] 有n个数,分成连续的若干段,每段的分数为a*x^2+b*x+c(a,b,c是给出的常数),其中x为该段的各个数的和.求如何分才能使得各个段的分数的总和最大. [输入格式]  第1行:1个 ...

  6. 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)

    Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...

  7. java中 快捷键输入System.out.println();

    syso 然后:alt+ /(就是问号键)

  8. 该配置节不能包含 CDATA 或文本元素

    当执行程序时报“该配置节不能包含 CDATA 或文本元素” ,有可能是你的web.config中有异常的文本节点,比如

  9. StringUtils.isEmpty()和StringUtils.isBlank() 区别

    isBlank()判断空的情况包括了isEmpty()的情况,isBlank()不仅判断了 无对象.空对象的情况,而且也判断了无意义的空白字符,比如空格等.

  10. [ 总结 ] 删除通过find查找到的文件

    [root@cloud abc]# touch test{,,,} [root@cloud abc]# ls shadow test test1 test2 test3 test5 [root@clo ...