Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 1
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; int map[110]; struct Point{ int start,end; double len; }point[10010]; int cmp(Point a,Point b){ return a.len<b.len; } struct dot{ double x,y; }dot[110]; int find(int x){ int r=x; while(r!=map[r])r=map[r]; int i=x,j; while(i!=r)j=map[i],map[i]=r,i=j; return r; } double distant(double x1,double y1,double x2,double y2){ double x=x2-x1,y=y2-y1,L; L=sqrt(x*x+y*y); return L; } int main(){ int n,c; double distance; while(~scanf("%d",&n)){ // memset(map,0,sizeof(map)); // memset(point,0,sizeof(point)); for(int i=1;i<=n;++i)map[i]=i; for(int i=1;i<=n;++i)scanf("%lf%lf",&dot[i].x,&dot[i].y);c=0; for(int i=1;i<=n;++i){ for(int j=1;j<i;++j){point[c].start=j;point[c].end=i; point[c].len=distant(dot[j].x,dot[j].y,dot[i].x,dot[i].y); c++; } } sort(point,point+c,cmp);distance=0; for(int i=0;i<c;++i){ int f1,f2; f1=find(point[i].start);f2=find(point[i].end); if(f1!=f2)map[f1]=f2,distance+=point[i].len; } printf("%.2lf\n",distance); } return 0; }
prime:
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 const int INF=0x3f3f3f3f; 5 const int MAXN=110; 6 int n; 7 double answer; 8 double map[MAXN][MAXN],low[MAXN]; 9 int vis[MAXN]; 10 double gd(double ax,double ay,double bx,double by){ 11 double x=bx-ax,y=by-ay; 12 double s=x*x+y*y; 13 return sqrt(s); 14 } 15 void prime(){ 16 int k; 17 double temp; 18 memset(vis,0,sizeof(vis)); 19 vis[0]=1; 20 for(int i=0;i<n;i++)low[i]=map[0][i]; 21 for(int i=0;i<n;i++){ 22 temp=INF; 23 for(int j=0;j<n;j++) 24 if(!vis[j]&&temp>low[j]) 25 temp=low[k=j];//k=写错地方了,又错了半天 26 if(temp==INF){ 27 printf("%.2lf\n",answer); 28 break; 29 } 30 answer+=temp; 31 vis[k]=1; 32 for(int j=0;j<n;j++) 33 if(!vis[j]&&low[j]>map[k][j]) 34 low[j]=map[k][j]; 35 } 36 } 37 int main(){ 38 double dx[MAXN],dy[MAXN]; 39 while(~scanf("%d",&n)){ 40 answer=0; 41 for(int i=0;i<n;i++) 42 for(int j=0;j<n;j++) 43 map[i][j]=INF; 44 for(int i=0;i<n;i++) 45 scanf("%lf%lf",&dx[i],&dy[i]); 46 for(int i=0;i<n;i++) 47 for(int j=i+1;j<n;j++){ 48 double dis=gd(dx[i],dy[i],dx[j],dy[j]); 49 //printf("%lf\n",dis); 50 if(dis<map[i][j]) 51 map[i][j]=map[j][i]=dis; 52 } 53 prime(); 54 } 55 return 0; 56 }