uVA 10600 ACM Contest and Blackout (prim求次小生成树)
来源:互联网 发布:tensorflow gpu下载 编辑:程序博客网 时间:2024/06/05 16:58
use标记当前边是否属于最小生成树。
d[i][j]代表从i->j的瓶颈路。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;int n , m , a , b , c;int map[105][105];int dist[105] , vis[105] , ans1 , ans2 , use[105][105] , p[205];int d[105][105];void prim(){ int tid , tmin; ans1 = 0; memset(vis , 0 , sizeof(vis)); memset(dist , 0 , sizeof(dist)); memset(p , -1 , sizeof(p)); for(int i = 1 ; i <= n ; i ++){ dist[i] = inf; } dist[1] = 0; p[1] = -1; for(int i = 0 ; i < n; i ++){ //printf("i = %d\n" , i); tid = -1; tmin = inf; for(int j = 1 ; j <= n ; j ++){ if(vis[j] == 0 && tmin > dist[j]){ tmin = dist[j]; tid = j; } } ans1 += tmin; //printf("tmin = %d\n" , tmin); vis[tid] = 1; for(int j = 1 ; j <= n ; j ++){ if(vis[j] == 0 && dist[j] > map[tid][j]){ dist[j] = map[tid][j]; p[j] = tid; //printf("j = %d pos = %d\n" , j , tid); } } } return ;}void dfs(int v){ for(int u = 1 ; u <= n ; u ++){ if(vis[u] == 0 && p[u] == v){ vis[u] = 1; for(int x = 1 ; x <= n ; x ++){ if(vis[x] == 1 && x != u){ d[x][u] = d[u][x] = max(d[x][v] , map[u][v]); } } dfs(u); } } return ;}void Second_MST(){ ans2 = inf; for(int i = 1;i <= n; ++i){ for(int j = 1;j <= n; ++j){ if(use[i][j] == 1){ if(ans1 + map[i][j] - d[i][j] < ans2) ans2 = ans1 + map[i][j] - d[i][j]; } } }}int main(){ int t; scanf("%d" , &t); while(t --){ memset(map , inf , sizeof(map)); memset(use , 0 , sizeof(use)); scanf("%d%d" , &n , &m); for(int i = 0 ; i < m ; i ++){ scanf("%d%d%d" , &a , &b , &c); map[a][b] = c; map[b][a] = c; use[a][b] = use[b][a] = 1; } prim(); printf("%d " , ans1); memset(vis , 0 , sizeof(vis)); memset(d , 0 , sizeof(d)); vis[1] = true; dfs(1); for(int i = 1 ; i <= n ; i ++){ if(p[i] != -1){ use[p[i]][i] = use[i][p[i]] = 0; } } Second_MST(); printf("%d\n" , ans2); }}
- uVA 10600 ACM Contest and Blackout (prim求次小生成树)
- 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
- 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(次小生成树)
- Crontab的格式
- C++黑窗口控制台中菱形的简单画法
- shell攻略-让文本飞
- Apache TrafficServer作为反向代理服务器
- git clone时,提示warning: remote HEAD refers to nonexistent ref, unable to checkout.
- uVA 10600 ACM Contest and Blackout (prim求次小生成树)
- 定时器实现 使用spring
- NS-2相关工具大体介绍<1>
- ios7 css 适配
- 哈希表及处理冲突的方法
- Servlet操作Cookie说明
- 观察者模式
- java实战(五)------tomcat 7.0的安装配置使用运行war包
- IE 浏览器