从终点逆推,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=y+19,0≤r≤19,即19*y=20*x+r,根据题意y应该尽量小,x的部分是不能变动的,所以y=x+ceil(x/19.0)。

然后从起点找一条字典序最小的路径即可,因为每个字母都是独一无二的,所以不必bfs,每次记录一个点就够了。

#include<bits/stdc++.h>
using namespace std; const int maxn = , maxm = maxn*maxn, town = , village = ;
bool g[maxn][maxn];
int head[maxn],nxt[maxm],to[maxm],ecnt;
int tp[maxn];
int id[];
char rid[]; void addEdge(int u,int v)
{
to[ecnt] = v;
nxt[ecnt] = head[u];
head[u] = ecnt++;
}
int id_cnt; void init()
{
memset(head,-,sizeof(head));
memset(id,-,sizeof(id));
memset(g,,sizeof(g));
ecnt = ;
id_cnt = ;
} inline int ID(char c) {
if(~id[c]) return id[c];
id[c]= id_cnt;
rid[id_cnt] = c;
tp[id_cnt] = 'a'<=c&&c<='z';//写成isalpha(c)-1;WA了
return id_cnt++;
}
typedef long long ll;
typedef pair<ll,int> Node;
#define fi first
#define se second ll d[maxn];
void dijkstra(int s,ll p)
{
memset(d,0x7f,sizeof(d));
priority_queue<Node,vector<Node>,greater<Node> > q;
q.push(Node(d[s] = p,s));
while(q.size()){
Node x = q.top(); q.pop();
int u = x.se;
if(d[u] != x.fi) continue;
ll t = (tp[u]?(+d[u]):((d[u]+)/+d[u]));
for(int i = head[u]; ~i; i = nxt[i]){
int v = to[i];
if(d[v] > t ){
q.push(Node(d[v]= t,v));
}
}
}
} ll cost(int u,int v)
{
return tp[v]?:((d[u]+)/);
} void FindPath(int s,int e)
{
int u = s;
while(u != e){
printf("%c-",rid[u]);
int nex = -;
for(int i = head[u]; ~i; i = nxt[i]){
int v = to[i];
if(d[u]- cost(u,v) >= d[v]){
if(~nex) {
if(rid[v] < rid[nex]) nex = v;
}else nex = v;
}
}
swap(nex,u);
}
printf("%c\n",rid[e]);
} int main()
{
//freopen("in.txt","r",stdin);
int n;
char a[],b[];
int kas = ;
while(scanf("%d",&n),~n){
init();
while(n--){
scanf("%s%s",a,b);
int u = ID(*a), v = ID(*b);
if(!g[u][v]){
g[u][v] = g[v][u] = true;
addEdge(u,v); addEdge(v,u);
}
}
ll p;
scanf("%lld%s%s",&p,a,b);
int s = ID(*a),e = ID(*b);
dijkstra(e,p);
printf("Case %d:\n%lld\n",++kas,d[s]);
FindPath(s,e);
}
return ;
}

UVA 10537 Toll! Revisited (逆推,最短路)的更多相关文章

  1. uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

    Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...

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

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

  3. 【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 题意: 有两种节 ...

  4. UVa 10537 The Toll! Revisited (最短路)

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

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

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

  6. Uva 10537 过路费

    题目链接:http://vjudge.net/contest/143062#problem/C 题意: 给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交 ...

  7. uva10537 dijkstra + 逆推

    21:49:45 2015-03-09 传送 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8& ...

  8. UVA116Unidirectional TSP(DP+逆推)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18206 题意:M*N的数阵,从左边一列到右边一列走过的数的和的最小.并输出路 ...

  9. UVA10537 Toll! Revisited

    difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

随机推荐

  1. 多进程小例子--fork+pipe

    1 #include<stdio.h>  2 #include<unistd.h>  3   4 #define m 6  5 int main()  6 {  7       ...

  2. 7.15实习培训日志 java题解

    周末总结 本周主要学习了markdown,git,docker等工具的使用.在本周的学习中,初步了解了markdown,git,docker的使用.本周的静态博客部署中,对于怎么显示一个博客内容,有两 ...

  3. Codevs 1688 求逆序对(权值线段树)

    1688 求逆序对  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定一个序列a1,a2,…, ...

  4. uoj#401. 【CTSC2018】青蕈领主(分治FFT)

    传送门 话说分治\(FFT\)是个啥子啊--还有题目里那字好像念(蕈xùn) 首先考虑无解的情况:区间相交或者\(L_n\neq n\) 这两个都可以感性理解一下 所以区间之间只会有包含关系,我们把每 ...

  5. 剑指Offer的学习笔记(C#篇)-- 求1+2+3+...+n

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 一 . 直接解题吧 芽儿呦,突然觉得,我不说! ...

  6. python 之 函数 面向过程 三元表达式 函数递归

    5.11 面向过程编程思想 核心是'过程'二字,过程即解决问题的步骤,即先干什么,再干什么........ 基于面向过程编写程序就好比在设计一条流水线,是一种机械式的思维方式. 总结优缺点: 优点:复 ...

  7. 搭建yum源

    五.保留缓存rpm包, 修改配置文件,将最新的rpm包下载到本地并保存. 3) 使用yum命令安装httpd软件包 六.制作yum仓库 1)         自定义yum仓库:createrepo 安 ...

  8. 如何在JMeter中使用ForEach控制器

    Jmeter中的ForEach Controller遍历变量数组. 在这个JMeter教程中,我们将使用ForEach控制器循环访问JSON数组. 有时我们需要解析响应并提取某些信息.例如,在测试AP ...

  9. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest A. Toda 2 贪心 + 暴力

    A. Toda 2 time limit per test 2 seconds memory limit per test 512 megabytes input standard input out ...

  10. oracle 列转行

    with temp as( as S3 from dual union all as S3 from dual ) select * from temp unpivot(Qty for Sizes i ...