Constructing Roads

题目链接:

http://acm.hust.edu.cn/vjudge/contest/124434#problem/D

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

##题意:

求最小花费使得所有点联通.


##题解:

裸的最小生成树.
读入邻接矩阵后再建图.
对于已经联通的边,直接把它们的距离赋成0. (即能用就优先用).


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct node{

int left,right,cost;

}road[maxn*maxn];

int cmp(node x,node y) {return x.cost<y.cost;}

int p[maxn],m,n;

int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}

int kruskal()

{

int ans=0;

for(int i=1;i<=n;i++) p[i]=i;

sort(road+1,road+m+1,cmp);

for(int i=1;i<=m;i++)

{

int x=find(road[i].left);

int y=find(road[i].right);

if(x!=y)

{

ans+=road[i].cost;

p[x]=y;

}

}

return ans;

}

int dis[maxn][maxn];

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%d", &n) != EOF)
{
m = 0;
memset(road,0,sizeof(road)); for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%d", &dis[i][j]);
}
} int q; cin >> q;
while(q--) {
int x,y; scanf("%d %d", &x,&y);
dis[x][y] = dis[y][x] = 0;
} for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
road[++m].left = i;
road[m].right = j;
road[m].cost = dis[i][j];
}
} int ans=kruskal(); printf("%d\n", ans);
} return 0;

}

POJ 2421 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 (最小生成树)

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

  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(最小生成树)

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

  6. [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads

    给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...

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

    题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...

  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 解题报告

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

随机推荐

  1. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  2. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...

  3. Android仿iPhone晃动撤销输入功能(微信摇一摇功能)

    重力传感器微信摇一摇SensorMannager自定义alertdialogSensorEventListener 很多程序中我们可能会输入长文本内容,比如短信,写便笺等,如果想一次性撤销所有的键入内 ...

  4. 用socket方式传输Image和Sound文件

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Obje ...

  5. 多线程程序设计学习(5)balking模式和timed模式

    Balking[返回模式]timed[超时模式]一:balking pattern的参与者--->GuardedObject(被警戒的对象) --->该模式的角色:模拟修改警戒对象的线程, ...

  6. liux下ftp链接服务器的常用命令

    FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用 FTP,都会遇到大量的FTP内部命令.熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍 ...

  7. 一天一个Java基础——序列化

    1.概念 Java的“对象序列化”能将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象的时候,能把这些byte数据恢复出来,并据此重新构建那个对象. 对象序列化能实 ...

  8. 【字符串处理】HDOJ-1020-Encoding

    [题目链接:HDOJ-1020] 相邻字符,两两比较. #include<cstdio> #include<cstring> ; char sr[MAXN]; int main ...

  9. 【转】WPS文档怎么清除格式

    原文网址:http://jingyan.baidu.com/article/c74d600060dc4a0f6a595d21.html 我们知道有时候如果我们粘贴了别的地方的文字到自己的文档里来,就会 ...

  10. Oracle表操作 (未完待续)

    1. Oracle 中将一个表中数据导入到另外一个表的方法 insert into scd_data_201007 select * from analog_data_201007 ; 2. 纵表转换 ...