hdoj1863
2016-04-30 17:13
190 查看
#include"iostream" using namespace std; const int vex=150; const int maxl=999999; int prim(int n,int *low,int c[][vex]) { int i,j,s[vex]={0},len=0,pos; pos=1; s[1]=1; for(i=2;i<=n;i++) low[i]=c[1][i]; for(i=1;i<n;i++) { int min=maxl; for(j=1;j<=n;j++) { if(!s[j]&&low[j]<min) { min=low[j]; pos=j; } } if(min==maxl) break; s[pos]=1; len+=min; for(j=1;j<=n;j++) { if(!s[j]&&c[pos][j]<low[j]) low[j]=c[pos][j]; } } if(i==n) return len; else return -1; } int main() { //freopen("a.txt","r",stdin); int low[vex]; int c[vex][vex]; int i,j,s,t,len,n,m; while(cin>>n>>m&&n) { for(i=1;i<=m;i++) { for(j=1;j<=m;j++) c[i][j]=maxl; low[i]=maxl; } for(i=0;i<n;i++) { cin>>s>>t>>len; if(c[s][t]>len) c[s][t]=c[t][s]=len; } int sb=prim(m,low,c); if(sb==-1) cout<<"?\n"; else cout<<sb<<endl; } return 0; }
相关文章推荐
- 微信学习_05_图文消息
- UVA 501 - Black Box(二分 + 树状数组 + 离散化)
- CodeForces - 584D Dima and Lisa (素数拆分)数学规律
- Java循环练习:打印图案-1
- 今年我23岁
- leetcode 058 Length of Last Word
- hdu4489(递推)
- POJ 3744 Scout YYF I (矩阵相乘+概率DP)
- optparse模块
- java验证自增
- optparse模块
- optparse模块
- Android Binder跨进程与非跨进程的传输异同源码分析
- Shell-批量巡检服务器脚本
- 设计模式:装饰模式(Decorator )
- 安卓实现老虎机抽奖的案例
- SVM核函数
- java中的List和Vector的区别
- Android Studio cannot resolve symbol 'TabLayout'
- android 时间控件的使用