模板题

#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<<];
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()
{
int cas=;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
tot=;
printf("Case #%d : ",++cas);
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;
}
}
if(tmp>){
printf("No way\n");
continue;
}
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);
}
if(ans==INF)printf("No second way\n");
else printf("%d\n",ans);
}
return ;
}

UVA 10462 Is There A Second Way Left? 次小生成树的更多相关文章

  1. UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)

    题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...

  2. UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树

    UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...

  3. UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解

    思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...

  4. UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】

    Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently a ...

  5. UVA - 10462 Is There A Second Way Left?

    题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...

  6. UVA 10600 ACM Contest and Blackout 次小生成树

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

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

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

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

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

  9. Qin Shi Huang's National Road System UVA - 1494(次小生成树)

    秦始皇统一中国之后要在全国修公路连接各个城市,皇帝只想修成最小生成树(距离最小,不考虑人力),一个道士说自己可以不花人力物力修一条路,经过两方妥协,选择max(两个城市人口/(生成树长度-这条路的长度 ...

随机推荐

  1. 【学习总结】 init & initWithFrame & initWithCoder

    //当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame { if (se ...

  2. Lua基础之table详解

    概要:1.table特性:2.table的构造:3.table常用函数:4.table遍历:5.table面向对象 原文地址:http://blog.csdn.net/dingkun520wy/art ...

  3. dnf脚本的研究

    [player number] 2 8  [pvp start area]0 0 0 00 0 0 00 0 0 0[dungeon]62[/dungeon][type] `[normal]`[gre ...

  4. MySQL的基本命令

    MySQL的基本命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...

  5. 5.1:FactoryBean的使用

    5.1  FactoryBean的使用 一般情况下,Spring通过反射机制利用bean的class属性指定实现类来实例化bean .在某些情况下,实例化bean过程比较复杂,如果按照传统的方式,则需 ...

  6. Scroll文字滚动js

    function ScrollImgLeft(){ var speed=50, doc=document, scroll_begin = doc.getElementById("scroll ...

  7. 对jQuery.extend()方法的分析

    jQuery.extend方法是我们常用的方法,也是jQuery源码中的基础方法.它的主要作用是:将一个或多个“源对象”合并到一个“目标对象”中,并返回目标对象.它主要有三种表现形式: a.jQuer ...

  8. jquery类选择器无法取得对象问题原因

    <html> <script type="text/javascript" src="jquery-1.9.1.js"></scr ...

  9. Connect to the mysql dataase from remote server

    Make sure that the firewall is closed!!!!!!!!! shell command should be like is: mysql -u username -p ...

  10. c缺陷与陷阱笔记-第七章 可移植性代码

    1.移位运算符 如果被移位的对象长度是n位,那么移位计数必须>=0,并且<n,例如对于1个32位的数,移位运算n<<31和n<<0是OK的,n<<32和 ...