1072 Gas Station (PAT甲级)

这篇具有很好参考价值的文章主要介绍了1072 Gas Station (PAT甲级)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
const int MAXN = 1011;
const int INF = 999999999;

struct node{
    int id;
    int dist;
    node(int _id, int _dist): id(_id), dist(_dist){}
};

struct station{
    std::string id;
    double minDist;
    double avgDist;
    station(std::string _id, double _minDist, double _avgDist):
    id(_id), minDist(_minDist), avgDist(_avgDist){}
};

int N, M, K, ds, dist, m, n;
std::string s, a, b;
std::map<std::string, int> mp;
std::vector<std::vector<node>> adj;
std::vector<station> ans;

void dijkstra(int k){
    bool visited[MAXN];
    int d[MAXN];
    int pivot, minD, totalDist;
    std::fill(visited, visited + N + M + 1, false);
    std::fill(d, d + M + N + 1, INF);
    d[k] = 0;
    for(int i = 0; i < M + N; ++i){
        pivot = -1;
        minD = INF;
        for(int j = 1; j <= M + N; ++j){
            if(!visited[j] && d[j] < minD){
                minD = d[j];
                pivot = j;
            }
        }
        if(pivot == -1){
            break;
        }
        visited[pivot] = true;
        for(int j = 0; j < adj[pivot].size(); ++j){
            if(!visited[adj[pivot][j].id] && d[pivot] + adj[pivot][j].dist < d[adj[pivot][j].id]){
                d[adj[pivot][j].id] = d[pivot] + adj[pivot][j].dist;
            }
        }
    }
    minD = INF;
    totalDist = 0;
    for(int i = 1; i <= N; ++i){
        if(d[i] > ds){
            return;
        }
        if(d[i] < minD){
            minD = d[i];
        }
        totalDist += d[i];
    }
    std::string str = "G" + std::to_string(k - N);
    ans.push_back(station(str, minD * 1.0, totalDist * 1.0 / N));
}

bool cmp(station &a, station &b){
    return a.minDist != b.minDist ? a.minDist > b.minDist : (a.avgDist != b.avgDist ? a.avgDist < b.avgDist : a.id < b.id);
}

int main(){
    scanf("%d %d %d %d", &N, &M, &K, &ds);
    for(int i = 1; i <= M; ++i){
        s = "G" + std::to_string(i);
        mp[s] = N + i;
    }
    adj.resize(N + M + 1);
    for(int i = 0; i < K; ++i){
        std::cin >> a >> b >> dist;
        if(mp[a] == 0){
            m = std::stoi(a);
        } else{
            m = mp[a];
        }
        if(mp[b] == 0){
            n = std::stoi(b);
        } else{
            n = mp[b];
        }
        adj[m].push_back(node(n, dist));
        adj[n].push_back(node(m, dist));
    }
    for(int i = N + 1; i <= N + M; ++i){
        dijkstra(i);
    }
    if(ans.empty()){
        printf("No Solution");
        return 0;
    }
    sort(ans.begin(), ans.end(), cmp);
    printf("%s\n", ans[0].id.c_str());
    printf("%.1f %.1f", ans[0].minDist, ans[0].avgDist);
    return 0;
}

题目如下:

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.文章来源地址https://www.toymoban.com/news/detail-494271.html

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

到了这里,关于1072 Gas Station (PAT甲级)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(21)
  • 1111 Online Map (PAT甲级)

    这道题我读题不仔细导致踩了个大坑,一个测试点过不了卡了好几个小时:第二个dijkstra算法中,题目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我却想当然地认为在fastest path is not unique的时候,判断标准是最短距离…… Input our

    2024年02月07日
    浏览(30)
  • 菜鸟记录PAT甲级1003--Emergency

    久违的PAT,由于考研408数据结构中有一定需要,同时也是对先前所遗留的竞赛遗憾进行一定弥补 ,再次继续PAT甲级1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    浏览(24)
  • 1114 Family Property (PAT甲级)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    浏览(38)
  • 1021 Deepest Root (PAT甲级)

    1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)_柳婼的博客-CSDN博客 柳婼的解法在这里,两次dfs,还是挺好玩的。 我的解法比较暴力,就是先用并查集算连通分量(这个其实还是dfs来算会更方便),如果只有一个连通分量,那deepest root一定在仅有一条arc的

    2024年02月15日
    浏览(22)
  • 1083 List Grades (PAT甲级)

    Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. Input Specification: Each input file contains one test case. Each case is given in the following format: where  name[i]  and  ID[i

    2024年02月08日
    浏览(41)
  • PAT 甲级【1007 Maximum Subsequence Sum】

    本题是考察动态规划与java的快速输入: max[i]表示第i个结尾的最大的连续子串和。b begin[i]表示第[begin[i],i]为最大和的开始位置 超时代码: 未超时: 能用动态规划解决的问题,需要满足三个条件:最优子结构,无后效性和子问题重叠。 具有最优子结构也可能是适合用贪心的

    2024年02月08日
    浏览(31)
  • 1074 Reversing Linked List (PAT甲级)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月09日
    浏览(32)
  • PAT 甲级1005【1005 Spell It Right】

    用JAVA可以用BigInteger解决。       太长不看版:结尾自取模板…… 高精度计算(Arbitrary-Precision Arithmetic),也被称作大整数(bignum)计算,运用了一些算法结构来支持更大整数间的运算(数字大小超过语言内建整型)。 高精度问题包含很多小的细节,实现上也有很多讲究。

    2024年02月08日
    浏览(66)
  • 菜鸟记录:c语言实现PAT甲级1003--Emergency

    久违的PAT,由于考研408数据结构中有一定需要,同时也是对先前所遗留的竞赛遗憾进行一定弥补 ,再次继续PAT甲级1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包