zoukankan      html  css  js  c++  java
  • 最小生成树のprim算法

    Problem A

    Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 31   Accepted Submission(s) : 10
    Problem Description
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
     
    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
     
    Output
    对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
     
    Sample Input
    3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
     
    Sample Output
    3 ?
     
    /****************************************/
    /*---------- C++代码如下 ----------------*/
    /****************************************/
     

    #include <iostream>
    #include <cstring>

    using namespace std;

    #define MaxInt 0x3f3f3f3f
    #define N 110

    int map[N][N],low[N],visited[N];
    int n;
     
    int prim()
    {
        int i,j,pos,min,result=0;
        memset(visited,0,sizeof(visited));
    //从某点开始,分别标记和记录该点
        visited[1]=1;pos=1;
    //第一次给low数组赋值
        for(i=1;i<=n;i++)
            if(i!=pos) low[i]=map[pos][i];
    //再运行n-1次
        for(i=1;i<n;i++)
        {
    //找出最小权值并记录位置
         min=MaxInt;
         for(j=1;j<=n;j++)
             if(visited[j]==0&&min>low[j])
             {
                 min=low[j];pos=j;
             }
       if(min==MaxInt) return -1;
    //最小权值累加
        result+=min;
    //标记该点
        visited[pos]=1;
    //更新权值
        for(j=1;j<=n;j++)
            if(visited[j]==0&&low[j]>map[pos][j])
                low[j]=map[pos][j];
        }
        return result;
    }

    int main()
    {
     int m,i,j,v,ans;
     while(cin>>m>>n&&m)
     {
      //所有权值初始化为最大
            memset(map,MaxInt,sizeof(map));

      while(m--)
      {
       cin>>i>>j>>v;
       if(i!=j)map[i][j]=map[i][j]=v;
      }
      ans = prim();
      if(ans==-1)cout<<"?"<<endl;
         else cout<<ans<<endl;
     
     
     }
     return 0;

    }

    /********************************/

    Problem B

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 25   Accepted Submission(s) : 14
    Problem Description
    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
     
    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。当N为0时,输入结束,该用例不被处理。
     
    Output
    对每个测试用例,在1行里输出最小的公路总长度。
     
    Sample Input
    3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
     
    Sample Output
    3 5 [hint]Hint[/hint] Huge input, scanf is recommended.
     
    /****************************************/
    /*---------- C++代码如下 ----------------*/
    /****************************************/
     

    #include <iostream>
    #include <cstring>

    using namespace std;

    #define MaxInt 0x3f3f3f3f
    #define N 110

    int map[N][N],low[N],visited[N];
    int n;
     
    int prim()
    {
        int i,j,pos,min,result=0;
        memset(visited,0,sizeof(visited));
    //从某点开始,分别标记和记录该点
        visited[1]=1;pos=1;
    //第一次给low数组赋值
        for(i=1;i<=n;i++)
            if(i!=pos) low[i]=map[pos][i];
    //再运行n-1次
        for(i=1;i<n;i++)
        {
    //找出最小权值并记录位置
         min=MaxInt;
         for(j=1;j<=n;j++)
             if(visited[j]==0&&min>low[j])
             {
                 min=low[j];pos=j;
             }
       if(min==MaxInt) return -1;
    //最小权值累加
        result+=min;
    //标记该点
        visited[pos]=1;
    //更新权值
        for(j=1;j<=n;j++)
            if(visited[j]==0&&low[j]>map[pos][j])
                low[j]=map[pos][j];
        }
        return result;
    }

    int main()
    {
     int m,i,j,v,ans;
     while(cin>>m>>n&&m)
     {
      //所有权值初始化为最大
            memset(map,MaxInt,sizeof(map));

      while(m--)
      {
       cin>>i>>j>>v;
       if(i!=j)map[i][j]=map[i][j]=v;
      }
      ans = prim();
      if(ans==-1)cout<<"?"<<endl;
         else cout<<ans<<endl;
     
     
     }
     return 0;

    }

    /********************************************/

    Problem C

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 10   Accepted Submission(s) : 8
    Problem Description
    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
     
    Input
    输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
     
    Output
    每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
     
    Sample Input
    2 2 10 10 20 20 3 1 1 2 2 1000 1000
     
    Sample Output
    1414.2 oh!
     
     
    /****************************************/
    /*---------- C++代码如下 ----------------*/
    /****************************************/
     
    #include <iostream>  
    #include <cmath>  
     
    using namespace std; 
    #define MAX 100010  
    #define LEN 105  
    double dist[LEN]; 
    double map[LEN][LEN]; 
    bool isvisited[LEN]; 
     
    //点的结构体  
    typedef struct Point

        double x; 
        double y; 
    }Point; 
     
    //初始化  
    void init(){ 
        int i,j; 
        for(i=0;i<LEN;i++)

            for(j=0;j<LEN;j++)

             if(i==j) map[i][j]=0;   //对a[][]进行初始化,一般都要;  
                map[i][j]=MAX; 
            } 
        } 

     
    //计算两点距离  
    double caculteD(Point a,Point b)

        double len; 
        len=sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y)); 
        return len; 
     

     
    //prim算法  
    double prim(int n){ 
        int i,j,min,pos; 
        double sum=0; 
        memset(isvisited,false,sizeof(isvisited)); 
     
        //初始化  
        for(i=1;i<=n;i++){ 
            dist[i]=map[1][i]; 
        } 
     
        //从1开始  
        isvisited[1]=true; 
        dist[1]=MAX; 
     
        //找到权值最小点并记录下位置  
        for(i=1;i<n;i++){
            min=MAX; 
            //pos=-1;  
            for(j=1;j<=n;j++){ 
                if(!isvisited[j] && dist[j]<min)

                    min=dist[j]; 
                    pos=j; 
                }
            }   
            if(min==MAX){ 
                return -1; 
     
            } 
            sum+=dist[pos];//加上权值  
            isvisited[pos]=true; 
     
            //更新权值  
            for(j=1;j<=n;j++){ 
                if(!isvisited[j] && dist[j]>map[pos][j])

                    dist[j]=map[pos][j]; 
                } 
            } 
        }   
        return sum; 

     
    int main(){ 
        Point p[105]; 
        int i,j,n,nCase; 
        cin>>nCase; 
        double result;//总价  
        while(nCase--){ 
            cin>>n; 
            init(); //初始化  
            for(i=1;i<=n;i++){ 
                scanf("%lf%lf",&p[i].x,&p[i].y); 
            } 
            for(i=1;i<n;i++){ 
                for(j=i+1;j<=n;j++){   
                    double len; 
                    len=caculteD(p[i],p[j]); 
                    if(len>=10 && len<=1000){//长度有限制  
                        map[i][j]=map[j][i]=100*len;//要*100    
                    }   
                } 
            } 
            result=prim(n); 
            if(result==-1){ 
                cout<<"oh!"<<endl; 
            } 
            else{ 
                printf("%.1lf\n",result); 
            } 
        } 
         return 0; 
    }
     
     
    /**********************************************/
     

    Problem D

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 15   Accepted Submission(s) : 6
    Problem Description
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
     

    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。 当N为0时输入结束。
     

    Output
    每个测试用例的输出占一行,输出全省畅通需要的最低成本。
     

    Sample Input
    3 1 2 1 0 1 3 2 0 2 3 4 0 3 1 2 1 0 1 3 2 0 2 3 4 1 3 1 2 1 0 1 3 2 1 2 3 4 1 0
     

    Sample Output
    3 1 0
     
     
     
    /****************************************/
    /*---------- C++代码如下 ----------------*/
    /****************************************/
     

    #include <iostream>
    #include <cstring>

    using namespace std;
    #define MaxInt 0x3f3f3f3f
    #define N 110


    int map[N][N],low[N],visited[N];


    int n;
     
    int prim()
    {
        int i,j,pos,min,result=0;
        memset(visited,0,sizeof(visited));

       visited[1]=1;pos=1;


        for(i=1;i<=n;i++)
            if(i!=pos) low[i]=map[pos][i];

        for(i=1;i<n;i++)
        {
     
         min=MaxInt;
     
         for(j=1;j<=n;j++)
      {
      
        if(visited[j]==0&&min>low[j])
             {
                 min=low[j];pos=j;
             }
     }
     
           

     if(min!=MaxInt)
      result+=min;

        visited[pos]=1;

        for(j=1;j<=n;j++)
            if(visited[j]==0&&low[j]>map[pos][j])
                low[j]=map[pos][j];
        }
        return result;
    }
     
    int main()
    {
        int i,v,j,ans,c,t;
        while(scanf("%d",&n)!=EOF&&n)
        {

            memset(map,MaxInt,sizeof(map));
            for(t=1;t<=n*(n-1)/2;t++)
            {
     scanf("%d%d%d%d",&i,&j,&v,&c);
                if(c==0)map[i][j]=map[j][i]=v;
        else map[i][j]=map[j][i]=0;
             }
                ans=prim();
        printf("%d\n",ans);
      
        }
     
        return 0;
    }

    /*******************************/

    Problem E

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 6   Accepted Submission(s) : 4
    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
     
     
    /****************************************/
    /*---------- C++代码如下 ----------------*/
    /****************************************/
     
    #include <iostream>  
    #include <cmath>  
     
    using namespace std; 
    #define MAX 100010  
    #define LEN 105  
    double dist[LEN]; 
    double map[LEN][LEN]; 
    bool isvisited[LEN]; 
     
    //点的结构体  
    typedef struct Point

        double x; 
        double y; 
    }Point; 
     
    //初始化  
    void init()

        int i,j; 
        for(i=0;i<LEN;i++){ 
            for(j=0;j<LEN;j++){ 
             if(i==j) map[i][j]=0;   //对a[][]进行初始化,一般都要;  
                map[i][j]=MAX; 
            } 
        } 

     
    //计算两点距离  
    double caculteD(Point a,Point b)

        double len; 
        len=sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y)); 
        return len; 
     

     
    //prim算法  
    double prim(int n)

        int i,j,min,pos; 
        double sum=0; 
        memset(isvisited,false,sizeof(isvisited)); 
     
        //初始化  
        for(i=1;i<=n;i++)

            dist[i]=map[1][i]; 
        } 
     
        //从1开始  
        isvisited[1]=true; 
        dist[1]=MAX; 
     
        //找到权值最小点并记录下位置  
        for(i=1;i<n;i++)
    {
            min=MAX; 
            //pos=-1;  
            for(j=1;j<=n;j++){ 
                if(!isvisited[j] && dist[j]<min)

                    min=dist[j]; 
                    pos=j; 
                }
            }   
            if(min==MAX)

                return -1; 
     
            } 
            sum+=dist[pos];//加上权值  
            isvisited[pos]=true; 
     
            //更新权值  
            for(j=1;j<=n;j++){ 
                if(!isvisited[j] && dist[j]>map[pos][j])

                    dist[j]=map[pos][j]; 
                } 
            } 
        }   
        return sum; 

     
    int main()

        Point p[105]; 
        int i,j,n; 
        double result; 
        double len;
        while(cin>>n)
        { 
            init(); //初始化  
            for(i=1;i<=n;i++){ 
                scanf("%lf%lf",&p[i].x,&p[i].y); 
            } 
            for(i=1;i<n;i++)
            { 
                for(j=i+1;j<=n;j++)
                {   
                    len=caculteD(p[i],p[j]); 
                    map[i][j]=map[j][i]=len;
                } 
            } 
            result=prim(n); 
            printf("%.2lf\n",result); 
            } 
         
         return 0; 
    }
     
    /***********************************/
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    sqlserver表结构导出excel格式
    c#对象深复制demo
    aspose.cells导出Demo
    复制表结构数据至另一数据库
    C#安全类型转换基于convert
    c# winform devexpress TreeList过滤和绑定
    万能分页存储过程
    WebSocket简单使用
    本地搭建持续集成(AzureDevops)
    centos 7 安装nginx并启动(笔记)
  • 原文地址:https://www.cnblogs.com/ws5167/p/3863870.html
Copyright © 2011-2022 走看看