标签:
#include<iostream> using namespace std; const int N=110; const int INF=0x3f3f3f3f; int n,ans; int imap[N][N],dis[N],vis[N]; void Prim(){ int i; for(i=1;i<=n;i++){ dis[i]=imap[1][i]; vis[i]=0; } dis[1]=0; vis[1]=1; int j,k,tmp; for(i=1;i<=n;i++){ tmp=INF; for(j=1;j<=n;j++) if(!vis[j] && tmp>dis[j]){ k=j; tmp=dis[j]; } if(tmp==INF) break; vis[k]=1; ans+=dis[k]; for(j=1;j<=n;j++) if(!vis[j] && dis[j]>imap[k][j]) dis[j]=imap[k][j]; } } int main(){ while(~scanf("%d",&n)&&n){ int ii,jj,temp1,temp2; for(int i=0;i<n*(n-1)/2;i++) { scanf("%d %d",&ii,&jj); scanf("%d",&temp1); scanf("%d",&temp2); if(temp2) {imap[ii][jj]=0; imap[jj][ii]=0;} else {imap[ii][jj]=temp1; imap[jj][ii]=temp1; } } ans=0; Prim(); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Sikaozhe/p/5627887.html