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;
    }
  • 相关阅读:
    Android 序列化 反序列功能
    Android两个应用之间共享数据之SharedPrefence
    利用Asp.Net的Identity控制登录权限
    基元类型
    CLR概述
    python中的函数-基础
    较为复杂的sql
    oracle中的替换字段字符串的方法
    使用Sql查看表对应的外键关系。
    js中的除法
  • 原文地址:https://www.cnblogs.com/lengxia/p/4387801.html
Copyright © 2011-2022 走看看