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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章