HDU1102 Constructing Roads
来源:互联网 发布:安卓js css本地打包 编辑:程序博客网 时间:2024/06/11 19:41
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.
InputThe 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.
OutputYou 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 Input30 990 692990 0 179692 179 011 2
Sample Output179
#include <iostream>
#include<cstring>
const int N = 100+5;
const int INF = 65535;
int low[N+1];
int dist[N+1][N+1];
bool vis[N+1] = {false};
int mina;
using namespace std;
int main()
{
int n;
while(cin >> n)
{
memset(vis,false,sizeof(vis));
int ans = 0;
for(int i=1; i<=N; i++)
{
for(int j=1; j<=N; j++)
{
if(i == j)
dist[i][j] = 0;
else
dist[i][j] = INF;
}
}
int m;
int v;
int t;
m = n*(n-1)/2;
for(int i=1; i<=n;i++)
{
for(int j=1; j<=n; j++)
{
cin >> t;
dist[i][j] = dist[j][i] = t;
}
}
int q;
int aa,bb;
cin >> q;
for(int i=1; i<=q; i++)
{
cin >> aa >> bb;
dist[aa][bb] = dist[bb][aa] = 0;
}
vis[1] = true;
low[1] = 0;
for(int i=2; i<=n; i++)
{
low[i] = dist[1][i];
}
for(int i=1; i<n; i++)
{
mina = INF;
for(int j=1; j<=n; j++)
{
if(!vis[j] && low[j]<mina)
{
mina = low[j];
v = j;
}
}
ans += mina;
vis[v] = true;
for(int j=1; j<=n; j++)
{
if(!vis[j] && low[j]>dist[v][j])
{
low[j] = dist[v][j];
}
}
}
cout << ans << endl;
}
return 0;
}
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.
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.
30 990 692990 0 179692 179 011 2
179
0 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
- java面向对象思想笔记
- poj 3070
- 32bit程序在64bit操作系统下处理重定向细节
- JDK8新特性--Stream
- MIT6.828 Lab2: 第3部分 Kernel Address Space
- HDU1102 Constructing Roads
- CodeForces 585D Present for Vitalik the Philatelist(容斥原理+gcd)
- ionic安卓默认的tabs修改为底部显示
- LeetCode|Add and Search Word - Data structure design
- 135_Service给Activity发消息
- BNUOJ 51637 Quailty's Life | BZOJ 4587 推箱子
- leetcode.84. Largest Rectangle in Histogram
- ftp安装
- Spark 2.0技术预览:更容易、更快速、更智能