您的位置:首页 > 其它

hdu 1102 Constructing Roads(Prim最小生成树)

2010-09-15 16:34 337 查看
/*
Author: ACb0y
Date: 2010-9-14
Type: MST
ProblemId: hdu 1102 Constructing Roads
Result: AC
*/
#include <iostream>
using namespace std;
#define inf 99999999

int n;
int q;
int g[110][110];
int vis[110];
int d[110];

void MST() {
int i, j;
for (i = 1; i <= n; i++) {
d[i] = g[1][i];
}
memset(vis, 0, sizeof(vis));
d[1] = 0;
int ans = 0;
for (i = 1; i <= n; i++) {
int Min = inf;
int pos = -1;
for (j = 1; j <= n; j++) if (!vis[j]) {
if (d[j] < Min) {
Min = d[j];
pos = j;
}
}
vis[pos] = 1;
ans += Min;
for (j = 1; j <= n; j++) if (!vis[j]){
if (g[pos][j] < d[j]) {
d[j] = g[pos][j];
}
}
}
cout << ans << endl;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1102.txt", "r", stdin);
#endif
while (cin >> n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
int temp;
cin >> g[i][j];
}
}
cin >> q;
for (i = 1; i <= q; i++) {
int a, b;
cin >> a >> b;
g[a][b] = g[b][a] = 0;
}
MST();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  2010