思路:单源最短路末班就好了,字符串映射成数字处理。


AC代码

//#define LOCAL
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <string>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 200;
map<string, int> ha;
int id, st, ed;
int n, k;
int hap[maxn];
char names[maxn][50];

int getId(string s) {
    if(!ha.count(s)) {
        ha[s] = id++;
    }
    return ha[s];
}

struct Edge {
    int from, to, dist;
    Edge(int u, int v, int d):from(u),to(v),dist(d) {}
};
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn], hp[maxn], pt[maxn], routes[maxn]; //Best
int p[maxn];

void init() {
    ha.clear();
    id = 0;
    for(int i = 0; i < maxn; i++) G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, int dist) {
    edges.push_back(Edge(from, to, dist));
    int m = edges.size();
    G[from].push_back(m-1);
}

struct HeapNode{
    int d, u;
    HeapNode(int d, int u):d(d), u(u){
    }
    bool operator < (const HeapNode& rhs) const {
        return d > rhs.d;
    }
};

//距离小,开心多,平均大
void dijkstra(int s) {
    memset(done, 0, sizeof(done));
    priority_queue<HeapNode> Q;
    for(int i = 0; i < n; i++) {
        d[i] = inf;
        hp[i] = -inf;
        pt[i] = inf;
    }
    d[s] = hp[s] = pt[s] = 0;
    routes[s] = 1;
    Q.push(HeapNode(0, s));
    while(!Q.empty()) {
        HeapNode x = Q.top(); Q.pop();
        int u = x.u;
        if(done[u]) continue;
        done[u] = true;
        for(int i = 0; i < G[u].size(); i++) {
            Edge& e = edges[G[u][i]];
            bool update = false;
            if(d[e.to] > d[u] + e.dist) {
                routes[e.to] = routes[u];
                update = true;
            } else if(d[e.to] == d[u] + e.dist) {
                routes[e.to] += routes[u];
                if(hp[e.to] < hp[u] + hap[e.to]) {
                    update = true;
                } else if(hp[e.to] == hp[u] + hap[e.to]) {
                    if(pt[e.to] > pt[u] + 1) {
                        update = true;
                    }
                }
            }
            if(update) {
                d[e.to] = d[u] + e.dist;
                p[e.to] = u;
                hp[e.to] = hp[u] + hap[e.to];
                pt[e.to] = pt[u] + 1;
                Q.push(HeapNode(d[e.to], e.to));
            }
        }
    }
}

void print(int u) {
    if(u == 0) {
        printf("%s", names[0]);
        return;
    } else {
        print(p[u]);
        printf("->%s", names[u]);
    }
}

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    while(scanf("%d%d%s", &n, &k, names[0]) == 3) {
        init();
        st = getId(names[0]);
        int happy;
        for(int i = 1; i < n; i++) {
            scanf("%s %d", names[i], &happy);
            int u = getId(names[i]);
            hap[u] = happy;
        }
        ed = getId("ROM");
        char x[50], y[50];
        int u, v, cost;
        for(int i = 0; i < k; i++) {
            scanf("%s%s%d", x, y, &cost);
            u = getId(x), v = getId(y);
            //printf("%d %d\n", u, v);
            addEdge(u, v, cost);
            addEdge(v, u, cost);
        }
        dijkstra(0);
        printf("%d %d %d %d\n", routes[ed], d[ed], hp[ed], (int)(hp[ed]/pt[ed]));
        print(ed);
        printf("\n");
    }
    return 0;
}

如有不当之处欢迎指出!

PAT All Roads Lead to Rome 单源最短路的更多相关文章

  1. PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]

    1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...

  2. PAT 1087 All Roads Lead to Rome

    PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...

  3. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  4. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  5. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

  7. [图的遍历&多标准] 1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...

  8. PAT1087. All Roads Lead to Rome

    PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...

  9. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

随机推荐

  1. Overload&Override

    Overload&Override overload-–重载 方法的重载就是在一个类中,可以定义多个有相同名字,但参数不同的方法.调用时,会根据不同的参数表选择对应的方法. 规    则:两同 ...

  2. Log4j源码解析--框架流程+核心解析

    OK,现在我们来研究Log4j的源码: 这篇博客有参照上善若水的博客,原文出处:http://www.blogjava.net/DLevin/archive/2012/06/28/381667.htm ...

  3. CentOS如何把deb转为rpm

    说明:可以转换,但不一定可用,可以根据报错提示,安装需要的依赖. 1 安装alien工具,下载地址http://ftp.de.debian.org/debian/pool/main/a/alien/ ...

  4. Linux make nginx 的时候报错

    报错如下: `conf/koi-win' and `/usr/local/nginx/conf/koi-win' are the same file   原因: 可能在编译 nginx 的时候步骤不对 ...

  5. PhpStudy 升级 MySQL 版本到5.7

    1:备份当前数据库数据. 最好是导成 SQL 文件 2:备份 PhpStudy 下的 MySQL 文件夹.以防升级失败.还可以使用旧版本的数据库 3:下载MySQL5.7.解压.然后放在 PhpStu ...

  6. php与HTML交互问题

    1.将表单中的action属性值设为PHP路径,则网页会跳转到这个网址 <html> <body> <form action="welcome.php" ...

  7. httpd添加新模块

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  8. Div+Css画太极图源代码

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>D ...

  9. final修饰符,多态,抽象类,接口

    1:final关键字(掌握)    (1)是最终的意思,可以修饰类,方法,变量.    (2)特点:        A:它修饰的类,不能被继承.        B:它修饰的方法,不能被重写.      ...

  10. Tomcat部署war应用总结

    前言:罗列在Tomcat部署web应用的几种方法,供以后翻阅,其中的以helloapp为例 Tomcat目录介绍 简单目录介绍: bin目录:包含tomcat启动/关闭等脚本,支持linux.wind ...