Constructing Roads

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

题意:有n个城市,城市标号1~n,已知每两个城市之间连接所需的费用以及已经连通的城市,要使所有的城市连通所需的费用最小。

代码①号(prime):

#include<stdio.h>
#define inf 0x3f3f3f
int n,m,x,y,a[120][120];
void prime()
{
    int i,j,k,mi,cnt=0,sum=0,dis[120],vis[120]={0};
    for(int i=1;i<=n;i++)//dis[i]表示连接城市1到i城市的所需的费用
        dis[i]=a[1][i];
    vis[1]=1;
    cnt++;
    while(cnt<n)//有n个城市,所以总共有n-1次连接就好
    {
        mi=inf;
        for(i=1;i<=n;i++)
        {
            if(vis[i]==0&&dis[i]<mi)
            {
                mi=dis[i];
                j=i;
            }
        }
        vis[j]=1;
        cnt++;
        sum+=dis[j];
        for(k=1;k<=n;k++)
        {
            if(vis[k]==0&&dis[k]>a[j][k])
                dis[k]=a[j][k];
        }
    }
    printf("%d\n",sum);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                scanf("%d",&a[i][j]);
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&x,&y);
            a[x][y]=a[y][x]=0;
        }
        prime();
    }
    return 0;
}

代码②号(kruskal):

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,t,x,y,f[12000];
struct node
{
    int x,y,d;
}p[12000];
bool cmp(node a,node b)
{
    return a.d<b.d;
}
int fond(int x)
{
    while(x!=f[x])
        x=f[x];
    return x;
}
void bing(int a,int b)
{
    a=fond(a);
    b=fond(b);
    if(a!=b)
        f[a]=b;
}
void kruskal()
{
    int sum=0,num=0;
    for(int i=0;i<t;i++)
    {
        if(fond(p[i].x)!=fond(p[i].y))
        {
            bing(p[i].x,p[i].y);
            sum+=p[i].d;
            num++;
        }
    }
    printf("%d\n",sum);
}
int main()
{
    while(~scanf("%d",&n))
    {
        t=0;
        for(int i=1;i<=n;i++)
        {
            f[i]=i;
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&p[t].d);
                p[t].x=i;
                p[t].y=j;
                t++;
            }
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);//因为城市x到城市y已经连接,所以将它们并到一起
            bing(x,y);
        }
        sort(p,p+t,cmp);
        kruskal();
    }
    return 0;
}

发布了78 篇原创文章 · 获赞 4 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章