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;
}

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

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
  • 631

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
  • 373

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
  • 2207

HDU 1102 Constructing Roads (裸的并查集)

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

Warning: constructing COleException, scode = DISP_E_MEMBERNOTFOUND ($80020003).

关于htmlview 在wmsize消息里面的处理,不做解释,你懂的,百度一下还是msdn说的清楚些:The last two lines are notification that an excep...
  • xuxinhua
  • xuxinhua
  • 2011年04月07日 19:26
  • 5755

iOS知识小集 第三期 (2015.06.30)

Swift2出来了,还是得与时俱进啊,不然就成老古董了。再者它开源了,又有事情要做了。当个程序猿真是累啊,一直在追,可从来没追上,刚有那么点念想了,人家又踩了脚油门。 一个月又要过去了,说好的一月两...
  • bluewindaa
  • bluewindaa
  • 2015年07月28日 15:24
  • 751

"can't supply flags when constructing one RegExp from another"

在Chrome47下遇到这个错误, 但是在版本54下是正常的.var re = /^\w{8}$/; var reObj = new RegExp(re, 'i');解决方法也很简单, 就是把i标志去...
  • butterfly5211314
  • butterfly5211314
  • 2016年11月25日 23:25
  • 910

HDU1025:Constructing Roads In JGShining's Kingdom(LIS)

Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities whic...
  • libin56842
  • libin56842
  • 2013年07月30日 17:18
  • 1672

HBASE内存泄露读文件失败的问题

2016-12-28 04:04:58,586 INFO  [RS_OPEN_REGION-slave1:16020-2] regionserver.HRegion: Replaying edits ...
  • Yuli_li
  • Yuli_li
  • 2016年12月29日 16:38
  • 1221

axis2 实现双向SSL 因为密码问题出现NoSuchAlgorithmException

AXIS2实现双向SSL代码: String endpoint = "https://xxxx/xxx/xxx?wsdl";          RPCServiceClient serviceClie...
  • h7haw9btp
  • h7haw9btp
  • 2014年01月06日 20:33
  • 1521
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Constructing Roadster
举报原因:
原因补充:

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