又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int N=;
const int INF=0x3f3f3f3f;
int fa[N],head[N],tot,T,n,m,d[N][N];
struct Edge{
int v,next,w;
}edge[N<<];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
struct Node{
int u,v,w;
bool mark;
bool operator<(const Node &rhs)const{
return w<rhs.w;
}
}p[N*N];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int s;
void dfs(int u,int f,int t){
d[s][u]=t;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(v==f)continue;
dfs(v,u,max(t,edge[i].w));
}
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
tot=;
memset(d,,sizeof(d));
for(int i=;i<=n;++i)fa[i]=i,head[i]=-;
for(int i=;i<=m;++i){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
p[i].mark=;
}
sort(p+,p++m);
int tmp=n;
int mst=,ans=INF;
for(int i=;i<=m;++i){
int u=find(p[i].u),v=find(p[i].v);
if(u!=v){
--tmp;
fa[u]=v;
mst+=p[i].w;
p[i].mark=;
add(p[i].u,p[i].v,p[i].w);
add(p[i].v,p[i].u,p[i].w);
if(tmp==)break;
}
}
for(int i=;i<=n;++i){
s=i;dfs(i,,);
}
for(int i=;i<=m;++i){
if(p[i].mark)continue;
ans=min(ans,mst-d[p[i].u][p[i].v]+p[i].w);
}
printf("%d %d\n",mst,ans);
}
return ;
}

UVA 10600 ACM Contest and Blackout 次小生成树的更多相关文章

  1. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树

    题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...

  2. uva 10600 ACM Contest And Blackout

    题意: 求最小生成树和次小生成树的总权值. 思路: 第一种做法,适用于规模较小的时候,prim算法进行的时候维护在树中两点之间路径中边的最大值,复杂度O(n^2),枚举边O(m),总复杂度O(n^2) ...

  3. UVA10600 ACM Contest and Blackout —— 次小生成树

    题目链接:https://vjudge.net/problem/UVA-10600 In order to prepare the “The First National ACM School Con ...

  4. UVA10600:ACM Contest and Blackout(次小生成树)

    ACM Contest and Blackout 题目链接:https://vjudge.net/problem/UVA-10600 Description: In order to prepare ...

  5. 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)

    [题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...

  6. 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)

    题意:有T组数据,N个点,M条边,每条边有一定的花费.问最小生成树和次小生成树的权值. 解法:具体请见 关于生成树的拓展 {附[转]最小瓶颈路与次小生成树}(图论--生成树) 1 #include&l ...

  7. UVA-10600 ACM Contest and Blackout (次小生成树)

    题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...

  8. UVA10600 ACM Contest and Blackout

    用prim算法求最小生成树和次小生成树~ #include<cstdio> #include<algorithm> #include<cstring> using ...

  9. kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数

    第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...

随机推荐

  1. 你所不知道的ref

    在c#中有个关键字叫ref,它的作用是使参数按引用传递,基本用法如下: class RefExample { static void Method(ref int i) { i = ; } stati ...

  2. winform保存登录cookie

       在web程序中,我们通常使会使用cookie来保存一些用户状态,或者权限或者你想保存的东西,但是在CS程序中,如果要使用cookie就必须要做些功课了... 最好注意以下几点:   1.使用成员 ...

  3. JavaScript里最有效率的功能特征检测方法

    代码执行效率对于程序员和程序来说都是至关重要的,尤其是遇到了那些需要大量调用.反复调用的函数方法.在很多Javascript框架里你都能看到有反复调用的函数.当在使用这些框架时,我们必须小心翼翼的尽量 ...

  4. BZOJ 3288 Mato矩阵 解题报告

    这个题好神呀..Orz taorunz 有一个结论,这个结论感觉很优美: $$ans = \prod_{i=1}^{n}\varphi(i)$$ 至于为什么呢,大概是这样子的: 对于每个数字 $x$, ...

  5. Flume学习——Flume中事务的定义

    首先要搞清楚的问题是:Flume中的事务用来干嘛? Flume中的事务用来保证消息的可靠传递. 当使用继承自BasicChannelSemantics的Channel时,Flume强制在操作Chann ...

  6. 【leetcode】4Sum(middle)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  7. [topcoder]AvoidRoads

    二维动态规划.和某一道leetcode的题目差不多.就是多了blocks的数组或集合. 本次解题的心得有:1.根据题意使用集合表示阻碍:2.使用字符串的形式表示整数的pair,简洁明了:3.p1到p2 ...

  8. 命令行添加用户的“作为服务登录”权利(添加Windows用户的时候,门道不是一般的多)good

    1.打开控制台(“开始”|“运行”中输入:MMC) 2.“文件”菜单|“添加删除管理单元”|“添加...”|选“安全模板”|“关闭”. 3.在“C:\Windows\Security\template ...

  9. Maven中聚合与继承

    何为继承? --›继承为了消除重复,我们把很多相同的配置提取出来 --›例如:grouptId,version等 就像写java程序一样,对于有共性切重复的东西,就提取出来. 如有三个pom.xml配 ...

  10. Spring Framework 4.1.1

    http://repo.spring.io/libs-release-local/org/springframework/spring/4.1.1.RELEASE/