UVA 10537 - The Toll! Revisited

option=com_onlinejudge&Itemid=8&page=show_problem&category=550&problem=1478&mosmsg=Submission+received+with+ID+14135315" target="_blank" style="">题目链接

题意:给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交1,给定起点终点和到目标地点要剩下的货物,问最少要带多少货物上路。并输出路径,假设有多种方案。要求字典序最小

思路:dijstra的逆向运用。d数组含义变成到该结点至少须要这么多货物,然后反向建图,从终点向起点反向做一遍

这题被坑了。。并非输出的城市才存在。比方以下这组例子

0

1 A A

应该输出

1

A

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; const int MAXNODE = 105; typedef long long Type;
const Type INF = (1LL<<61); struct Edge {
int u, v;
Type dist;
Edge() {}
Edge(int u, int v, Type dist) {
this->u = u;
this->v = v;
this->dist = dist;
}
}; struct HeapNode {
Type d;
int u;
HeapNode() {}
HeapNode(Type d, int u) {
this->d = d;
this->u = u;
}
bool operator < (const HeapNode& c) const {
return d > c.d;
}
}; char to[255]; struct Dijkstra {
int n, m;
vector<Edge> edges;
vector<int> g[MAXNODE];
bool done[MAXNODE];
Type d[MAXNODE];
int p[MAXNODE]; void init(int tot) {
n = tot;
for (int i = 0; i < n; i++)
g[i].clear();
edges.clear();
} void add_Edge(int u, int v, Type dist) {
edges.push_back(Edge(u, v, dist));
m = edges.size();
g[u].push_back(m - 1);
} void print(int e) {
if (p[e] == -1) {
printf("%c\n", to[e]);
return;
}
printf("%c-", to[e]);
print(edges[p[e]].u);
} void dijkstra(Type start, int s) {
priority_queue<HeapNode> Q;
for (int i = 0; i < n; i++) d[i] = INF;
d[s] = start;
p[s] = -1;
memset(done, false, sizeof(done));
Q.push(HeapNode(start, 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]];
Type need;
if (e.dist) need = (Type)ceil(d[u] * 1.0 / 19 * 20);
else need = d[u] + 1;
if (d[e.v] > need || (d[e.v] == need && to[u] < to[edges[p[e.v]].u])) {
d[e.v] = need;
p[e.v] = g[u][i];
Q.push(HeapNode(d[e.v], e.v));
}
}
}
}
} gao; typedef long long ll;
int n, m, vis[255]; int main() {
int cas = 0;
for (int i = 0; i < 26; i++) {
vis['A' + i] = i;
to[i] = 'A' + i;
}
for (int i = 0; i < 26; i++) {
vis['a' + i] = i + 26;
to[i + 26] = 'a' + i;
}
while (~scanf("%d", &m) && m != -1) {
gao.init(52);
char a[2], b[2];
int u, v;
while (m--) {
scanf("%s%s", a, b);
u = vis[a[0]], v = vis[b[0]];
gao.add_Edge(u, v, a[0] < 'a');
gao.add_Edge(v, u, b[0] < 'a');
}
ll need;
scanf("%lld%s%s", &need, a, b);
u = vis[a[0]]; v = vis[b[0]];
gao.dijkstra(need, v);
printf("Case %d:\n", ++cas);
printf("%lld\n", gao.d[u]);
gao.print(u);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA 10537 - The Toll! Revisited(dijstra扩张)的更多相关文章

  1. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...

  2. UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)

    题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可.现在如果要让p货物到达e,那么从起点出发最少要准备 ...

  3. UVa 10537 The Toll! Revisited (最短路)

    题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...

  4. uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

    Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...

  5. UVA10537 Toll! Revisited

    difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  6. 【Toll!Revisited(uva 10537)】

    题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra!       在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...

  7. The Toll! Revisited UVA - 10537(变形。。)

    给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...

  8. UVA 10537 Toll! Revisited (逆推,最短路)

    从终点逆推,d[u]表示进入u以后剩下的货物,那么进入u之前的货物数量设为y,d[u] = x,那么y-x=ceil(y/20.0)=(y-1)/20+1=(y+19)/20. (y-x)*20+r= ...

  9. Uva 10537 过路费

    题目链接:http://vjudge.net/contest/143062#problem/C 题意: 给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交 ...

随机推荐

  1. 无法识别的属性“targetFramework”。请注意,属性名是大写和小写。错误的解决方案

    "/CRM"应用server错. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查以下的特定错误具体信息并适当地改动配置文件. 分析器错误消息: 无法识别的属性 ...

  2. flex4 一些项目使用的技术

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  3. C# 6.0 (C# vNext) 的新功能:Exception-Handling Improvements

    于 C# 6.0 包裹在异常处理的新功能,有两个方面的改进: 异步处理(async and await)能力 catch block 总结使用.于 C# 5.0 释放 async and await, ...

  4. 使用SKIP-GRANT-TABLES 解决 MYSQL ROOT密码丢失(转)

    B.5.3.2 How to Reset the Root Password If you have never assigned a root password for MySQL, the ser ...

  5. GUIForDebug

    package gui; import org.luaj.vm2.Globals; import org.luaj.vm2.LuaValue; import org.luaj.vm2.ast.Chun ...

  6. JAVA学习JSTL与EL

    一.基础 1.EL(Expression Language):为了使jsp写起来更加简单,提供了在Jsp中简化表达式的方法 2.JSTL:(JSP Standard Tag Library)jstl标 ...

  7. Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW

    一.Telnet 采用Telnet用来訪问远程计算机的TCP/IP协议以控制你的网络设备,相当于在离开某个建筑时大喊你的username和口令.非常快会有人进行监听, 并且他们会利用你安全意识的缺乏. ...

  8. [LeetCode203]Remove Linked List Elements

    题目: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 ...

  9. Node.js Tools for Visual Studio

    https://www.visualstudio.com/en-us/features/node-js-vs.aspx

  10. GitLab 之 Linux十分钟快装(转)

    先把 Shell 命令贴出来,楼主以 CentOS release 6.5 (Final) 64位 为例: //配置系统防火墙,把HTTP和SSH端口开放. sudo yum install curl ...