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 题意: 有两种节 ...
随机推荐
- vm虚拟机怎么访问本地硬盘
第一步:点击VMware菜单“虚拟→设置”,在配置窗口的“选项”标签页内点击“共享文件夹”,在右侧点击“添加”按钮添加要共享的文件夹. 先确认虚拟机是关闭状态(susppend时无法修改属性)再修改虚 ...
- oracle计算两个日期的时间差时分秒
Oracle函数可以实现诸多的功能,下面就介绍使用oracle函数计算时间差的实现方法. 两个Date类型字段:START_DATE,END_DATE,计算这两个日期的时间差(分别以天,小时,分钟,秒 ...
- 关于 ThinkPHP5 使用 getBy 字段名方式获取数据
关于 ThinkPHP5 使用 getBy 字段名方式获取数据 有小伙半说怎么全文搜索都没有搜索到 getByName 之类的函数. 其实是在这里.
- 洛谷2473(SCOI2008)奖励关
题目:https://www.luogu.org/problemnew/show/P2473 因为可不可选此物与之前选过什么物品有关,所以状态可以记录成前面已经选过什么物品. 因为选不选此物与它带来的 ...
- 【白银组】codevs_1160 蛇形矩阵
#include <iostream> using namespace std; #define M 100 int a[M][M]; void pt( int n ) { for ( i ...
- 试玩mpvue,用vue的开发模式开发微信小程序
mpvue,美团开源的vue文件转换成小程序的文件格式,今天玩了一下练练手 mpvue文档地址: http://mpvue.com/mpvue/#_1 暂时有几个点需要注意的: 1.新增页面需要重新启 ...
- [转]基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作
基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作 转载请注明作者及来源:张峻崎,博客园 目前在.net的范围内,好的而且方便的ORM的真的不是很多,与VS集成 ...
- 1021 docker初识
docker与虚拟机相比,没有虚拟化内核,转而使用宿主机的内核.因此docker更轻更快 docker缺点:后端兼容性测试需求.把软件安装在不同的操作系统上进行测试,观察软件运行是否良好. 不能用do ...
- aix操作系统的版本中TL SP 含义
AIX 分为四个主要的操作系统级别:版本.发行版.技术级 (TL) 和服务包 (SP).版本和发行版通常指的是 AIX 的名称,例如AIX 7.1.TL 是包含重大更新的操作系统的发行版,而 SP 包 ...
- leetcode877
public class Solution { public bool StoneGame(int[] piles) { return true; } } 这问题很不好...