http://poj.org/problem?id=2421

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24132   Accepted: 10368

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

Source

 
把已经修好的路得代价改成0,再跑最小生成树就可以了。
 #include <algorithm>
#include <cstdio> using namespace std; const int N();
int n,q,u,v,w;
int dis[N][N],ans;
int minn,vis[N],d[N]; void Prime()
{
for(int i=;i<=n;i++) d[i]=dis[][i];
d[]=vis[]=;
for(int i=;i<n;i++)
{
minn=;
for(int j=;j<=n;j++)
if(!vis[j]&&(!minn||d[j]<d[minn])) minn=j;
vis[minn]=;
for(int j=;j<=n;j++)
if(!vis[j]) d[j]=min(d[j],dis[minn][j]);
}
for(int i=;i<=n;i++) ans+=d[i];
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&dis[i][j]);
scanf("%d",&q);
for(;q;q--)
{
scanf("%d%d",&u,&v);
dis[u][v]=dis[v][u]=;
}
Prime();
printf("%d",ans);
return ;
}

POJ——T2421 Constructing Roads的更多相关文章

  1. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  3. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  4. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  5. poj 2421 Constructing Roads 解题报告

    题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...

  6. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  7. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  8. POJ - 2421 Constructing Roads(最小生成树&并查集

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  9. POJ 2421 Constructing Roads

    题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...

随机推荐

  1. 《Javascript权威指南》学习笔记之十五:BOM之源---window对象

    BOM是Browser Object Model的缩写,即浏览器对象模型,提供了独立于网页内容和浏览器窗体之间进行交互的APi.API由若干对象组成,因为浏览器是Javascript的宿主,因此,这些 ...

  2. hdu 3177 Crixalis&#39;s Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. No unique bean of type [net.shougongfang.action.paymoney.AlipayPayMoneyReturnObj] is defined: Unsat

    0 你把@Service放到实现类上吧.这个问题好像不止一个人在问啦 2013年10月25日 10:34 shidan66  30  0 1 1 加入评论 00 1,@service放到实现上  2. ...

  4. mysql Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)

    [现象说明] C/S程序远程訪问正常,本地訪问报下面异常 MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to h ...

  5. JPEG压缩图像超分辨率重建算法

    压缩图像超分辨率重建算法学习 超分辨率重建是由一幅或多幅的低分辨率图像重构高分辨率图像,如由4幅1m分辨率的遥感图像重构分辨率0.25m分辨率图像.在军用/民用上都有非常大应用. 眼下的超分辨率重建方 ...

  6. 痛苦的人生——JRuby on Rails的开发与部署小记

    最近单位领导部署了一项开发用户自助服务系统的任务,该任务有且仅有我一人独立完成——哈哈,十分美妙的工作呢. 恰巧楼主最近被Ruby的美妙特性所迷惑,于是义无反顾地投入到Ruby on Rails的怀抱 ...

  7. SpringMVC+uploadify3.2.1版实现附件上传功能(直接可以使用)

    <link rel="stylesheet" type="text/css" href='<c:url value="/uploadify ...

  8. python中黏包现象

    #黏包:发送端发送数据,接收端不知道应如何去接收造成的一种数据混乱现象. #关于分包和黏包: #黏包:发送端发送两个字符串"hello"和"word",接收方却 ...

  9. JavaScript DOM编程艺术(第2版)学习笔记2(4~6章应用实例)

    本书的第4章使用第3章学到的操作DOM的方法和属性写了一个展示图片的网页,并在第5,6章对代码进行了优化. 第一版,搭建网页的静态结构,包括一级标题<h1>,无序列表清单<ul> ...

  10. Java NIO(七)管道

    Java NIO 管道是两个线程之间的单向数据连接.Pipe有一个source通道和sink通道(内部类).数据会被写到sink通道,从source通道读取. 给一张Pipe通道的原理图: 创建管道: ...