【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
【题意】
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(最小生成树和次小生成树)的更多相关文章
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- uva 10600 ACM Contest And Blackout
题意: 求最小生成树和次小生成树的总权值. 思路: 第一种做法,适用于规模较小的时候,prim算法进行的时候维护在树中两点之间路径中边的最大值,复杂度O(n^2),枚举边O(m),总复杂度O(n^2) ...
- 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)
题意:有T组数据,N个点,M条边,每条边有一定的花费.问最小生成树和次小生成树的权值. 解法:具体请见 关于生成树的拓展 {附[转]最小瓶颈路与次小生成树}(图论--生成树) 1 #include&l ...
- UVA 10600 ACM Contest and Blackout 次小生成树
又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...
- UVA10600:ACM Contest and Blackout(次小生成树)
ACM Contest and Blackout 题目链接:https://vjudge.net/problem/UVA-10600 Description: In order to prepare ...
- UVA10600 ACM Contest and Blackout —— 次小生成树
题目链接:https://vjudge.net/problem/UVA-10600 In order to prepare the “The First National ACM School Con ...
- UVA-10600 ACM Contest and Blackout (次小生成树)
题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...
- UVA10600 ACM Contest and Blackout
用prim算法求最小生成树和次小生成树~ #include<cstdio> #include<algorithm> #include<cstring> using ...
- URAL 1416 Confidential --最小生成树与次小生成树
题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...
随机推荐
- RPC框架之Thrift
目前流行的服务调用方式有很多种,例如基于SOAP消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其中所用到的数据传输方式包括 XML,JSON 等,然而 XML ...
- 20151205 jquery 学习笔记--Ajax
Ajax全称为:“Asynchronous JavaScript and XML”(异步 JavaScript 和 XML), 它并不是 JavaScript 的一种单一技术,而是利用了一系列交互式网 ...
- Js5中基本类型
分别是: Number,//数值 string,字符串 Boolean,//布尔 null,null类型 undefined//未定义 标准库提供了对 布尔 ,数值,字符串三种的构造函数封装 aler ...
- JS中==和===的区别
1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...
- C#获取时间戳的问题
最近在做一个接口,需要用到时间戳,在请求接口时,返回超时,接口方的技术称是时间戳的不对(超出一定范围[比如1分钟]就返回超时)导致的. 首先,看代码: public static double Get ...
- 01_根据Id查询User的数据
[工程目录] [数据库表中内容 user表] [sqlMapConfig.xml配置文件主要内容] 简述:sqlMapConfig.xml配置文件主要有两个作用: 1.配置和数据连接的相关信息,例如事 ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- python相关博客
入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...
- zlib1.2.8 编译小记
官网下载:http://www.zlib.net/ 用vs命令行工具运行zlib-1.2.8\contrib\masmx86\bld_ml32.bat 用vs2012打开zlib-1.2.8\cont ...
- 使用JS实现鼠标滚轮事件
网站需要实现鼠标滚轮滚一下,页面向下滑向下一个锚点,由于前面有个一样式必须用jQuery1.3.2,而好多滚轮事件都使用了更高版本的jQuery,于是就从网上找了找 <script type=& ...