zoukankan      html  css  js  c++  java
  • 管道问题(prim)

           

    Description

    Water can flow around, does not it? And it can also flow along a tunnel until it become stable. A certain amount of water could be held by some length of tunnel, so the amount of water in the tunnels is related to the length of tunnels. Here goes the problem that you are given some coordinates of vertexes. All you have to do is to connect all the vertexes with tunnels, can you find a way to connect all these vertexes so that the mount of water in these tunnels is minimized.

    Input

    The input contains multiple cases. Ease case begins with a line with an integer N (0<=N<=100), which is the number of vertexes you need to connect. The next N lines each contains two real numbers, the coordinates of vertexes. N=0 indicates the end of input.

    Output

    For each test case print the minimum of water you need, rounded to 2 decimal places.

    Sample Input

    5
    0 0
    0 3
    2 1
    1 1
    2.5 3.7
    0

    Sample Output

    7.25

     

          第一道最小生成树,用的是Prim。

     

    代码:

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #define N 101
    #define MAX 0x7fffffff
    using namespace std;
    
    double map[N][N];//邻接矩阵 
    
    typedef struct point
    {
      double x,y;       
    } POINT; 
    POINT p[N];
    
    double distance(POINT p1,POINT p2)
    {
      return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));       
    }
    
    double prim(int n);
    
    int main()
    {
      double ans;
      int vex,i,j;
      
        while(scanf("%d",&vex)&& vex)
        {
            for(i = 0 ; i < vex ; ++i)
            scanf("%lf%lf",&p[i].x,&p[i].y);
            
            /*
              计算任意两点间的距离 
            */
            for(i = 0 ; i < vex ; ++i)
            for(j = i + 1 ; j < vex ; ++j)
              map[i][j] = map[j][i] = distance(p[i],p[j]);                        
            /*
              自己到自己的距离为0 
            */ 
            for(i = 0 ; i < vex ; ++i)
              map[i][i] = 0;
            
            ans = prim(vex);
         
            printf("%.2lf\n",ans);
        }   
        
      system("pause");
      return 0;    
    }
    
    double prim(int n)
    {
      double lowcost[N];//存放各点到第 0个点的最小代价 
      double sum;
      int vexset[N];//标记最小生成树中的点,1表示在,0表示不在 
      int v,i,j;
      sum = 0;
        
        for(i = 1 ;i<n ; ++i)
        {
           lowcost[i] = map[0][i];
           vexset[i] = 0 ;      
        }       
        
        vexset[0] = 1;//首先把第0号点加入 
        
          for(i = 0 ; i < n -1; ++i)   
          {
              /*
                找出离最小生成树 “最近 ”的点 v 
              */  
              for(v = -1,j = 1 ; j < n ; ++j)//j 从1开始,0早已加入 
              {
                 /* 以前是单独声明一个变量来比较,这样多了一个语句
                     用 v = -1就 可以少一个语句,因为是for()循环,
                     所以 大大提高效率 
                 */   
                 if(!vexset[j] && (v == -1 ||lowcost[j]< lowcost[v]))
                    v = j ; 
              }     
             
              vexset[v] = 1;//v 新加入 
             /*
               权值相加 
             */ 
              sum += lowcost[v];    
            
              /*
                以新加入的点 v 为起点更新 各点到 0点 的最小代价 
              */
              for(j = 0 ; j < n ; ++j)
              {
                  if( !vexset[j] && map[v][j] < lowcost[j])  
                  {
                    lowcost[j] = map[v][j];     
                  }  
              }        
          }
          return sum; 
    }
    
  • 相关阅读:
    VS2008中影响exe大小的和速度的选项(转)
    Some VS2005 and VS2008 Wizards Pop Up Script Error(转)
    winform 界面控件更新(转)
    SQL 2005 的存储过程和触发器调试大法(转)
    c# 正则表达式(转)
    让国外邮件服务商不再拒收你的邮件,The IP you're using to send mail is not authorized(转)
    vs2010 修改标题的方法
    SetFilePointer 使用
    修复2003系统,html图表不正确的方法
    c#字符串及数组操作 (转)
  • 原文地址:https://www.cnblogs.com/HpuAcmer/p/2439638.html
Copyright © 2011-2022 走看看