Constructing Roads

原创 2018年04月17日 21:02:55
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;
}

HDOJ 1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • zwj1452267376
  • zwj1452267376
  • 2015-08-11 21:07:43
  • 660

POJ-2421-Constructing Roads(最小生成树 普利姆)

D - Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u...
  • qq_32680617
  • qq_32680617
  • 2016-05-21 19:43:15
  • 455

HDOJ 1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • cs_zlg
  • cs_zlg
  • 2012-04-05 17:49:50
  • 2303

HDOJ--1102--Constructing Roads(包含题意)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • gui951753
  • gui951753
  • 2015-08-11 20:30:22
  • 331

HDU 1102 Constructing Roads (裸的并查集)

题目意思:有n个村庄,编号1-n,以矩阵的形式给出任意两个村庄之间的距离,然后告诉已经有q个村庄已经修好了路,问现在要打算使所有村庄都联通需要修路的最小长度。 思路就是构造一棵最小生成树,所以将距离排...
  • IAccepted
  • IAccepted
  • 2014-06-07 19:55:30
  • 1711

poj2421Constructing Roads(最小生成树)

思路:一些已经连好的边添加一条权值为0的路,然后就是上模板了,又SB忘记init() #include #include #include #include #include #i...
  • qq_21057881
  • qq_21057881
  • 2016-01-31 23:16:29
  • 420

Constructing Roads(1102)hdu

Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)...
  • dqsyguoguo
  • dqsyguoguo
  • 2014-11-04 20:38:59
  • 413

HUT F - Constructing Roads

继续最短路模板题额。。 只是多了一个条件就是有些路是已经修好的,那么只要把修好的路之间的距离设为0,再Dijkstra一遍就出答案~                              ...
  • sky_fighting
  • sky_fighting
  • 2013-04-14 21:58:28
  • 594

Constructing Roads poj

Description There are N villages, which are numbered from 1 to N, and you should build some roads...
  • clx55555
  • clx55555
  • 2016-09-02 11:06:39
  • 144

POJ2421 Constructing Roads(最小生成树)

题意: 村庄之间连通,要求输出最小权值和,注意有些村庄已经连通 要点: Prim算法稍微变一下,因为是求权值和,已经连通的之间权值改为0即可,反正为0后面算的时候肯定是最小值,会加入集合的,然后...
  • SeasonJoe
  • SeasonJoe
  • 2016-04-03 15:02:03
  • 155
收藏助手
不良信息举报
您举报文章:Constructing Roads
举报原因:
原因补充:

(最多只允许输入30个字)