1. 程式人生 > >PAT 1072. Gas Station (30)(多個點的最短路徑)

PAT 1072. Gas Station (30)(多個點的最短路徑)

官網

1072. Gas Station (30)

時間限制
200 ms
記憶體限制
65536 kB
程式碼長度限制
16000 B
判題程式
Standard
作者
CHEN, Yue
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

題目大意


  • 1.給出幾個候選加油站,求使:
  • 1.到所有居住點的最近的人家越遠越好。
  • 2.到每家每戶的平均距離越小越好。
  • 3.如果還相同就取序號最小的。

解題思路

  • 1.對每個加油站dijkstra即可。

AC程式碼

#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<algorithm>
#include<map>
#include<sstream>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
int n, m, k, ds;
//路徑的題,road初始為0
int road[1011][1011];
int visited[1011];
//dist初始為INF
int dist[1011];
void Dijkstra(int now){
    visited[now] = 1;
    for (int i = 1; i <= n + m; i++){
        if (road[now][i] > 0 && dist[now] + road[now][i]<dist[i])
        {
            dist[i] = dist[now] + road[now][i];
        }
    }
    //找到距離距離最短的未訪問過的點去訪問
    int minDist = INF, minId = -1;
    for (int i = 1; i <= n + m; i++){
        if (visited[i] != 1 && dist[i] < minDist){
            minDist = dist[i];
            minId = i;
        }
    }
    if (minId != -1){
        Dijkstra(minId);
    }
}
struct station{
    int id;
    double sum, per, minDist;
    station(int _id,double _s, double _p, double _m) :id(_id),sum(_s), per(_p), minDist(_m){}
};
bool cmp(const station &a, const station &b){
    if (a.minDist != b.minDist){
        return a.minDist > b.minDist;
    }
    else if (a.per != b.per){
        return a.per < b.per;
    }
    else{
        return a.id < b.id;
    }
}
int main(){
    cin >> n >> m >> k >> ds;
    string w, t; int l, r, v; 
    for (int i = 0; i < k; i++){
        cin >> w >> t >> v;
        if (w[0] == 'G'){
            stringstream ss;
            w = w.substr(1);
            ss << w; ss >> l;
            l += n;
        }
        else{
            stringstream ss;
            ss << w; ss >> l;
        }
        if (t[0] == 'G'){
            stringstream ss;
            t = t.substr(1);
            ss << t;
            ss >> r;
            r += n;
        }
        else{
            stringstream ss;
            ss << t; ss >> r;
        }
        road[l][r] = road[r][l] = v;
    }
    vector<station> keep;
    for (int j = 1; j <= m; j++){
        fill(dist, dist + 1011, INF);
        fill(visited, visited + 1011, 0);
        dist[n + j] = 0;
        Dijkstra(n + j);
        double sum = 0;
        bool isInRange = true;
        double minDist = INF;
        for (int i = 1; i <= n; i++){
            //範圍內
            if (dist[i] > ds){
                isInRange = false;
                break;
            }
            //最小距離越大越好
            if (dist[i] > 0 && dist[i] < minDist){
                minDist = dist[i];
            }
            //平均值越小越好
            sum += dist[i];
        }
        if (isInRange){
            keep.push_back(station(j, sum, sum / (double)n, minDist));
        }
    }
    if (keep.empty()){
        cout << "No Solution" << endl;
    }
    else{
        sort(keep.begin(), keep.end(), cmp);
        printf("G%d\n", keep[0].id);
        printf("%.1f %.1f\n", keep[0].minDist, keep[0].per);
    }
    return 0;
}

相關推薦

PAT 1072. Gas Station (30)(路徑)

官網 1072. Gas Station (30) 時間限制 200 ms 記憶體限制 65536 kB 程式碼長度限制 16000 B 判題程式 Standard 作者 CHEN, Yue A gas station has to

PAT 1072 Gas Station (30)

esp 狀態 solution sam 不知道 specific 解決方案 sep ans A gas station has to be built at such a location that the minimum distance between the stat

路徑問題

問題描述: 給定帶權又向圖G=(V,E),對任意頂點Vi和Vj,求頂點Vi到Vj的最短路徑長度? 分析: Floyd演算法程式碼很簡單,但是理解起來有一定的難度。網上有很多解釋方法,我自己的思想還

PAT1072. Gas Station (30)【dijkstra演算法】

題目描述 A gas station has to be built at such a location that the minimum distance between the station and any of the residential hou

