UVA10600 ACM Contest and Blackout —— 次小生成树
题目链接:https://vjudge.net/problem/UVA-10600
In order to prepare the “The First National ACM School Contest” (in 20??) the major of the city decided to provide all the schools with a reliable source of power. (The major is really afraid of blackoutsJ). So, in order to do that, power station “Future” and one school (doesn’t matter which one) must be connected; in addition, some schools must be connected as well. You may assume that a school has a reliable source of power if it’s connected directly to “Future”, or to any other school that has a reliable source of power. You are given the cost of connection between some schools. The major has decided to pick out two the cheapest connection plans – the cost of the connection is equal to the sum of the connections between the schools. Your task is to help the major — find the cost of the two cheapest connection plans.
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
题解:
赤裸裸的求最小生成树和次小生成树。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e2+; int cost[MAXN][MAXN], lowc[MAXN], pre[MAXN], Max[MAXN][MAXN];
bool vis[MAXN], used[MAXN][MAXN]; int Prim(int st, int n)
{
int ret = ;
memset(vis, false, sizeof(vis));
memset(used, false, sizeof(used));
memset(Max, , sizeof(Max)); for(int i = ; i<=n; i++)
lowc[i] = (i==st)?:INF;
pre[st] = st; for(int i = ; i<=n; i++)
{
int k, minn = INF;
for(int j = ; j<=n; j++)
if(!vis[j] && minn>lowc[j])
minn = lowc[k=j]; vis[k] = true;
ret += minn;
used[pre[k]][k] = used[k][pre[k]] = true; //pre[k]-k的边加入生成树
for(int j = ; j<=n; j++)
{
if(vis[j] && j!=k) //如果遇到已经加入生成树的点,则找到两点间路径上的最大权值。
Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowc[k]); //k的上一个点是pre[k]
if(!vis[j] && lowc[j]>cost[k][j]) //否则,进行松弛操作
{
lowc[j] = cost[k][j];
pre[j] = k;
}
}
}
return (ret==INF)?-:ret;
} int SMST(int t1 ,int n)
{
int ret = INF;
for(int i = ; i<=n; i++) //用生成树之外的一条边去代替生成树内的一条边
for(int j = i+; j<=n; j++)
{
if(cost[i][j]!=INF && !used[i][j]) //去掉了i-j路径上的某条边,但又把i、j直接连上,所以还是一棵生成树。
ret = min(ret, t1+cost[i][j]-Max[i][j]);
}
return ret;
} int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
cost[i][j] = (i==j)?:INF; for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
cost[u][v] = cost[v][u] = w;
} int t1 = Prim(, n);
int t2 = SMST(t1, n);
printf("%d %d\n", t1, t2);
}
}
UVA10600 ACM Contest and Blackout —— 次小生成树的更多相关文章
- 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 ...
- UVA-10600 ACM Contest and Blackout (次小生成树)
题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...
- UVA10600 ACM Contest and Blackout
用prim算法求最小生成树和次小生成树~ #include<cstdio> #include<algorithm> #include<cstring> using ...
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- 【uva 10600】ACM Contest and Blackout(图论--次小生成树 模版题)
题意:有T组数据,N个点,M条边,每条边有一定的花费.问最小生成树和次小生成树的权值. 解法:具体请见 关于生成树的拓展 {附[转]最小瓶颈路与次小生成树}(图论--生成树) 1 #include&l ...
- uva 10600 ACM Contest And Blackout
题意: 求最小生成树和次小生成树的总权值. 思路: 第一种做法,适用于规模较小的时候,prim算法进行的时候维护在树中两点之间路径中边的最大值,复杂度O(n^2),枚举边O(m),总复杂度O(n^2) ...
- kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数
第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
随机推荐
- jQuery常用案例总结
模态对话框 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- .NET中的缓存实现
软件开发中最常用的模式之一是缓存,这是一个简单但非常有效的概念,想法是重用操作结果,执行繁重的操作时,我们会将结果保存在缓存容器中,下次我们需要该结果时,我们将从缓存容器中取出它,而不是再次执行繁重的 ...
- RF新手常见问题总结--(基础篇)
1. 经常有人问这个元素找不到,一般先排除这两个地方,再自己找找A:是否等待了足够的时间让元素加载 (增加sleep xx, wait Until xxx)B: 仔细查查,这个元素是否进入到另一个f ...
- BootStrap学习01框架搭建
中文文档:https://v3.bootcss.com/css/ 开发工具 WebStorm 一.新建项目08bootstrap 引入bootstrap-3.3.7,引入jQuery,引入holder ...
- 如何使用werkzeug创建WSGI APP
注意 : 1.定义__call__的意义 class App(): def __init__(self): pass def method(self): pass app=App() app() #错 ...
- 运动员最佳匹配问题(km算法)
洛谷传送门 带权二分图最大权完美匹配. 裸的km算法. 注意开long long. #include <cstdio> #include <cstring> #include ...
- VirtualBox - 虚拟机下主机与虚拟机、虚拟机与虚拟机之间通信配置
看了一下网上别人写的文章:http://www.it165.net/os/html/201401/7063.html 文章里面使用的是Debian,我这里配置的虚拟机系统一个是Ubuntu 14.10 ...
- 1716: [Usaco2006 Dec]The Fewest Coins 找零钱
n<=100种硬币,给每种的硬币的面额<=120和我每种有多少个<=10000,店主的硬币跟我一样但有无限个,求买t<=10000块钱的东西钱最少转手几次. 我拿的硬币最少几次 ...
- msp430项目编程31
msp430中项目---无线通信系统31 1.SPI工作原理 2.nrf24l01工作原理 3.代码(显示部分) 4.代码(功能实现) 5.项目总结
- php 压缩数据存储
php 压缩数据存储 当接收到大量的数据时,存储到数据库和从数据库读取时,时间都比较慢,所以压缩一下入库可能会好一点. 仅供参考!!! 封装的压缩数据函数: /** * 压缩数据 * @param s ...