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


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. python_如何判断字符串a以某个字符串开头或结尾?

    案例: 某文件系统目录下有一系列文件: 1.c 2.py 3.java 4.sh 5.cpp ...... 编写一个程序,给其中所有的.sh文件和.py文件加上可执行权限 如何解决这个问题? 1. 先 ...

  2. scrapy_简介页面和详情页面

    如何对提取的URL进行限定? 往上找id和class属性值,进行多次层级选择,进行内容限定 如何实现获取主页所有urls,然后交给scrapy下载后并解析详情页面,返回结果?(文章简介页面和文章详细页 ...

  3. Windows下为PHP安装redis扩展

    1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本. 2.下载 php_redis-2.2.7-5.5-ts-vc11-x86.zip 和 php_igbinary-2.0.5- ...

  4. php加入环境变量

    一.配置变量 右击计算机-属性-高级系统设置-环境变量-系统变量-双击path,编辑,将php的文件夹路径添加到后面,我是:D:\wamp64\bin\php\php5.6.25 二.测试 当没有加入 ...

  5. input标签的美化

    css input checkbox和radio样式美化:http://www.haorooms.com/post/css_mh_ck_radio 自定义 css checkbox 样式  :http ...

  6. mysql与nagios的结合使用

    一. 对mysql建库建表,并测试数据 基本信息:库名:nh_nagios表名:nagios_alerts [root@nhserver2 ~]# mysql -u root -pEnter pass ...

  7. ng机器学习视频笔记(二) ——梯度下降算法解释以及求解θ

    ng机器学习视频笔记(二) --梯度下降算法解释以及求解θ (转载请附上本文链接--linhxx)   一.解释梯度算法 梯度算法公式以及简化的代价函数图,如上图所示. 1)偏导数 由上图可知,在a点 ...

  8. eclipse每次闪退后都提示查看\workspace\.metadata\.log

    错误如下: 找到<workspace>/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi"文件,将其删掉,再重启 ...

  9. Ubantu搭建FTP

    1.安装并启动 FTP 服务 安装 VSFTPD 使用 apt-get 安装 vsftpd kylin@kylin:~$ sudo apt-get install vsftpd -y [sudo] p ...

  10. 浅谈计算机中的IO模型

    IO模型一共有5种: blocking IO #阻塞IO nonblocking IO #非阻塞IO IO myltiplexing #IO多路复用 signal driven IO #信号驱动IO ...