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. Streak OpenCart 商城自适应主题模板 ABC-0010

    兼容浏览器 IE9, Firefox, Safari, Opera, Chrome OpenCart版本号 OpenCart 1.5.x, OpenCart 1.5.6.x, OpenCart 1.5 ...

  2. iOS_11_tableViewCell使用alertView变更数据

    最后效果图: Girl.h // // Girl.h // 11_tableView的使用_红楼梦 // // Created by beyond on 14-7-26. // Copyright ( ...

  3. Discuz 楼主帖子采集

    try { ; i < ; i++) { var html = GetHtmls("http://bbs.fobshanghai.com/viewthread.php?tid=3885 ...

  4. 文件搜索神器 Everything

    Everything 是一款 NTFS 磁盘格式下的文件搜索工具,1月5日发布测试版本 1.3.0.631b Beta,增加文件列表.收藏夹.自定义快捷键.高级搜索等功能,取消了比较实用的 etp/f ...

  5. Linux 于 shell 变数 $#,$@,$0,$1,$2 含义解释:

    变量说明: $$ Shell自己PID(ProcessID) $! Shell背景上次执行Process的PID $? 命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 $* 全部參数 ...

  6. pyspark简要原则

    概要 这是一个看前一段时间spark的python支持的时,有点简单的后pyspark内python代码,我们把一个一般流程.虽然几乎没有python,但基本上能看懂pyspark它是如何使不同的虚拟 ...

  7. rhel6使用的版本数部分intel xeon处理器时间bug

    可惜在总前几天"oracle操作和维护的高级别小组"于.BBQ 上帝说,大量RHEL的bug.这bug在这个例子中,下面的URL: https://access.redhat.co ...

  8. system strategies of Resources Deadlock

    In computer science, Deadlock is a naughty boy aroused by compete for resources. Even now,     there ...

  9. x264 - open gop and closed gop

    GOP - Group of picture(影像集团),它指的是两个I帧之间的距离. Reference(基准期).  它指的是两个P帧之间的距离. 简而言之, 跨参考gop的,变open gop: ...

  10. 让struts2和servlet共存

    由于struts2默认的是拦截全部的请求 由配置文件能够看出 <filter> <filter-name>struts2</filter-name> <fil ...