HDU1102--Constructing Roads
来源:互联网 发布:华梦汽车网络 编辑:程序博客网 时间:2024/06/05 18:42
Problem 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.
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.
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
30 990 692990 0 179692 179 011 2
Sample Output
179
首先上Kruskal...
#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define maxn 108#define inf 0x3f3f3f3fint W[maxn][maxn];bool vis[maxn];int dis[maxn];int main(){int n;while(scanf("%d",&n)!=EOF && n){memset(vis,0,sizeof(vis));memset(dis,0x3f,sizeof(dis));memset(W,0x3f,sizeof(W));for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int cost;scanf("%d",&cost);W[i][j] = W[j][i] = cost;}}int m;scanf("%d",&m);for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);W[u][v]=W[v][u]=0;}dis[1] = 0;int cost = 0;for(int i=1;i<=n;i++){int minlen = inf;int nowv;for(int j=1;j<=n;j++){if(!vis[j] && dis[j] < minlen){minlen = dis[j];nowv = j;}}cost += minlen;vis[nowv] = 1;for(int j=1;j<=n;j++){if(!vis[j] && W[nowv][j] < dis[j]){dis[j] = W[nowv][j];}}}printf("%d\n",cost);}return 0;}
- Constructing Roads hdu1102
- Constructing roads(HDU1102)
- hdu1102 Constructing Roads
- HDU1102 Constructing Roads
- Constructing Roads hdu1102 kruskal
- HDU1102--Constructing Roads
- hdu1102 Constructing Roads
- hdu1102:Constructing Roads(prime)
- HDU1102 Constructing Roads【Prim】
- HDU1102---Constructing Roads
- HDU1102 Constructing Roads
- HDU1102 Constructing Roads
- HDU1102-Constructing Roads
- HDU1102&&POJ2421 Constructing Roads
- HDU1102 Constructing Roads (Kruskal)
- hdu1102 Constructing Roads
- hdu1102 Constructing Roads(prim)
- 7.22 T hdu1102 Constructing Roads
- 特殊SQL1
- 计算机专业相关课程学习资料大汇总 优质学习资料我的百度网盘免费下载
- OBJECT-C 对象初始化
- 关于HibernateUtil的一个封装
- 软件开发嵌入式方向+ ACM程序设计/C语言/算法与数据结构+ C++/C#语言+汇编语言+人工智能 学习资料免费下载
- HDU1102--Constructing Roads
- WinNT.h
- IE6下使用CSS定义DIV高度行之有效的办法
- 黑马程序员_字符串
- linux od命令
- 软件开发JAVA基础+JavaWeb方向+算法与数据结构学习资料我的百度网盘免费下载+优质+超全
- 【js】与获取元素相关
- Redis简介
- 298D Fish Weight