Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9935 Accepted Submission(s): 3916
无压力水过
code:
1 #include <iostream> 2 #include <iomanip> 3 #include <fstream> 4 #include <sstream> 5 #include <algorithm> 6 #include <string> 7 #include <set> 8 #include <utility> 9 #include <queue> 10 #include <stack> 11 #include <list> 12 #include <vector> 13 #include <cstdio> 14 #include <cstdlib> 15 #include <cstring> 16 #include <cmath> 17 #include <ctime> 18 #include <ctype.h> 19 using namespace std; 20 21 #define MAXN 105 22 #define intmax 9999999 23 24 int n,m; 25 int map[MAXN][MAXN]; 26 int dis[MAXN]; 27 int vst[MAXN]; 28 29 void init() 30 { 31 int i,j; 32 for(i=1;i<=m;i++) 33 for(j=i;j<=m;j++) 34 map[i][j]=map[j][i]=intmax; 35 } 36 37 int prim() 38 { 39 int i,j,k; 40 int sum=0; 41 bool flag=true; 42 for(i=1;i<=m;i++) 43 { 44 vst[i]=0; 45 dis[i]=map[1][i]; 46 } 47 vst[1]=1; 48 for(i=2;i<=m;i++) 49 { 50 k=1; 51 int temp=intmax; 52 for(j=1;j<=m;j++) 53 if(dis[j]<temp&&!vst[j]) 54 { 55 temp=dis[j]; 56 k=j; 57 } 58 if(temp==intmax) 59 { 60 flag=false; 61 break; 62 } 63 sum+=temp; 64 vst[k]=1; 65 for(j=1;j<=m;j++) 66 if(dis[j]>map[k][j]) 67 dis[j]=map[k][j]; 68 } 69 if(!flag) 70 return -1; 71 return sum; 72 } 73 74 int main() 75 { 76 int i,j; 77 int a,b,temp; 78 while(~scanf("%d%d",&n,&m),n) 79 { 80 init(); 81 for(i=1;i<=n;i++) 82 { 83 scanf("%d%d%d",&a,&b,&temp); 84 map[a][b]=map[b][a]=temp; 85 } 86 int ans=prim(); 87 if(ans!=-1) 88 printf("%d\n",ans); 89 else 90 printf("?\n"); 91 } 92 return 0; 93 }