标签:des style blog http color strong
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19847 | Accepted: 6959 |
Description
Input
Output
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 #define INF 0x3f3f3f 11 using namespace std; 12 int mp[101][101],d[101]; 13 int n,m; 14 bool vis[101]; 15 int prim(){ 16 int i,j,k,temp,index,ans = 0; 17 for(i = 1; i <= n; i++) d[i] = INF; 18 d[1] = 0; 19 memset(vis,false,sizeof(vis)); 20 for(i = 0; i < n; i++){ 21 temp = INF; 22 for(j = 1; j <= n; j++) 23 if(!vis[j] && d[j] < temp) temp = d[index = j]; 24 k = 0; 25 for(j = 1; j <= n; j++){ 26 if(vis[j] && mp[index][j] == temp) k++; 27 } 28 if(k > 1) return -1; 29 vis[index] = true; 30 ans += temp; 31 for(j = 1; j <= n; j++){ 32 if(!vis[j] && d[j] > mp[index][j]) d[j] = mp[index][j]; 33 } 34 } 35 return ans; 36 } 37 int main(){ 38 int ks,i,j,u,v,w; 39 scanf("%d",&ks); 40 while(ks--){ 41 scanf("%d%d",&n,&m); 42 for(i = 0; i <= n; i++) 43 for(j = 0; j <= n; j++) 44 mp[i][j] = INF; 45 for(i = 0; i < m; i++){ 46 scanf("%d%d%d",&u,&v,&w); 47 if(mp[u][v] > w){ 48 mp[u][v] = mp[v][u] = w; 49 } 50 } 51 w = prim(); 52 if(w < 0) puts("Not Unique!"); 53 else printf("%d\n",w); 54 } 55 return 0; 56 }
POJ 1679 The Unique MST,布布扣,bubuko.com
标签:des style blog http color strong
原文地址:http://www.cnblogs.com/crackpotisback/p/3856991.html