【kruscal】【最小生成树】poj2421 Constructing Roads
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的更多相关文章
- 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) ...
- POJ2421 Constructing Roads
Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...
- POJ2421 Constructing Roads【最小生成树】
题意: 有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少. 思路: 最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树. 代码: prime: #inc ...
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
- 最小生成树 D - Constructing Roads
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- (最小生成树)Constructing Roads -- poj -- 2421
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2113 ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
随机推荐
- [bzoj 1143]最长反链二分图最大匹配
Dilworth定理:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. 证明:http://www.cnblogs.com/itlqs/p/6636222.html 题目让求的是最大反链的 ...
- shell编程 if 注意事项
read -n 1 -p "Let's go(y or n):" if [ "$REPLY"x = "y"x -o "$REPLY ...
- Spring 对象的声明与注入
1.怎么把一个对象该过过交给Spring管理? 1)通过xml中配置<bean>节点来声明 2)通过Spring注解来声明,如:@Service.@Repository.@Componen ...
- usaco 2000 contest 滑雪
2013-09-11 10:22 [题目大意]给定N个点的高度和M条相连的路线(单向),从最高点向下走, 到无法走时为一条路径,求不同的路径数,(一节点不同就叫不同) [输入样例] 4 5 (N, ...
- Linux curl命令【curl】
命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具 ...
- 以root启动pycharm
在使用scapy模块的时候提示permitted就猜想可能是权限问题.然后换成root启动啥事情都没了. 由于本机是deepin先找到pycharm.sh脚本 然后再执行sudo ./pycharm. ...
- 大型网站调试工具之一(php性能优化分析工具XDebug)
一.安装配置 1.下载PHP的XDebug扩展,网址:http://xdebug.org/ 2.在Linux下编译安装XDebug 引用 tar -xzf xdebug-2.0.0RC3.gzcd x ...
- linux sun/awt/X11GraphicsEnvironment 找不到错误
转载自:http://blog.csdn.net/bolg_hero/article/details/46594915 在Linux下,我们在用Java的图片包来处理图片时,经常遇到java.lang ...
- opencv-写入AVI视频文件
#include <cv.h> #include <highgui.h> int main(int argc, char **argv) { CvCapture* captur ...
- pytest学习(1)
pytest是python的一个测试框架,主要是用来进行一些小的测试. 在pycharm中,缺省用的是unittest,这里说明如何设置为pytest. 当然,第一部是安装pytest pip3 in ...