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


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. nginx服务器的作用与简单搭建(windows)

    Nginx是一款开源代码的反向代理服务器. 何为反向代理呢?即以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连 ...

  2. 20165220 学习基础和C语言基础调查

    # # # # 我觉得我打游戏(不知道算不算一技之长)毕竟从小学一年级就接触到了各种形形色色的游戏,讲道理其实我的游戏天赋毕竟还是很大的,从意识到感觉我觉得都比大多数人好一些,其实吧打游戏打得好也是很 ...

  3. 07_jquery入门第一天

    视频来源:麦子学院 讲师:魏畅然 补充:JSON.stringify()函数 [https://www.cnblogs.com/damonlan/archive/2012/03/13/2394787. ...

  4. 通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值

    通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值 目录 一.常见的 enum 类型 二.演变:class 版本的 enum 类型 ...

  5. 对List中每个对象元素按时间顺序排序

    需求: 需要对List中的每个User按照birthday顺序排序,时间由小到大排列. 代码实现: import java.text.SimpleDateFormat; import java.uti ...

  6. 从返回的HTTP Header信息中隐藏Apache的版本号及PHP的X-Powered-By信息

    默认情况下,很多apache安装时会显示版本号及操作系统版本,甚至会显示服务器上安装的是什么样的apache模块.这些信息可以为黑客所用,并且黑客还可以从中得知你所配置的服务器上的很多设置都是默认状态 ...

  7. 关于C#连接Oracle数据库 尝试加载Oracle客户端时引发BadImageFormatException 如果在安装32位Oracle客户端组件的情况下以64位模式运行,将出现此问题

    这个问题已经困扰了我快一个月了,各种百度,各种博客,可是,一个个都试过了,什么下载32位客户端,配置环境变量什么的,纯属扯犊子,开发环境win10 64位    oracle 11g r2 64位,这 ...

  8. 《Thinking in Java》学习笔记(三)

    1>Java中的常量 使用final和static来修饰的变量称为常量,常量用大写字母表示,字母间用下划线连接. Java中定义常量有以下几种方式: interface ConstantInte ...

  9. Java基础之Throwable,文件加载

    Java中的异常与错误都继承自Throwable,Exception又分为运行时异常(RuntimeException)和编译时异常. 运行时异常是程序的逻辑不够严谨或者特定条件下程序出现了错误,例如 ...

  10. 洛谷 [P1280] 尼克的任务

    DP 题目问的是最大空暇时间,那么就定义dp[i]为第i分钟的最大空暇时间,显然满足最优子结构,我们发现dp[i]仅与其后的值有关,那么从后往前推,如果第i分钟没有任务,dp[i]=dp[i+1],如 ...