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. composite template 组合模式

      1. 主要优点 组合模式的主要优点如下: (1) 组合模式可以清楚地定义分层次的复杂对象,表示对象的全部或部分层次,它让客户端忽略了层次的差异,方便对整个层次结构进行控制. (2) 客户端可以一致 ...

  2. [LeetCode129]Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  3. cygwin,在win中开发linux程序

    cygwin,在win中开发linux程序 http://www.cygwin.cn/site/info/show.php?IID=1001  很多用windows的朋友不习惯于用linux的开发环境 ...

  4. JFileChooser

    http://www.cnblogs.com/dyllove98/archive/2012/03/05/2461895.html package swing.choose; import java.a ...

  5. 3、采用Gradle创Libgdx工程

    (原文链接:http://www.libgdx.cn/topic/20/3-%E4%BD%BF%E7%94%A8gradle%E5%88%9B%E5%BB%BAlibgdx%E9%A1%B9%E7%9 ...

  6. 求n阶勒让德多项式

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 161  Solved: 105 [Submit][Status][Web Board] Descrip ...

  7. 栈上分配存储器的方法 alloca 抽样

    声明一个局部变量,必须分配在堆栈上,但有或没有它的方法 当然,,那是 alloca 下面的代码显示了可变长度参数转换,alloca 要使用 int main(int argc, char ** arg ...

  8. [WF4.0 现实] WF4.0 Receive &amp;&amp; Send

    写这篇博客的目的正是由于这个receive和send使用一直很困惑,有应用程序的多个实例,但整体感觉很模糊认识.每一次遇到,再要弄清楚.如今将这send和receive结合我们之前做的实例(未使用WC ...

  9. codeforces Round #260(div2) D解决报告

    D. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. [原创].NET 分布式架构开发实战之三 数据访问深入一点的思考

    原文:[原创].NET 分布式架构开发实战之三 数据访问深入一点的思考 .NET 分布式架构开发实战之三 数据访问深入一点的思考 前言:首先,感谢园子里的朋友对文章的支持,感谢大家,希望本系列的文章能 ...