/*------------------------------------------------------------------------------ * problem023.cc: Camp Station (10.5.3) **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #include #include #include #include using namespace std; /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define NVERTICES 500 /*------------------------------------------------------------------------------ * Data Structures **----------------------------------------------------------------------------*/ typedef struct { int weight[NVERTICES + 1][NVERTICES + 1]; int nvertices; int nedges; int station[NVERTICES + 1]; int nstations; } Graph; /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ static void initialize_graph(Graph &g) { int i, j; for (i = 1; i <= NVERTICES; i++) for (j = 1; j <= NVERTICES; j++) g.weight[i][j] = INT_MAX / 2; memset(g.station, 0, sizeof(int) * (NVERTICES + 1)); } static int read_graph(Graph &g) { int i, s, x, y, w; if (cin >> g.nstations >> g.nvertices >> g.nedges) { initialize_graph(g); for (i = 1; i <= g.nstations; i++) { cin >> s; g.station[s] = 1; } for (i = 1; i <= g.nvertices; i++) g.weight[i][i] = 0; for (i = 1; i <= g.nedges; i++) { cin >> x >> y >> w; g.weight[x][y] = w; g.weight[y][x] = w; } return (g.nvertices); } return (0); } /** Floyd's algorithm as outlined by Skiena and Revilla */ static void compute_all_pairs_shortest_path(Graph &g) { int i, j, k, through_k; for (k = 1; k <= g.nvertices; k++) for (i = 1; i <= g.nvertices; i++) for (j = 1; j <= g.nvertices; j++) { through_k = g.weight[i][k] + g.weight[k][j]; if (through_k < g.weight[i][j]) g.weight[i][j] = through_k; } } static int compute_distance_to_stations(Graph &g) { int i, j, dtotal, dmin; dtotal = 0; for (i = 1; i <= g.nvertices; i++) { dmin = INT_MAX; for (j = 1; j <= g.nvertices; j++) if (g.station[j] && g.weight[i][j] < dmin) dmin = g.weight[i][j]; dtotal += dmin; } return (dtotal); } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { Graph g; int i, d, min_station, min_distance; while (read_graph(g)) { compute_all_pairs_shortest_path(g); min_distance = INT_MAX; for (i = 0; i <= g.nvertices; i++) { if (g.station[i]) { continue; } else { g.station[i] = 1; d = compute_distance_to_stations(g); if (d < min_distance) { min_distance = d; min_station = i; } g.station[i] = 0; } } cout << min_station << endl; } return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=cpp **----------------------------------------------------------------------------*/