PAT 1072. Gas Station (30)

1072. Gas Station (30)

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 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2 10
1 G1 9
2 G1 20
Sample Output 2:
No Solution

典型的最短路径问题,用一个矩阵记录每一个station到所有的house的距离,然后根据题目的要求找到相应的答案
  1 #include <iostream>
  2 #include <string>
  3 #include <bitset>
  4 #include <iomanip>
  5 #include <algorithm>
  6 
  7 using namespace std;
  8 
  9 const int INF = 0x7fffffff;
 10 int Map[1010][1010];
 11 bitset<1010> visited;
 12 int minLength[10][1010];
 13 int houseNum, stationNum, roadNum, range;
 14 
 15 int ToInt(string s)
 16 {
 17     int res = 0;
 18     for (int i = 0; i < s.size(); i++)
 19         res = res * 10 + s[i] - '0';
 20     return res;
 21 }
 22 
 23 int HashIndex(string p)
 24 {
 25     if (p[0] == 'G')
 26         return ToInt(string(p.begin() + 1, p.end())) - 1;
 27     else
 28         return ToInt(p) - 1 + stationNum;
 29 }
 30 
 31 int main()
 32 {
 33     cin >> houseNum >> stationNum >> roadNum >> range;
 34     for (int i = 0; i < stationNum + houseNum; i++)
 35         for (int j = 0; j < stationNum + houseNum; j++)
 36             Map[i][j] = INF;
 37     for (int i = 0; i < roadNum; i++)
 38     {
 39         string p1, p2;
 40         int dist;
 41         cin >> p1 >> p2 >> dist;
 42         int index1 = HashIndex(p1), index2 = HashIndex(p2);
 43         Map[index1][index2] = Map[index2][index1] = dist;
 44     }
 45     //initial the minLength to INF
 46     for (int i = 0; i < stationNum; i++)
 47         for (int j = 0; j < stationNum + houseNum; j++)
 48             minLength[i][j] = INF;
 49     for (int i = 0; i < stationNum; i++)
 50     {
 51         int start = i;
 52         visited.reset();
 53         //using dijkstra algorithm
 54         //initial the start
 55         minLength[start][start] = 0;
 56         visited.set(start);
 57         int min = start;
 58         while (1)
 59         {
 60             for (int i = 0; i < stationNum + houseNum; i++)
 61             {
 62                 if (Map[min][i] < INF && minLength[start][min] + Map[min][i] < minLength[start][i])
 63                     minLength[start][i] = minLength[start][min] + Map[min][i];
 64             }
 65             int length = INF;
 66             for (int i = 0; i < stationNum + houseNum; i++)
 67             {
 68                 if (!visited[i] && minLength[start][i] < length)
 69                 {
 70                     length = minLength[start][i];
 71                     min = i;
 72                 }
 73             }
 74             visited.set(min);
 75             if (length == INF)
 76                 break;
 77         }
 78     }
 79 
 80     int min[11], max[11], sum[11];
 81     for (int i = 0; i < stationNum; i++)
 82     {
 83         min[i] = *min_element(minLength[i] + stationNum, minLength[i] + stationNum + houseNum);
 84         max[i] = *max_element(minLength[i] + stationNum, minLength[i] + stationNum + houseNum);
 85         sum[i] = 0;
 86         for (int j = stationNum; j < houseNum + stationNum; j++)
 87             sum[i] += minLength[i][j];
 88     }
 89     double maxDist = -INF, minSum;
 90     int maxIndex;
 91     for (int i = 0; i < stationNum; i++)
 92     {
 93         if (max[i] <= range)
 94         {
 95             if (min[i] > maxDist)
 96             {
 97                 maxDist = min[i];
 98                 maxIndex = i;
 99                 minSum = sum[i];
100             }
101             else if (min[i] == maxDist)
102             {
103                 if (sum[i] < minSum)
104                 {
105                     minSum = sum[i];
106                     maxIndex = i;
107                 }
108             }
109         }
110     }
111     
112     if (maxDist == -INF)
113         cout << "No Solution";
114     else
115     {
116         cout << "G" << maxIndex + 1 << endl;
117         printf("%.1f %.1f", maxDist, minSum / houseNum);
118         //cout << hex << setprecision(1) << 1.0*maxDist << " " << 1.0*minSum / houseNum;
119     }
120 
121 }

 

转载于:https://www.cnblogs.com/jackwang822/p/4755158.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值