Constructing Roadster

原创 2015年11月18日 18:08:04

 Constructing Roads

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    

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
 

#include"cstdio"
#include"cstring"
#include"iostream"

using namespace std;

#define INF 0x7f7f7f7f

int n;
int maze[105][105];
int dis[105];
bool vis[105];

void Prim()
{
    int ans = 0;
    dis[1] = 0;
    vis[1] = true;
    for(int i = 1;i <= n;i++)
    {
        int minn = INF;
        int mark = INF;
        for(int j = 1;j <= n;j++)
        {
            if(!vis[j] && dis[j] < minn)
            {
                minn = dis[j];
                mark = j;
            }
        }
        if(minn == INF)
        {
            break;
        }
        ans += dis[mark];
        vis[mark] = true;
        for(int j = 1;j <= n;j++)
        {
            if(!vis[j] && maze[mark][j] < dis[j])
            {
                dis[j] = maze[mark][j];
            }
        }
    }
    printf("%d\n",ans);
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                scanf("%d",&maze[i][j]);
            }
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int a,b;
            scanf("%d%d",&a,&b); //已经修建的道路
            maze[a][b] = 0;
            maze[b][a] = 0;
        }
        for(int i = 1;i <= n;i++)
        {
            dis[i] = maze[1][i];
        }
        memset(vis,false,sizeof(vis));
        Prim();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Constructing identities in Indian networks

  • 2012年12月06日 15:55
  • 326KB
  • 下载

Constructing Minimum Roads(修建最短距离公路)问题

简而言之,就是N个村庄互相连通,在指定个别已修好的路的前提下求最短公路距离。 结合数据结构的普里姆算法和克鲁斯卡尔算法求出。 算法输入第一行为村庄个数,然后各行就是个邻接矩阵,最后指出有几条已修的...

Constructing a Bootable CD

  • 2012年03月02日 16:15
  • 152KB
  • 下载

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

题目:Constructing RoadsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others...

HDU 1102 Constructing Roads(Prim算法)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

HDU1102 Constructing Roads

题意大致是: 有N多村子,村庄之间的距离已经给出。求联通各个村庄的最短路径。 有一个附加条件,期中一些村庄已经有公路村庄,即已经连通。   非常简单, 只要把已经连通的村庄之间的距离设置为0,...

HDU 1102 Constructing Roads

Problem Description There are N villages, which are numbered from 1 to N, and you should build so...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Constructing Roadster
举报原因:
原因补充:

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