标签:
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.
Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.
Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.
Output Specification:
For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.
Sample Input 1:4 3 11 5 1 2 2 1 4 2 1 G1 4 1 G2 3 2 3 2 2 G2 1 3 4 2 3 G3 2 4 G1 3 G2 G1 1 G3 G2 2Sample Output 1:
G1 2.0 3.3Sample Input 2:
2 1 2 10 1 G1 9 2 G1 20Sample Output 2:
No Solution
#include<iostream>
#include<map>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#pragma warning(disable:4996)
#define INF 1<<30
using namespace std;
int town[1021][1021] = { 0 };
int mark[1021];
map<string, int> loc;
int n, m, k;
double ds;
int locCnt = 0;
int minDis[12] = { 0 }, totalDis[12] = { 0 };
int minDisTemp = INF;
int outRangeMark[12] = { 0 };
int dist[1021] = { 0 };
int Str2Int(string s) {
if (loc[s] == 0) {
locCnt++;
loc[s] = locCnt;
return locCnt;
}
else
return loc[s];
}
string Int2Str(int a) {
for (auto &p : loc) {
if (p.second == a)
return p.first;
}
}
void dij(int index) {
int u,v,min=INF;
for (int i = 1; i < n + m; i++) {
min = INF;
for (int j = 1; j <= n + m; j++) {
if (dist[j] < min&&mark[j] == 0) {
u = j;
min = dist[j];
}
}
mark[u] = 1;
for (v = 1; v <= n + m; v++) {
if (town[u][v]!=INF&&dist[v] > dist[u] + town[u][v]) {
dist[v] = dist[u] + town[u][v];
}
}
}
}
int main(void) {
freopen("Text.txt", "r", stdin);
cin >> n >> m >> k >> ds;
for (int j = 0; j <= n + m; j++)
for (int z = 0; z <= n + m; z++) {
if (z == j)
town[z][j] = 0;
else
town[j][z] = town[z][j] = INF;
}
for (int i = 0; i < k; i++) {
string s1, s2;
int dis;
char a[6], b[6];
scanf("%s %s %d", a, b, &dis);
int q = 0;
while (a[q]!=0)
{
s1 += a[q];
q++;
}
q = 0;
while (b[q] != 0)
{
s2 += b[q];
q++;
}
//cin >> s1 >> s2 >> dis;
town[Str2Int(s1)][Str2Int(s2)] = dis;
town[Str2Int(s2)][Str2Int(s1)] = dis;
}
for (int i = 1; i <= m; i++) {
memset(mark, 0, sizeof(mark));
memset(dist, 0, sizeof(dist));
minDisTemp = INF;
string start = "G";
start += i + ‘0‘;
for (int j = 1; j <= n + m; j++)
dist[j] = town[loc[start]][j];
mark[loc[start]] = 1;
dij(loc[start]);
for (int j = 1; j <= n + m; j++) {
if (Int2Str(j)[0] != ‘G‘) {
if (1.0*dist[j] > ds)
outRangeMark[i] = 1;
if (dist[j] < minDisTemp)
minDisTemp = dist[j];
totalDis[i] += dist[j];
minDis[i] = minDisTemp;
}
}
}
int maxIndex = -1, max = -1;
for (int p = 1; p <= m; p++) {
if (minDis[p] > max&&outRangeMark[p]==0) {
max = minDis[p];
maxIndex = p;
}
else if (minDis[p] == max&&totalDis[p] < totalDis[maxIndex] && outRangeMark[p] == 0) {
maxIndex = p;
}
}
if (maxIndex == -1)
cout << "No Solution";
else {
string start = "G";
start += maxIndex + ‘0‘;
cout << start << endl;
printf("%.1f %.1f", 1.0*minDis[maxIndex], 1.0*totalDis[maxIndex] / n);
}
return 0;
}
#include<iostream>
#include<map>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<string>
#pragma warning(disable:4996)
#define INF 99999999
using namespace std;
int town[1011][1011] = { 0 };
int mark[1011];
map<string, int> loc;
int n, m, k, ds;
int locCnt = 0;
int minDis[11] = { 0 }, totalDis[11] = { 0 };
int minDisTemp = INF;
int outRangeMark[11] = { 0 };
int Str2Int(string s) {
if (loc[s] == 0) {
locCnt++;
loc[s] = locCnt;
return locCnt;
}
else
return loc[s];
}
string Int2Str(int a) {
for (auto &p : loc) {
if (p.second == a)
return p.first;
}
}
void dfs(int startPomt, int endpoint,int length) {
if (length >= minDisTemp)
return;
if (startPomt == endpoint) {
if (length < minDisTemp) {
minDisTemp = length;
}
}
else {
for (int i = n + m; i >=1; i--) {
if (town[startPomt][i] != 0 && mark[i] == 0/*&&Int2Str(i).length()==1*/) {
mark[i] = 1;
dfs(i, endpoint, length + town[startPomt][i]);
mark[i] = 0;
}
}
}
}
int main(void) {
//freopen("Text.txt", "r", stdin);
cin >> n >> m >> k >> ds;
for (int i = 0; i < k; i++) {
string s1, s2;
int dis;
char a[3], b[3];
scanf("%s %s %d", a, b, &dis);
int q = 0;
while (a[q]!=0)
{
s1 += a[q];
q++;
}
q = 0;
while (b[q] != 0)
{
s2 += b[q];
q++;
}
//cin >> s1 >> s2 >> dis;
town[Str2Int(s1)][Str2Int(s2)] = dis;
town[Str2Int(s2)][Str2Int(s1)] = dis;
}
/*for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
minDisTemp = INF;
string start = "G";
start += i + ‘0‘ + 1;
string end;
end += j + ‘0‘ + 1;
mark[loc[start]] = 1;
dfs(loc[start], loc[end], 0);
mark[loc[start]] = 0;
if (minDis[i] == 0)
minDis[i] = minDisTemp;
else if (minDis[i] != 0 && minDisTemp < minDis[i])
minDis[i] = minDisTemp;
if (minDisTemp > ds) {
outRangeMark[i] = 1;
break;
}
totalDis[i] += minDisTemp;
}
}
int maxIndex = -1, max = 0;
for (int p = 0; p < m; p++) {
if (minDis[p] > max&&outRangeMark[p]==0) {
max = minDis[p];
maxIndex = p;
}
else if (minDis[p] == max&&totalDis[p] < totalDis[maxIndex] && outRangeMark[p] == 0) {
maxIndex = p;
}
}
if (maxIndex == -1)
cout << "No Solution";
else {
string start = "G";
start += maxIndex + ‘0‘ + 1;
cout << start << endl;
printf("%.1f %.1f", 1.0*minDis[maxIndex], 1.0*totalDis[maxIndex] / n);
}
*/
return 0;
}
标签:
原文地址:http://www.cnblogs.com/zzandliz/p/5023230.html