标签:memset names strong ++ map 生成 for span cst
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43080 Accepted Submission(s): 19636
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<queue> 7 #include<map> 8 #include<set> 9 #include<vector> 10 #include<cstdlib> 11 #include<string> 12 #define eps 0.000000001 13 typedef long long ll; 14 typedef unsigned long long LL; 15 using namespace std; 16 const int N=100+10; 17 const int INF=0x3f3f3f3f; 18 int mp[N][N]; 19 int n; 20 int vis[N]; 21 int dis[N]; 22 int u,v,w; 23 void prim(){ 24 int ans=0; 25 memset(vis,0,sizeof(vis)); 26 memset(dis,0,sizeof(dis)); 27 for(int i=1;i<=n;i++)dis[i]=mp[1][i]; 28 vis[1]=1; 29 for(int i=1;i<=n-1;i++){ 30 int pos; 31 int minn=INF; 32 for(int j=1;j<=n;j++){ 33 if(vis[j]==0&&minn>dis[j]){ 34 pos=j; 35 minn=dis[j]; 36 } 37 38 } 39 vis[pos]=1; 40 ans=ans+minn; 41 for(int j=1;j<=n;j++){ 42 if(vis[j]==0&&dis[j]>mp[pos][j])dis[j]=mp[pos][j]; 43 } 44 } 45 cout<<ans<<endl; 46 } 47 int main(){ 48 while(scanf("%d",&n)!=EOF){ 49 if(n==0)break; 50 for(int i=1;i<=n;i++) 51 for(int j=1;j<=n;j++)mp[i][j]=INF; 52 int m; 53 if(n%2==0)m=n/2*(n-1); 54 else 55 m=(n-1)/2*n; 56 for(int i=1;i<=m;i++){ 57 scanf("%d%d%d",&u,&v,&w); 58 mp[u][v]=w; 59 mp[v][u]=w; 60 } 61 prim(); 62 } 63 }
标签:memset names strong ++ map 生成 for span cst
原文地址:http://www.cnblogs.com/Aa1039510121/p/6463952.html