UVa 10537 The Toll! Revisited (最短路)
题意:给定一个图,你要从 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 (最短路)的更多相关文章
- UVA 10537 - The Toll! Revisited(dijstra扩张)
UVA 10537 - The Toll! Revisited option=com_onlinejudge&Itemid=8&page=show_problem&catego ...
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
- UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)
题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可.现在如果要让p货物到达e,那么从起点出发最少要准备 ...
- uva 10537 Toll! Revisited(优先队列优化dijstra及变形)
Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...
- UVA10537 Toll! Revisited
difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- 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= ...
- 【Toll!Revisited(uva 10537)】
题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra! 在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...
- The Toll! Revisited UVA - 10537(变形。。)
给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...
- 【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 题意: 有两种节 ...
随机推荐
- FastAdmin 升级后出现 is already in use
FastAdmin 升级后出现 is already in use 升级 FastAdmin 改进很多,但全新安装出现以下错误 Cannot use app\common\library\Menu a ...
- 【转】ubuntu 12.04 下 Vim 插件 YouCompleteMe 的安装
原文网址:http://www.cnblogs.com/jostree/p/4137402.html 作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree ...
- postgresql双机热备、高可用方案(采用pacemaker+corosync实现)
http://blog.csdn.net/qguanri/article/details/51151974 需求描述 我们有两台centos7的数据库主机A.B.要对A.B实现双机热备,A作为数据库m ...
- IntelliJ IDEA 基础设置
原文地址:IntelliJ IDEA 基础设置 博客地址:http://www.extlight.com 一.前言 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,Intel ...
- scrollTop、offsetHeight和offsetTop等属性用法详解--转转转
scrollTop.offsetHeight和offsetTop等属性用法详解: 标题中的几个相关相关属性在网页中有这大量的应用,尤其是在运动框架中,但是由于有些属性相互之间的概念比较混杂或者浏览器兼 ...
- mysql-4连接
联合多表查询 菜鸟教程join 日常应用较多的是从多个表格中获取数据.使用join可以在多个表查询进行select.update.delete. join按照功能分为三类: inner join(内连 ...
- Django ORM-02
6.ForeignKey 相关操作 1.正向查找 正向查找:那么什么是正向查找,我们知道对于一对多或者多对一的情况,我们一般将ForeignKey设置在多的一边,比如我们的书籍与出版社一般是多对一的, ...
- Python2.X如何将Unicode中文字符串转换成 string字符串
Python2.X如何将Unicode中文字符串转换成 string字符串 普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring = u&q ...
- Backing up the tail
The tail of the transaction log usually refers to the contents of the database's transaction log tha ...
- memcached配置 (初级)以及测试
一.memcached安装 memcached依赖 $ sudo apt-get install libevent-dev 安装memcached服务 $ sudo apt-get install ...