PAT All Roads Lead to Rome 单源最短路
思路:单源最短路末班就好了,字符串映射成数字处理。
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 单源最短路的更多相关文章
- 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 ...
- 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 ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT_A1087#All Roads Lead to Rome
Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...
- [图的遍历&多标准] 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 ...
- PAT1087. All Roads Lead to Rome
PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
随机推荐
- Python中变量和常量的理解
一.变量的定义:把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做"变量". 二.变量的作用:变量用于存储要在计算机程序中引用和操作的信息.它提供了 ...
- linkinFrame--测试项目添加git管理
OK,前面一篇博客,已经搭建好了项目的基础的结构.现在我们添加一个简单的servlet来测试下项目能不能跑,然后在讲项目添加到coding上用git管理. 1,我们编写一个servlet,编写一个js ...
- TDD入门demo
OK,前面的博客整理了一系列的junit相关内容,这里举一个例子TDD实际的编码例子,不管实际编码中是否使用TDD,个人觉得这种思想必须要有. 我们不一定在写业务代码之前一定要说是把测试类都写出来,至 ...
- MS SQL 监控数据/日志文件增长
前几天,在所有数据库服务器部署了监控磁盘空间的存储过程和作业后(MS SQL 监控磁盘空间告警),今天突然收到了两封告警邮件,好吧,存储规划是一方面,但是,是不是要分析一下是什么原因造成磁盘空 ...
- servlet 监听器分类
http://blog.csdn.net/cxg200888/article/details/77894842
- 05_Javascript进阶第一天
内部私有函数 function a(){ alert('aaa'); return function b(){ alert('bbb'); } } //调用内部私有函数b,方法1 var func=a ...
- 【转】EI收录的中国期刊
ISSN 期刊名 0567-7718 Acta Mechanica Sinica 1006-7191 Acta Metallurgica Sinica (English Letters) ...
- ehcache模糊批量移除缓存
目录 前言 实现 总结 前言 众所周知,encache是现在最流行的java开源缓存框架,配置简单,结构清晰,功能强大.通过注解@Cacheable可以快速添加方法结果到缓存.通过@CacheEvic ...
- 蓝桥杯练习系统—基础练习 sine之舞
题目:最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数,所以他准备和奶牛们做一个"Sine之舞"的游戏,寓教于乐,提高奶牛们的计算能力. 不妨设 ...
- Python self学习
最开始学习Python时,用的IDE是IDEL,看的教程是<简明Python教程>,强烈推荐一下,讲得还是很不错的,适合新手学习 http://www.kuqin.com/abyteofp ...