POJ 1258 -Agri-Net- 最小生成树
2016-03-11 15:22
435 查看
裸生成树
#/*--------------------------------------------------------------------------------------*/ // Helica's header // Second Edition // 2015.11.7 // #include <algorithm> #include <iostream> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map> //debug function for a N*M array #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\ {for(int j=0;j<(M);j++){\ printf("%d",G[i][j]);}printf("\n");} //debug function for int,float,double,etc. #define debug_var(X) cout<<#X"="<<X<<endl; /*--------------------------------------------------------------------------------------*/ using namespace std; const int maxn = 100+10; const int INF = 0x3f3f3f3f; int N,cost[maxn][maxn],lowc[maxn]; bool vis[maxn]; int Prim() { int ans = 0; memset(vis,false,sizeof vis); vis[0] = true; for(int i=1;i<N;i++) lowc[i] = cost[0][i]; for(int i=1;i<N;i++) { int minc = INF,p=-1; for(int j=0;j<N;j++) if(!vis[j] && minc > lowc[j]) { minc = lowc[j]; p = j; } if(minc == INF) return -1; ans += minc; vis[p] = true; for(int j=0;j<N;j++) if(!vis[j] && lowc[j]>cost[p][j]) lowc[j] = cost[p][j]; } return ans; } int main() { //freopen("input.in","r",stdin); while(~scanf("%d",&N)) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { scanf("%d",&cost[i][j]); } } printf("%d\n",Prim()); } }
相关文章推荐
- SQL Server 事务、异常和游标
- linux的getopt_long用法简介
- Android xUtils框架(一) DbUtils
- 黑马程序员独家解析 “人机大战”胜负背后的意义
- SyntaxError: missing ; before statement,SCRIPT1004: 缺少 ';'
- Android 7.0 给开发者带来了什么
- iOS 多个网络请求并行/并发处理
- linux yum命令详解
- 在Mac上安装cocoapods
- web前端页面取值的几种方式
- statement语句
- JAVA程序中如何调用C/C++方法
- 2-1-1 GDI
- thinkPHP volist标签循环输出多维数组
- SQL Server T-SQL高级查询
- 让PHP7达到最高性能的几个Tips(转)
- Josephus问题
- java 遍历Map的四种方法
- tuxedo 安装,运行示例。(解决遇到的问题)
- updating local specs repositories卡住