链接:http://poj.org/problem?id=2421
Constructing Roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23711 | Accepted: 10166 |
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
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
题意:有n个村庄,编号为1 ,2 ,3 ,,,n ,然后求最小生成树。以矩阵的形式给边和权值,转换一下就可以了。
另外有q条边已经连接,要把权值变为0;
代码:main函数内有些繁琐
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<functional>
#include<map>
const int maxn = 60000 + 10;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
struct node {
int x, d;
node(int x = 0, int y = 0):x(x),d(y){}
};
vector<node>G[maxn];
int n;
int s[maxn];
int q;
int a, b;
bool intree[maxn];
int mindis[maxn];
int ans;
void init() {
for (int i = 0; i < n; i++) {
intree[i] = false;
G[i].clear();
mindis[i] = INF;
}
}
void Prim(int s) {
ans = 0;
int vex, tempMin, addNode = 0;
intree[s] = true;
for (int i = 0; i < G[s].size(); i++) {
vex = G[s][i].x;
mindis[vex] = G[s][i].d;
}
for (int nodeNum = 0; nodeNum < n - 1; nodeNum++) {
tempMin = INF;
for (int i = 0; i < n; i++) {
if (intree[i] == false && mindis[i] < tempMin) {
tempMin = mindis[i];
addNode = i;
}
}
// cout << addNode <<" "<<tempMin<< endl;
intree[addNode] = true;
ans += tempMin;
for (int i = 0; i < G[addNode].size(); i++) {
if (intree[i] == false && mindis[i]>G[addNode][i].d)
mindis[i] = G[addNode][i].d;
}
}
}
int main() {
while (scanf("%d", &n) != EOF) {
init();
for (int i = 0; i < n; i++) {
int k;
for (int j = 0; j < n; j++) {
scanf("%d", &s[j]);
if (s[j] == 0)k = j;
}
for (int j = 0; j < n; j++) {
G[k].push_back(node(j, s[j]));
}
}
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d %d", &a, &b);
G[a - 1][b - 1].d = 0;
G[b - 1][a - 1].d = 0;
}
Prim(0);
printf("%d\n", ans);
}
return 0;
}