/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2012 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=1102 Name : 1102 Constructing Roads Date : Tuesday, February 7, 2012 Time Stage : half an hour Result: 5325772 2012-02-07 15:30:46 Accepted 1102 15MS 212K 2023 B C++ pyy 5325749 2012-02-07 15:27:05 Time Limit Exceeded 1102 1000MS 184K 2016 B C++ pyy Test Data : Review : //----------------------------------------------------------------------------*/ #include <cstdio> #include <stdlib.h> #include <string.h> #include <algorithm> using namespace std ; #define MEM(a, v) memset (a, v, sizeof (a)) // a for address, v for value #define max(x, y) ((x) > (y) ? (x) : (y)) #define min(x, y) ((x) < (y) ? (x) : (y)) #define INF (0x3f3f3f3f) #define MAXN (103) #define MAXE (MAXN*(MAXN-1)/2) #define DEBUG /##/ int n, q ; int map[MAXN][MAXN], dist[MAXN], used[MAXN] ; int Prim() { int i, j, sum, iMinPath, MinPath ; // 一开始把1放入集合A中,然后求集合A到集合B各点的距离 for (i = 1 ; i <= n ; ++i) dist[i] = map[1][i] ; MEM(used, 0) ; sum = 0 ; used[1] = true ; // 标记集合A中的点 for (i = 1 ; i <= n-1 ; ++i) { MinPath = INF ; // 求出集合A到集合B中各点距离的最小值 for (j = 1 ; j <= n ; ++j) if (!used[j] && dist[j] < MinPath) { MinPath = dist[j] ; iMinPath = j ; } // 将集合B中的点并入集合A中 used[iMinPath] = true ; // 新点加入意味着新的路径产生了 sum += MinPath ; // 由于新点的加入,集合A到集合B中各点的距离可能会变化, // 所以要及时更新 for (j = 1 ; j <= n ; ++j) if (!used[j] && dist[j] > map[iMinPath][j]) dist[j] = map[iMinPath][j] ; } return sum ; } int main() { int i, j ; int x, y ; while (scanf("%d", &n) != EOF) // 一开始忘了判断 EOF 了,= =! { MEM (map, INF) ; for (i = 1 ; i <= n ; ++i) for (j = 1 ; j <= n ; ++j) scanf ("%d", &map[i][j]) ; scanf ("%d", &q) ; for (i = 1 ; i <= q ; ++i) { scanf ("%d%d", &x, &y) ; map[x][y] = map[y][x] = 0 ; // 已建成的路就不用再建了 } printf ("%d\n", Prim()) ; } return 0 ; }