题意:给定一个图,你要从 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. mysql having,group by查询去除重复记录

    http://m.jb51.net/article/39302.htm 可以这样去理解group by和聚合函数 http://www.cnblogs.com/wuguanglei/p/4229938 ...

  2. TASKER 定制你的手机让它在办公室时屏幕 30 分钟才灭

    TASKER 定制你的手机让它在办公室时屏幕 30 分钟才灭 因为到的办公室,手机一直是充电的,不想屏幕太快关关掉,所以使用 TASKER 做了一个条件. 当 WIFI 连接到公司 WIFI 且充电中 ...

  3. GPU 服务器环境安装中一些基础note

    GPU 服务器环境安装中一些基础note GPU 服务器: 添加组,用户,并为之新建主目录. c302@c302-dl:~$ sudo addgroup testgroup Adding group ...

  4. android adb端口被占用解决方法

    1.输入adb devices命令 C:\Users\Nick>adb devices List of devices attached adb server version (31) does ...

  5. NPOI-Excel系列-1002.创建带有Document Summary Information和Summary Information的Excel文件

    1. using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using Microsoft.VisualSt ...

  6. 学习笔记之C++ Primer中文版(第五版)

    非常权威系统的语言书,正好学习下C++11内容. C++ Primer_百度百科 http://baike.baidu.com/link?url=YLvDJE9w3CjGp3eQwjuXYKUZs7v ...

  7. 读Understanding the Linux Kernel, 3rd Edition有感

    14.3.2.2. Avoiding request queue congestion Each request queue has a maximum number of allowed pendi ...

  8. C# user32.dll

    #region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备 ...

  9. 「小程序JAVA实战」小程序首页视频(49)

    转自:https://idig8.com/2018/09/21/xiaochengxujavashizhanxiaochengxushouyeshipin48/ 视频显示的内容是视频的截图,用户的头像 ...

  10. FDQuery sqlserver 临时表

    用FDQuery执行创建临时表,查不到临时表,用ADOQuery和BDEQuery均正常,比较发现用ADOQuery执行的时候只有SQL没有调用sql的系统存储过程sp_prepexec. 是fdqu ...