UVa 10600 ACM contest and Blackout( 次小生成树)
来源:互联网 发布:如何配眼镜知乎 编辑:程序博客网 时间:2024/05/22 08:03
很裸的模板题
求最小生成树,和次小生成树
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 110;const int M = 310;const int INF = 100000000;int n, m, map[N][N], p[N], d[N], f[N][N], s1, s2, mark[N];void prim() { for ( int i = 1; i <= n; ++i ) p[i] = -1, d[i] = INF; int mi, v; bool vis[N]; memset(vis, 0, sizeof(vis)); d[1] = 0, s1 = 0; for ( int u = 0; u < n; ++u ) { mi = INF; for ( int i = 1; i <= n; ++i ) if ( !vis[i] && mi > d[i] ) mi = d[i], v = i; s1 += mi; vis[v] = true; for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] > map[v][i] ) d[i] = map[v][i], p[i] = v; }}void dfs( int v ) { for ( int u = 1; u <= n; ++u ) { if ( !mark[u] && p[u] == v && u != v ) { mark[u] = true; for ( int x = 1; x <= n; ++x ) if ( mark[x] && u != x ) { f[x][u] = f[u][x] = max( f[x][v], map[u][v] ); } dfs(u); } }}int main() { int T; scanf("%d", &T); while ( T-- ) { scanf("%d%d", &n, &m); for ( int i = 1; i <= n; ++i ) for ( int j = 1; j <= n; ++j ) map[i][j] = INF; while ( m-- ) { int a, b, c; scanf("%d%d%d", &a, &b, &c); map[a][b] = map[b][a] = c; } prim(); memset( f, 0, sizeof(f)); memset( mark, 0, sizeof(mark)); mark[1] = true; dfs( 1 ); s2 = INF; /*for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) printf("%d ", f[i][j] ); printf("\n"); }*/ for ( int i = 1; i <= n; ++i ) for ( int j = i+1; j <= n; ++j ) { if ( j != p[i] && i != p[j] ) { s2 = ( s2 <= s1 - f[i][j] + map[i][j] ? s2 : s1 - f[i][j] + map[i][j] ); } } printf("%d %d\n", s1, s2); }}
- UVA 10600 - ACM Contest and Blackout 次小生成树
- 【UVa】10600 ACM Contest and Blackout 次小生成树
- uva 10600 ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout (次小生成树)
- UVA - 10600 ACM Contest and Blackout(次小生成树)
- Uva 10600 ACM Contest and Blackout(次小生成树)
- UVA 10600 - ACM Contest and Blackout(最小生成树&次小生成树)
- UVa 10600 ACM contest and Blackout( 次小生成树)
- uva 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 - ACM Contest and Blackout (次小生成树)
- UVA 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout(次小生成树)
- uva 10600 ACM Contest and Blackout (次小生成树)
- uva 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- UVa 10600__ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVA 10600 ACM Contest and Blackout (次小生成树 Kruskal 封装)
- 多个菜单展开和折叠
- java辨析
- Codeforces Round #144 (Div. 1), problem: (B) Table DP 组合数学
- 微软的Visual Studio 2010 SP1的Web标准更新
- 【Android数据传递】全局变量的使用方法(附源码)
- UVa 10600 ACM contest and Blackout( 次小生成树)
- 汇编中的管道操作方法
- 云计算特点
- JAVA语言编码规范
- c语言中的#ifndef、#def、#endif等宏是什么意思 .
- Codeforces Round #138 (Div. 1), problem: (B) Two Strings
- static 初始化原理
- Ubuntu 12.10解决flash白屏的问题
- android 基础