【题意】

  n个点,m条边,求最小生成树的值和次小生成树的值。

Input
The Input starts with the number of test cases, T (1 < T < 15) on a line. Then T test cases follow. The
first line of every test case contains two numbers, which are separated by a space, N (3 < N < 100)
the number of schools in the city, and M the number of possible connections among them. Next M
lines contain three numbers Ai
, Bi
, Ci
, where Ci
is the cost of the connection (1 < Ci < 300) between
schools Ai and Bi
. The schools are numbered with integers in the range 1 to N.
Output
For every test case print only one line of output. This line should contain two numbers separated by a
single space – the cost of two the cheapest connection plans. Let S1 be the cheapest cost and S2 the
next cheapest cost. It’s important, that S1 = S2 if and only if there are two cheapest plans, otherwise
S1 < S2. You can assume that it is always possible to find the costs S1 and S2.
Sample Input
2
5 8
1 3 75
3 4 51
2 4 19
3 2 95
2 5 42
5 4 31
1 2 9
3 5 66
9 14
1 2 4
1 8 8
2 8 11
3 2 8
8 9 7
8 7 1
7 9 6
9 3 2
3 4 7
3 6 4
7 6 2
4 6 14
4 5 9
5 6 10
Sample Output
110 121
37 37

【分析】

  主要就是次小生成树咯。

  次小生成树一定是最小生成树删一边再加一条边。

  先求出最小生成树,然后n^2预处理两点的最小瓶颈路的值maxcost,然后枚举新加入的那条边然后替换边就好了。

  啊啊啊啊啊,忘了清bool哭瞎

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 110
#define INF 0xfffffff struct node
{
int x,y,c,next;
bool p;
}tt[Maxn*Maxn],t[Maxn];
int len;
int fa[Maxn],first[Maxn]; bool cmp(node x,node y) {return x.c<y.c;}
int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} int ffa(int x)
{
if(fa[x]!=x) fa[x]=ffa(fa[x]);
return fa[x];
} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int n,m,mc[Maxn][Maxn];
bool np[Maxn]; void dfs(int x,int f,int l)
{
for(int i=;i<=n;i++) if(np[i])
mc[x][i]=mc[i][x]=mymax(mc[f][i],l);
np[x]=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
dfs(t[i].y,x,t[i].c);
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&tt[i].x,&tt[i].y,&tt[i].c);
tt[i].p=;
}
sort(tt+,tt++m,cmp);
for(int i=;i<=n;i++) fa[i]=i;
int cnt=,a1=,a2=INF;
len=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++)
{
if(ffa(tt[i].x)!=ffa(tt[i].y))
{
fa[ffa(tt[i].x)]=ffa(tt[i].y);
tt[i].p=;
cnt++;
ins(tt[i].x,tt[i].y,tt[i].c);
ins(tt[i].y,tt[i].x,tt[i].c);
a1+=tt[i].c;
}
if(cnt==n-) break;
}
memset(mc,,sizeof(mc));
memset(np,,sizeof(np));
dfs(,,);
for(int i=;i<=m;i++) if(!tt[i].p&&tt[i].x!=tt[i].y)
{
a2=mymin(a2,a1-mc[tt[i].x][tt[i].y]+tt[i].c);
}
printf("%d %d\n",a1,a2);
}
return ;
}

2016-11-01 20:51:55

【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. 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)

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

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

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

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

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

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

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

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

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

  8. UVA10600 ACM Contest and Blackout

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

  9. URAL 1416 Confidential --最小生成树与次小生成树

    题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...

随机推荐

  1. JVM笔记5:Class文件结构

    Class文件是一组以8位字节为基础单位的二进制流,包含多个数据项目(数据项目的顺序,占用的字节数均由规范定义),各个数据项目严格按照顺序紧凑的排列在Class文件中,不包含任何分隔符,使得整个Cla ...

  2. Java并发——ReentrantLock类源码阅读

    ReentrantLock内部由Sync类实例实现. Sync类定义于ReentrantLock内部. Sync继承于AbstractQueuedSynchronizer. AbstractQueue ...

  3. commons-fileupload源码学习心得

    commons-fileupload依赖于commons-io包. commons-fileupload的使用方法: 1.创建一个文件项目工厂类DiskFileItemFactory.       D ...

  4. commons-io源码阅读心得

    FileCleanTracker: 开启一个守护线程在后台默默的删除文件. /* * Licensed to the Apache Software Foundation (ASF) under on ...

  5. 11.13 noip模拟试题

    题目名称 笔记 括号 城堡可执行文件名 note brackets castle输入文件名 note.in brackets.in castle.in输出文件名 note.in brackets.ou ...

  6. asp.net Ajax Post 请求一般处理程序

    其实很早就开通博客园了,一直想写些有价值的东西,供自己以后查阅的同时,也可以帮助别人遇到此类问题时能有一个好的解决方法.但是由于各种原因, 就没有实施我的想法.今天突然很想写下一篇文章,不知道我的第一 ...

  7. 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

  8. ios6-7以后用户开热点后的屏幕适配

    // 排版时,注意logical coordinate space和device coordinate space的区别,注意frame和bounds的区别! - (void)loadView { / ...

  9. initialize 和init

    initialize 是类方法,创建实例时会调用该方法.但是只会调用一次.如一个类创建了10个对象,initialize方法只会调用一次,但是init会调用10次.init 是实例方法,每次创建一个实 ...

  10. swift-02代码流程的控制

    // //  main.swift //  02-语句 // //  Created by wanghy on 15/8/9. //  Copyright (c) 2015年 wanghy. All ...