美文网首页
PAT甲级A1072---最短路径

PAT甲级A1072---最短路径

作者: 1nvad3r | 来源:发表于2020-09-09 16:48 被阅读0次

1072 Gas Station (30分)

1072
分析:

先处理加油站的编号问题,把加油站的字母G去掉,然后加上n就是图中加油站的编号。
再遍历所有的加油站,获取到达所有居民房的最短路径。
再按照题目的要求选一个离居民房尽可能远,但不超过范围ds,且平均距离最小的。

C++:
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;


const int maxn = 1020;
const int INF = 1 << 30;

int G[maxn][maxn];
bool isVisit[maxn];
int d[maxn];

int n, m, k, ds;

void dijkstra(int s) {
    fill(isVisit, isVisit + maxn, false);
    fill(d, d + maxn, INF);
    d[s] = 0;
    for (int i = 0; i < m + n; i++) {
        int u = -1, min = INF;
        for (int j = 1; j <= m + n; j++) {
            if (isVisit[j] == false && d[j] < min) {
                min = d[j];
                u = j;
            }
        }
        if (u == -1) {
            return;
        }
        isVisit[u] = true;
        for (int j = 1; j <= m + n; j++) {
            if (isVisit[j] == false && d[u] + G[u][j] < d[j]) {
                d[j] = d[u] + G[u][j];
            }
        }
    }
}

int getId(char str[]) {
    int len = strlen(str);
    int id = 0, product = 1;
    for (int i = len - 1; i >= 0; i--) {
        if (str[i] != 'G') {
            id += (str[i] - '0') * product;
            product *= 10;
        }
    }
    if (str[0] == 'G') {
        return id + n;
    } else {
        return id;
    }
}

int main() {
    scanf("%d%d%d%d", &n, &m, &k, &ds);
    fill(G[0], G[0] + maxn * maxn, INF);
    int a, b, value;
    char city1[5], city2[5];
    for (int i = 0; i < k; i++) {
        scanf("%s %s %d", city1, city2, &value);
        a = getId(city1);
        b = getId(city2);
        G[a][b] = G[b][a] = value;
    }

    double resDis = -1, resAvg = INF;
    int resId = -1;
    for (int i = n + 1; i <= n + m; i++) {
        double minDis = INF, avg = 0;
        dijkstra(i);
        for (int j = 1; j <= n; j++) {
            if (d[j] > ds) {
                minDis = -1;
                break;
            }
            if (d[j] < minDis) {
                minDis = d[j];
            }
            avg += 1.0 * d[j] / n;
        }
        if (minDis == -1) {
            continue;
        }
        if (minDis > resDis) {
            resId = i;
            resDis = minDis;
            resAvg = avg;
        } else if (minDis == resDis && avg < resAvg) {
            resId = i;
            resAvg = avg;
        }
    }
    if (resId == -1) {
        printf("No Solution\n");
    } else {
        printf("G%d\n", resId - n);
        printf("%.1f %.1f\n", resDis, resAvg);
    }
    return 0;
}

相关文章

  • PAT甲级A1072---最短路径

    1072 Gas Station (30分) 分析: 先处理加油站的编号问题,把加油站的字母G去掉,然后加上n就是...

  • PAT甲级A1087---最短路径

    1087 All Roads Lead to Rome (30分) 分析: dijkstra+dfs C++:

  • PAT A1001 A+B Format (20)

    PAT A1001 A+B Format 原题链接PAT甲级题目目录(简书)PAT甲级题目目录(CSDN)在CSD...

  • 字符串输入问题

    PAT 甲级 1100 People on Mars count their numbers with base ...

  • PAT甲级题解 全部 JAVA版 持续更新

      临近过年,闲来无事。恰逢弟弟准备考PAT甲级,总是问我一些问题。又见网上PAT甲级多为c/c++版本的答案。罕...

  • PAT甲级 1043 Is It a Binary Search

    原题链接 PAT甲级 1043 Is It a Binary Search Tree (25 分) 题目大意 给定...

  • 2020-02-06

    PAT-甲级 做题笔记 目录 0000 做题 Tips 基本经验1003 Emergency (Dijkstra ...

  • Pat 甲级 1001

    太感人终于把1001做对了,一开始没有看清题目要求。产生各种各样的错误,最后又有编译错误。在Pat中Java的类名...

  • Pat 甲级1002

    在编译器中将边界设为1001 然后提交的代码边界设为1000,结果产生了部分正确的结果,实在是不应该,浪费太多时间...

  • PAT甲级题目

    先把会做的做了,emmmm 1006 水题,关键就是在于如何切分字符串而已,考的应该也就是字符串了: 1007 这...

网友评论

      本文标题:PAT甲级A1072---最短路径

      本文链接:https://www.haomeiwen.com/subject/wageektx.html