1072 Gas Station30 分)(路徑

  #include<bits/stdc++.h> using namespace std; const int N=1e3+100; int n,m,k,Ds; int mp[N][N]; int dis[N]; int vis[N]; int inf=0x3f3f3f3

PAT A1072 Gas Station30 分)-------圖路徑---比較難點的題

總結: 1.這道題用了dijstra演算法,關鍵是開始對G1非數字的處理即Gi處理成i+n;我最後一個測試點開始沒過就是因為用s.size()判斷輸入為數字還是G2,但是其實資料n+m是大於99的 程式碼: #include<iostream> #include<alg

PAT 1072. Gas Station

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 possi

hdu2066 dijkstra終點求路徑

dijkstra演算法的思路: (1)找到最短距離已經確定的頂點,從它出發更新相鄰頂點的最短距離 (2)此後不再關心(1)中最短距離已經確定的頂點 最開始時只有起點的最短距離是確定的,而在未使用過的頂點中,距離d[i]最小的頂點就是最短距離已經確定的頂點,在不存在負邊的情況下d[i]不會

hdu2066 dijkstra終點求路徑

dijkstra演算法的思路: (1)找到最短距離已經確定的頂點,從它出發更新相鄰頂點的最短距離 (2)此後不再關心(1)中最短距離已經確定的頂點 最開始時只有起點的最短距離是確定的,而在未使用過的頂點中,距離d[i]最小的頂點就是最短距離已經確定的頂點,在不存在負邊的

PAT 1111 —— Online Map(Dijkstra單源路徑

題目注意事項: 迪傑斯特拉兩次: 第一次求最短的路程長度【順便記錄時間用時,以判斷長度相同的時候選擇時間短的】 第二次求最短的路程時間【順便記錄到達每個點的最短路徑所需要的點數,以判斷時間相同時候選擇點數少的】【注意:第一次求得時間記錄需要重新初始化以免錯誤】

程式設計基礎30 tips 圖的路徑(二)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over th

單源路徑Dijkstra和Bellmanford

單源點最短路徑Dijkstra和Bellmanford Dijikstra演算法精髓在於維護了兩個集合s和v,而這也是實際程式設計中實現起來比較頭疼的事情,我的做法是把每個節點都設定成一個結構體,裡面有個狀態變數,為‘s’則意味著它在S集合當中。 另外還有很多小的細節需要注意 - 在

1030 Travel Plan (30 分)(路徑 and dfs)

#include<bits/stdc++.h> using namespace std; const int N=510; const int inf=0x3f3f3f3f; int mp[N][N]; bool vis[N]; int dis[N]; int n,m,s,D; int

單源路徑演算法:Dijkstra演算法

背景知識 圖簡介 圖由節點和邊組成,邊有方向的圖稱為有向圖,邊沒有方向的圖稱為無向圖,最短路徑演算法裡可以把無向圖視為雙向連線的有向圖。 邊有權重的圖稱為有權圖,邊沒有權重的圖稱為無權圖,無權圖可以視為邊的權重均為1的圖。 單源點最短路徑 給定

Dijkstra演算法--一個到其餘路徑

Dijkstra演算法 要求 求一個點(源點)到其餘各個頂點的最短路徑。 思路 先將源點到其餘各個點的路徑列出來dis[],找到最小值,這個最小值就是源點到這一點u的最短路徑,並標記已經找出,再以這個點開始,依次遍歷到其它點v,如果這個點u到

單源路徑Dijkstra的java實現

<pre name="code" class="java">public class Dijkstra { public static void main(String[] args) { } public static int[] dijkstra(int[][] weight

PAT 備考——圖論演算法(二)路徑

最短路徑演算法是PAT甲級考試常考演算法,具體說來,最短路徑包括Dijkstra演算法、Floyd演算法,其餘的Bellman-Ford和SPFA基本不會考(參《演算法筆記》胡凡,曾磊著) 目錄 一、最短路徑基本概念與問題分類 1.基本概念 2.問題分類 二、D

Dijkstra演算法(單元路徑

Dijkstra演算法解決圖中某特定點到其他點的最短路徑。 迪傑斯塔拉(Dijkstra)演算法思想: 按路徑長度遞增的次序產生最短路徑的演算法。設集合S存放已經找到最短路徑的頂點,V為所有節點的集合,S的初始狀態只包含源點V0,對Vi∈V-S。 假設從源點V0到Vi的有向

PAT甲級 1072 Gas Station (30 分)Dijkstra

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 res

PAT (Advanced Level) Practice 1072 Gas Station30 分)

程式設計題 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 t