zoukankan      html  css  js  c++  java
  • HDOJ1162 Eddy's picture[求最短路prim||kruskal算法]

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4035    Accepted Submission(s): 1993


    Problem Description
    Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
    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?
     
    Input
    The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

    Input contains multiple test cases. Process to the end of file.
     
    Output
    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
     
    Sample Input
    3 1.0 1.0 2.0 2.0 2.0 4.0
     
    Sample Output
    3.41
     
    Author
    eddy
     
    Recommend
    JGShining
     
     
     
     
     
     
    prim算法:
     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 101
    22 
    23 double map[MAXN][MAXN];
    24 int vst[MAXN];
    25 double dis[MAXN];
    26 int n;
    27 
    28 typedef struct point
    29 {
    30     double x,y;
    31 }Point;
    32 Point point[MAXN];
    33 
    34 double fun_dis(Point a,Point b)
    35 {
    36     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    37 }
    38 
    39 double prim()
    40 {
    41     int i,j,k;
    42     double min,sum=0;
    43     memset(vst,0,sizeof(vst));
    44     for(i=1;i<=n;i++)
    45         dis[i]=map[1][i];
    46     vst[1]=1;
    47     for(i=2;i<=n;i++)
    48     {
    49         k=1;
    50         min=INT_MAX;
    51         for(j=2;j<=n;j++)
    52             if(dis[j]<min&&!vst[j])
    53             {
    54                 min=dis[j];
    55                 k=j;
    56             }
    57         sum+=min;
    58         vst[k]=1;
    59         for(j=1;j<=n;j++)
    60             if(dis[j]>map[k][j])
    61                 dis[j]=map[k][j];
    62     }
    63     return sum;
    64 }
    65 
    66 int main()
    67 {
    68     int i,j;
    69     while(~scanf("%d",&n))
    70     {
    71         for(i=1;i<=n;i++)
    72             scanf("%lf%lf",&point[i].x,&point[i].y);
    73         for(i=1;i<=n;i++)
    74             for(j=1;j<=i;j++)
    75                 map[i][j]=map[j][i]=fun_dis(point[i],point[j]);
    76         printf("%.2lf\n",prim());
    77     }
    78     return 0;
    79 }

    kruskal算法:

      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 101
     22 
     23 int n;
     24 int father[MAXN];
     25 
     26 typedef struct point
     27 {
     28     double x,y;
     29 }Point;
     30 Point point[MAXN];
     31 
     32 typedef struct edge
     33 {
     34     int x,y;
     35     double len;
     36 }Edge;
     37 Edge edge[MAXN*MAXN];
     38 
     39 double fun_dis(Point a,Point b)
     40 {
     41     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
     42 }
     43 
     44 double cmp(const Edge &a,const Edge &b)
     45 {
     46     return a.len<b.len;
     47 }
     48 
     49 int findset(int v)
     50 {
     51     return father[v];
     52 }
     53 
     54 void merget(int a,int b)
     55 {
     56     int i;
     57     for(i=1;i<=n;i++)
     58         if(father[i]==a)
     59             father[i]=b;
     60 }
     61 
     62 int main()
     63 {
     64     int i,j;
     65     int m;
     66     int count;
     67     int cnt;
     68     double sum;
     69     int tempx,tempy;
     70     while(~scanf("%d",&n))
     71     {
     72         sum=0;
     73         cnt=0;
     74         count=0;
     75         memset(edge,0,sizeof(edge));
     76         for(i=1;i<=n;i++)
     77             father[i]=i;
     78         for(i=1;i<=n;i++)
     79             scanf("%lf%lf",&point[i].x,&point[i].y);
     80         m=((n-1)*n)>>1;
     81         for(i=1;i<=n;i++)
     82             for(j=i+1;j<=n;j++)
     83             {
     84                 edge[count].x=i;
     85                 edge[count].y=j;
     86                 edge[count++].len=fun_dis(point[i],point[j]);
     87             }
     88         sort(edge,edge+count,cmp);
     89         for(i=0;i<m&&cnt!=n;i++)
     90         {
     91             tempx=findset(edge[i].x);
     92             tempy=findset(edge[i].y);
     93             if(tempx!=tempy)
     94             {
     95                 merget(tempx,tempy);
     96                 cnt++;
     97                 sum+=edge[i].len;
     98             }
     99         }
    100         printf("%.2lf\n",sum);
    101     }
    102     return 0;
    103 }
  • 相关阅读:
    Node入门--事件模块
    Node入门--1--module&require
    Node入门--1-->Hello World
    文件上传(StringMVC)
    StringMVC
    Spring基础
    手动添加日期到mysql数据库
    aspectj 注解
    HandlerMapping执行过程。。。
    在考试我打
  • 原文地址:https://www.cnblogs.com/XBWer/p/2618595.html
Copyright © 2011-2022 走看看