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

析:最短路,逆推,d[i] 表示的是从 i 到时 t 最少要带多少,然后就能顺利的推出从 s 开始时要带多少,然后打印路径,每次取最小的字母即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} struct Edge{
int from, to;
LL dist;
};
struct HeapNode{
LL d; int u;
bool operator < (const HeapNode &p) const{
return d > p.d;
}
}; int ID(char ch){
if(islower(ch)) return ch - 'a' + 26;
return ch - 'A';
} char invID(int x){ return x < 26 ? 'A' + x: 'a' + x - 26; } struct Dijkstra{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
LL d[maxn]; void init(int n){
this->n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int u, int v, LL d){
edges.pb((Edge){u, v, d});
m = edges.sz;
G[u].pb(m-1);
} void dijkstra(int s, LL val){
priority_queue<HeapNode> pq;
fill(d, d + n, LNF);
d[s] = val; ms(done, 0);
pq.push((HeapNode){0, s});
while(!pq.empty()){
HeapNode x = pq.top(); pq.pop();
int u = x.u;
if(done[u]) continue;
done[u] = 1;
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(u > 25 && d[e.to] >= d[u] + 1){
e.dist = edges[G[u][i]^1].dist = 1LL;
d[e.to] = d[u] + 1;
pq.push((HeapNode){d[e.to], e.to});
}
else if(u < 26){
LL l = d[u], r = d[u] * 20;
while (l < r) {
LL m = (l + r) / 2;
LL x = m - ceil(m / 20.);
if (x < d[u]) l = m + 1;
else r = m;
}
if(d[e.to] >= l){
e.dist = edges[G[u][i]^1].dist = l - d[u];
d[e.to] = l;
pq.push((HeapNode){d[e.to], e.to});
}
}
}
}
} void solve(int m, int s, int t){
dijkstra(t, m);
printf("%lld\n", d[s]);
while(s != t){
printf("%c-", invID(s));
int mmin = INF;
for(int i = 0; i < G[s].sz; ++i){
Edge &e = edges[G[s][i]];
if(d[s] == d[e.to] + e.dist) mmin = min(mmin, e.to);
}
s = mmin;
}
printf("%c\n", invID(s));
}
}; Dijkstra dij; int main(){
int kase = 0;
while(scanf("%d", &n) == 1 && n != -1){
char s1[5], s2[5];
dij.init(60);
for(int i = 0; i < n; ++i){
scanf("%s %s", s1, s2);
dij.addEdge(ID(s1[0]), ID(s2[0]), 0LL);
dij.addEdge(ID(s2[0]), ID(s1[0]), 0LL);
}
scanf("%d %s %s", &n, s1, s2);
printf("Case %d:\n", ++kase);
dij.solve(n, ID(s1[0]), ID(s2[0]));
}
return 0;
}

  

UVa 10537 The Toll! Revisited (最短路)的更多相关文章

  1. UVA 10537 - The Toll! Revisited(dijstra扩张)

    UVA 10537 - The Toll! Revisited option=com_onlinejudge&Itemid=8&page=show_problem&catego ...

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

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

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

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

  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. 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= ...

  7. 【Toll!Revisited(uva 10537)】

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

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

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

  9. 【UVA10537】The Toll! Revisited (逆推最短路)

    题目: Sample Input1a Z19 a Z5A DD XA bb cc X39 A X-1Sample OutputCase 1:20a-ZCase 2:44A-b-c-X 题意: 有两种节 ...

随机推荐

  1. 华为交换机STP 根ID优先级设置

    http://m.blog.csdn.net/flyfish5/article/details/50224537 STP(Spanning Tree Protocol)生成树协议. 冗余链路 当前的交 ...

  2. java.lang.NumberFormatException:For input string:"undefined"

    在将字符串转换为数字时导致此错误,解决此问题的思路: 1.添加 try catch语句 2.判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法 发生错误的代码: java.la ...

  3. jmeter录制https请求时,浏览器每一个请求都 跳 不安全访问页面的解决方法

    1.关闭所有浏览器 2,使用终端 输入 : /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --ignore-certif ...

  4. ScheduledExecutorService 定时器用法

    1,如果只是想简单的写个定时任务,比如10分钟跑一次,那么ScheduledExecutorService是比较方便的,下面举个简单的例子 import java.util.concurrent.Ex ...

  5. 寒武纪-1005 Travel(树形DP)

    一.题目链接 http://aiiage.hustoj.com/problem.php?id=1005 二.题面 PDF:http://aiiage.hustoj.com/upload/file/20 ...

  6. MS-TEST 批处理执行测试时的资源文件目录问题

    What: 使用MS-TEST的 批处理执行它的测试 DLL  时,如: MSTest /testcontainer:C:\David\ADAccountGIT\AdAccountMSTest\ADA ...

  7. 安装MySQL时出现黄色感叹号,提示3306已被占用

    windows系统如何查看现在某个端口的应用进程id呢,命令是: 1.netstat  -aon|findstr 3306 2.最后的那个数值就是进程id号,此时需要查看该id号对应的应用是哪一个,可 ...

  8. Java下LDAP操作的资料

    话说LDAP真是个诡异的protocol(或者数据库,或者服务,whatever...),没有一个特别形象的spec.这里列出一些筛选出的还可以的文档,都是oracle的: https://docs. ...

  9. Python 中 logging 日志模块在多进程环境下的使用

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...

  10. js网页倒计时精确到秒级

    网页实时倒计时,精确到秒级,和天数倒计时原理一样. 一个很好用的js倒计时!网页实时倒计时,精确到秒级,和天数倒计时原理一样.js倒计时一般用于商城网站团购,特卖,很多地方都可用到!希望能够给大家带来 ...