标签:
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 11028 | Accepted: 3124 | Special Judge |
Description
Input
Output
Sample Input
9 1 5 0 0 3 2 4 5 5 1 0 4 5 2 1 2 5 3 3 1 3 9 7 1 2
Sample Output
1 6 3 7 4 9 5 7 8 3
Source
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 double a[800], b[800], map[800][800], dis[800]; int vis[800]; 8 int n; 9 void Prim() 10 { 11 for(int i = 1; i <= n; i++){ 12 dis[i] = map[1][i]; 13 vis[i] = 1; 14 } 15 dis[1] = 0; vis[1] = -1; 16 for(int i = 0; i< n-1; i++) 17 { 18 int temp = -1; double min = INF; 19 for(int j = 1; j <= n; j++) 20 { 21 if(vis[j] != -1 && min > dis[j]) 22 { 23 temp = j; 24 min = dis[j]; 25 } 26 } 27 28 if(temp != -1 && dis[temp] != 0) 29 printf("%d %d\n", temp, vis[temp]); 30 vis[temp] = -1; 31 for(int j = 1; j <= n; j++) 32 { 33 if(vis[j] != -1 && dis[j] > map[temp][j]){ 34 dis[j] = map[temp][j]; vis[j] = temp; 35 } 36 } 37 } 38 //printf("-1\n"); 39 } 40 41 int main() 42 { 43 scanf("%d", &n); 44 for(int i = 1; i <= n; i++) 45 scanf("%lf %lf", &a[i], &b[i]); 46 for(int i = 1; i <= n; i++) 47 for(int j = 1; j <= n; j++){ 48 double temp = sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j])); 49 map[i][j] = map[j][i] = temp; 50 } 51 int m; 52 scanf("%d", &m); 53 for(int i = 1; i <= m; i++){ 54 int a, b; 55 scanf("%d %d", &a, &b); 56 map[a][b]=map[b][a]=0; 57 58 } 59 Prim(); 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4725985.html