1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

題目信息

1072. Gas Station (30)

時間限制200 ms
內存限制65536 kB
代碼長度限制16000 B

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 (<= 10^3), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 10^4), 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

解題思路

把房子和站點放在一起當做節點,其中站點放在n+1 到 n + m之中,依次對每個站點dijkstra求最短路,同時維護全局最優值即可

AC代碼

#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int n, m, k, ds, a, b, d;
map<int, map<int, int> > mp;
int maxLeng = -1, sum = 0x7fffffff, resId = -1;

void dijkstra(int id)
{
    vector<int> v(n + m + 1, 0x7fffffff); //dijkstra中記錄起點到各點的最短距離
    vector<bool> vis(n + m + 1, false); //是否訪問
    v[id] = 0;
    int cnt = n + m; //需要訪問的點個數
    while (cnt--){
        int mx = 0x7fffffff, t = 0;
        for (int i = 1; i <= n + m; ++i){
            if (!vis[i] && v[i] <= mx){
                mx = v[i];
                t = i;
            }
        }
        if (t <= n && v[t] > ds) break; //超過最大輻射範圍退出,一定注意此處的t <= n
        vis[t] = true;
        for (map<int, int>::iterator it = mp[t].begin(); it != mp[t].end(); ++it){
            v[it->first] = min(v[it->first], v[t] + it->second);
        }
    }
    if (cnt < 0){ //都在輻射範圍內
        int minValue = *min_element(v.begin() + 1, v.begin() + n + 1); //最小值
        if (minValue > maxLeng){ //比全局最小值小則更新
            maxLeng = minValue;
            sum = accumulate(v.begin() + 1, v.begin() + n + 1, 0);
            resId = id;
        }else if (minValue == maxLeng){
            int t = accumulate(v.begin() + 1, v.begin() + n + 1, 0);
            if (t < sum){ //最小值與全局相同時若和更小則更新
                resId = id;
                sum = t;
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d", &n, &m, &k, &ds);
    char s1[15], s2[15];
    for (int i = 0; i < k; ++i){
        scanf("%s%s%d", s1, s2, &d);
        sscanf((s1[0] == 'G') ? s1+1:s1, "%d", &a);
        sscanf((s2[0] == 'G') ? s2+1:s2, "%d", &b);
        a += (s1[0] == 'G') ? n : 0;
        b += (s2[0] == 'G') ? n : 0;
        mp[a][b] = mp[b][a] = d;
    }
    for (int i = 1; i <= m; ++i){
        dijkstra(n + i);
    }
    if (resId == -1){
        printf("No Solution\n");
    }else{
        printf("G%d\n%0.1f %0.1f\n", resId - n, 1.0 * maxLeng, 1.0 * sum / n);
    }
    return 0;
}

個人遊戲推廣:
《10雲方》與方塊來次消除大戰!

發佈了184 篇原創文章 · 獲贊 9 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章