链接http://acm.tju.edu.cn/toj/showp4117.html
4117.   Happy tree friends


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 164   Accepted Runs: 60

yuebai has an undirected complete graph with n vertices. He wants to know the minimum spanning tree of the graph. It's so easy, so yuebai wants to challenge himself. He will choose one edge which must be
in the spanning tree.

INPUT

There are multiple test cases.
For each test case, the first line contain an integer n.
In the next n lines,
there is an adjacency matrix M. Mij denotes
the weight of the edge i to j.
Next line contains two dinstinct integer u and v,
which denotes the edge which is from u to v with
the value Muv must
be in the spanning tree.
(2≤n≤100,0≤Mij≤100). Mij=0 if
and only if i=j.

OUTPUT

For each case, print the result.

Sample Input


3
0 2 3
1 0 4
5 10 0
2 3

Sample Output


5

Hint

The edge of the spanning tree is 2->3 and 2->1

Source: TJU
Team Selection 2015 Round B

用Kruskal做

只要把题目中要求的边先合和起来,其余按照模版来,题目说了无向,所以对于每边的长度取矩阵中的最小值(真是坑,一开始以为是最小树形图)

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; #define N 50005
int a[105][105];
struct graph{
int x,y,wei;
}nodd[N];
int m,n,ufind[N]; int cmp(graph a1,graph a2){
return a1.wei<a2.wei;
}
int find(int x){
return ufind[x]==x? x : ufind[x]=find(ufind[x]);
}
int Kruskal(int a,int b){
int ans=0;
int i,j;
for(i=1;i<=n;i++) ufind[i]=i;
sort(nodd,nodd+m,cmp);
ufind[a]=b;
for(i=0;i<m;i++){
int x=find(nodd[i].x); int y=find(nodd[i].y);
if(x!=y){
ans+=nodd[i].wei;
ufind[x]=y;
}
}
return ans;
} int main(){
int x;
int sum;
int temp;
int a1,a2;
int i,j,k;
while(scanf("%d",&x)!=EOF){
sum=0;
temp=0;
for(i=1;i<=x;i++)
for(j=1;j<=x;j++)
scanf("%d",&a[i][j]);
m=x*(x-1)/2;
n=x;
scanf("%d %d",&a1,&a2);
sum+=a[a1][a2];
a[a2][a1]=a[a1][a2];
for(i=1;i<=x;i++)
for(j=i+1;j<=x;j++){
nodd[temp].x=i; nodd[temp].y=j; nodd[temp].wei=min(a[i][j],a[j][i]);
temp++;
}
//for(i=0;i<temp;i++) printf("%d %d %d\n",nodd[i].x,nodd[i].y,nodd[i].wei);
printf("%d\n",Kruskal(a1,a2)+sum);
}
}

最小生成树 TOJ 4117 Happy tree friends的更多相关文章

  1. 说说最小生成树(Minimum Spanning Tree)

    minimum spanning tree(MST) 最小生成树是连通无向带权图的一个子图,要求 能够连接图中的所有顶点.无环.路径的权重和为所有路径中最小的. graph-cut 对图的一个切割或者 ...

  2. [bzoj1016][JSOI2008]最小生成树计数 (Kruskal + Matrix Tree 定理)

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  3. 二分+最小生成树【bzoj2654】: tree

    2654: tree 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 二分答案,然后跑最小生成树判断. 注意优先跑白色边. code: ...

  4. prim算法查找最小生成树

    我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...

  5. "《算法导论》之‘图’":最小生成树(无向图)

    本文主要参考自<算法>. 加权图是一种为每条边关联一个权值或是成本的图模型.这种图能够自然地表示许多应用.在一幅航空图中,边表示航线,权值则可以表示距离或是费用.在一幅电路图中,边表示导线 ...

  6. 最小生成树之Kruskal(克鲁斯卡尔)算法

    学习最小生成树算法之前我们先来了解下下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的所 ...

  7. 最小生成树 Prim算法 和 Kruskal算法,c++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  8. 最小生成树之克鲁斯卡尔(Kruskal)算法

    学习最小生成树算法之前我们先来了解下 下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的 ...

  9. 由最小生成树(MST)到并查集(UF)

    背景 最小生成树(Minimum Spanning Tree)的算法中,克鲁斯卡尔算法(Kruskal's algorithm)是一种常用算法. 在克鲁斯卡尔算法中的一个关键问题是如何判断图中的两个点 ...

随机推荐

  1. CF341C. Iahub and Permutations [DP 排列]

    http://codeforces.com/contest/341/problem/C 题意: 有一个长度为n的排列a,其中有一些位置被替换成了-1.你需要尝试恢复这个排列,将-1替换回数字.求有多少 ...

  2. 计算机基础之Windows10操作系统安装U盘制作

    1.第一步,下载Windows10--ISO镜像(Windows7类似),下载站点: https://msdn.itellyou.cn/(百度搜索msdn即可),个人认为这是最干净的操作系统镜像站点, ...

  3. python基础(str,list,tuple)

    python是一门动态解释型的强类型定义语言(先编译后解释) 动态类型语言 动态类型的语言编程时,永远也不用给任何变量指定数据类型,该语言会在你第一次赋值给变量时,在内部将数据类型记录下来 解释型 程 ...

  4. php.ini 中文详解

    [PHP]  ; PHP还是一个不断发展的工具,其功能还在不断地删减  ; 而php.ini的设置更改可以反映出相当的变化,  ; 在使用新的PHP版本前,研究一下php.ini会有好处的   ;;; ...

  5. LeetCode - 620. Not Boring Movies

    X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a ...

  6. TinyMce 使用初探

    https://www.cnblogs.com/nkxyf/p/3883586.html 参考:http://www.tinymce.com/    官网 http://www.tinymce.com ...

  7. 项目构建工具Maven

  8. ajax上传图片chrome报错net::ERR_CONNECTION_RESET/net::ERR_CONNECTION_ABORTED

    网上搜了一下,base64图片太大,tomcat对post请求大小有默认限制,要在tomcat配置文件server.xml 加一个:maxPostSize="0",0表示无限制 & ...

  9. aforge 学习-基本图像处理要用的类库

    1.图像灰度化:Grayscale.CommonAlgorithms.BT709(3种) FiltersSequence =new  FiltersSequence(数组处理函数): 2.二值化(阈值 ...

  10. hdu 1010 回溯加奇偶性剪枝

    普通的剪枝会超时,必须加入奇偶性剪枝. 直接上图: AC代码: #include<cstdio> #include<cstring> #include<algorithm ...