Fire Station
A city is served by a number of fire stations. Some residents have complained that the distance from their houses to the nearest station is too far, so a new station is to be built. You are to choose the location of the fire station so as to reduce the distance to the nearest station from the houses of the disgruntled residents.
The city has up to 500 intersections, connected by road segments of various lengths. No more than 20 road segments intersect at a given intersection. The location of houses and firestations alike are considered to be at intersections (the travel distance from the intersection to the actual building can be discounted). Furthermore, we assume that there is at least one house associated with every intersection. There may be more than one firestation per intersection.
Input
The first line of input contains two positive integers: f,the number of existing fire stations (f <= 100) and i, the number of intersections (i <= 500). The intersections are numbered from 1 to i consecutively. f lines follow; each contains the intersection number at which an existing fire station is found. A number of lines follow, each containing three positive integers: the number of an intersection, the number of a different intersection, and the length of the road segment connecting the intersections. All road segments are two-way (at least as far as fire engines are concerned), and there will exist a route between any pair of intersections.
Output
You are to output a single integer: the lowest intersection number at which a new fire station should be built so as to minimize the maximum distance from any intersection to the nearest fire station.
Sample Input
1 6
2
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
Sample Output
5
Source
题目类型:最短路
算法分析:先使用floyd求出任意两点之间的最短路,然后把每个点到最近消防站的距离算出来,然后从小到大枚举每个点并更新此时每个点到消防站的最短距离,若得到的最大最短距离较小则更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
/************************************************* Author :supermaker Created Time :2016/3/24 12:55:37 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 500 + 66; int edge[maxn][maxn], dis[maxn][maxn], in[maxn], f, n, res[maxn], tres[maxn]; void floyd () { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[i][j] = edge[i][j]; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == k || j == k) continue; if (dis[i][k] < INF && dis[k][j] < INF) dis[i][j] = min (dis[i][j], dis[i][k] + dis[k][j]); } } int main() { //CFF; //CPPFF; while (scanf ("%d%d", &f, &n) != EOF) { for (int i = 1; i <= f; i++) scanf ("%d", &in[i]); for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) { if (i == j) edge[i][j] = 0; else edge[i][j] = INF; } int u, v, w; while (scanf ("%d%d%d", &u, &v, &w) != EOF) { edge[u][v] = edge[v][u] = w; } floyd (); fill (res, res + maxn, INF); for (int i = 1; i <= f; i++) for (int j = 1; j <= n; j++) res[j] = min (res[j], dis[in[i]][j]); int maxval = -INF; for (int i = 1; i <= n; i++) maxval = max (maxval, res[i]); int ans = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) tres[j] = res[j]; for (int j = 1; j <= n; j++) tres[j] = min (tres[j], dis[i][j]); int tmaxval = -INF; for (int j = 1; j <= n; j++) tmaxval = max (tmaxval, tres[j]); if (tmaxval < maxval) { maxval = tmaxval; ans = i; } } printf ("%d\n", ans); } return 0; } |
- « 上一篇:poj1192
- poj3169:下一篇 »