zoukankan      html  css  js  c++  java
  • Constructing Roads(HDU 1102 最小生成树)

    Description

    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

    Input

    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

    Output

    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

    Sample Input

    3
    0 990 692
    990 0 179
    692 179 0
    1
    1 2
    

    Sample Output

    179

    Hint


    Prim算法

    #include<iostream>
    using namespace std;
    int n,p[105][105],x,y,vis[105],i,j;
    int main()


    {while (scanf("%d", &n) != EOF)
    {for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    cin>>p[i][j];
    int n1;
    cin>>n1;
    for(i=1;i<=n1;i++)
    {cin>>x>>y;
    p[x][y]=0;p[y][x]=0;
    }


    memset(vis,0,sizeof(vis));
    vis[1]=1;
    int sum=0;
    for(i=0;i<n-1;i++)
    {int  max=1000;
    for(j=1;j<=n;j++)
    {if(!vis[j]&&p[1][j]<max)
    {max=p[1][j];
    x=j;}
    }
    vis[x]=1;
    sum+=max;
    for(j=1;j<=n;j++)
    {if(!vis[j]&&p[1][j]>p[x][j])
    p[1][j]=p[x][j];

    }
    }

    cout<<sum<<endl;

    }
    return 0;}







    Kruskal算法


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    struct stu
    {
     int p;
     int q;
     int val;
    }c[10009];
    int set[109];
    void Make_set(int n)
    {
     int i;
     for(i=0;i<n;i++)
      set[i]=i;
    }
    int Found_set(int a)
    {
     int i,r=a;
     while(set[r]!=r)
      r=set[r];
     while(a!=r)
     {
      i=set[a];
      set[a]=r;
      a=i;
     }
     return a;
    }
    void Union(int a,int b)
    {
     set[a]=b;
    }
    int cmp(const void *a,const void *b)
    {
     struct stu *c,*d;
     c=(struct stu *)a;
     d=(struct stu *)b;
     return c->val-d->val;
    }
    int main()
    {
     int i,j,str[101][101],n,m,x,y,k,fx,fy,sum;
     while(scanf("%d",&n)!=EOF)
     {
      for(i=0;i<n;i++)
       for(j=0;j<n;j++)
        scanf("%d",&str[i][j]);
       scanf("%d",&m);
       for(i=0;i<m;i++)
       {
        scanf("%d %d",&x,&y);
        x--;y--;
        str[x][y]=0;
        str[y][x]=0; 
       }
       k=0;
       for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
         c[k].p=i;
         c[k].q=j;
         c[k].val=str[i][j];
         k++;
        }
        qsort(c,k,sizeof(c[0]),cmp);
        sum=0;
        Make_set(n);
        for(i=0;i<k;i++)
        {
         fx=Found_set(c[i].p);
         fy=Found_set(c[i].q);
         if(fx!=fy)
         {
          Union(fx,fy);
          sum+=c[i].val;
         }
        }
        printf("%d ",sum);
     }
     return 0;
    }
  • 相关阅读:
    redhat 6.4下PXE+Kickstart无人值守安装操作系统
    ubuntu14.04安装好Hadoo之后接着安装hbase和介绍常用命令
    避坑之Hadoop安装伪分布式(Hadoop3.2.0/Ubuntu14.04 64位)
    kindeditor文件上传设置文件说明为上传文件名(JSP版)
    sqlmap 扫描注入漏洞
    局域网内访问不同网段的主机(转记)
    cmd创建用户开启3389命令
    用python来更改windows开机密码
    代码安全之上传文件
    web渗透(转)
  • 原文地址:https://www.cnblogs.com/lengxia/p/4387801.html
Copyright © 2011-2022 走看看