给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 
已知起点为SS,终点为TT,希望在到达TT时能够拥有PP的金额,问一开始在SS最少要携带多少金额,并求出路径(若有多条,输出字典序最小的) 
从SS离开时不需要缴费,进入TT时需要缴费

倒序找最短路  d[i] 表示从i到终点需要的最少的金额 在更新d的时候 分两种情况

#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = ;
const long long Inf = (1LL<<);
int head[N];
int vis[];
int cnt;
char to[];
int n, m;
long long d[N];
bool inq[N];
int p[N];
struct Edge{
int v, w, next;
}edges[N*]; void add(int u ,int v, int w)
{
edges[cnt].v = v;
edges[cnt].w = w;
edges[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
memset(head, -, sizeof(head));
cnt = ;
} void print(int u){
if( p[u]==- ){
printf("%c\n", to[u] );
return ;
}
printf("%c-", to[u] );
print(p[u]);
}
void spfa(int s, long long vl){
for ( int i=; i<N; i++ ) d[i]=Inf, inq[i]=;
d[s]=vl;
p[s]=-;
queue<int> Q;
Q.push(s);
while( !Q.empty() ){
int u=Q.front(); Q.pop();
inq[u]=;
for ( int i=head[u]; i!=-; i=edges[i].next ){
Edge e=edges[i];
long long nd;
if(e.w) nd=(long long) ceil(d[u]*1.0/*); //推一下公式。。虽然我没推出来
else nd=d[u]+;
if( d[e.v]>nd || d[e.v]==nd && to[u]<to[p[e.v]] ){ //如果相等 则判断字典序
d[e.v]=nd;
p[e.v]=u;
if( !inq[e.v] ){
inq[e.v]=;
Q.push(e.v);
}
}
}
}
} int main(){
for ( int i=; i<; i++ ) {
vis['a'+i]=i+; to[i+]='a'+i;
vis['A'+i]=i; to[i]='A'+i;
}
int n, m, kas=;
while( scanf("%d", &m ) == && m!=- ){
init();
char a[], b[];
for ( int i=; i<=m; i++ ){
scanf("%s%s", a, b );
int u=vis[a[]], v=vis[b[]];
add(u,v,a[]<'a');
add(v,u,b[]<'a');
}
long long vl;
scanf("%lld%s%s", &vl, a, b );
int u=vis[a[]], v=vis[b[]];
spfa(v,vl);
printf("Case %d:\n", ++kas);
printf("%lld\n", d[u]);
print(u);
}
}

The Toll! Revisited UVA - 10537(变形。。)的更多相关文章

  1. 【Toll!Revisited(uva 10537)】

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

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

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

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

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

  4. UVA10537 Toll! Revisited

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

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

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

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

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

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

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

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

  9. Uva 10537 过路费

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

随机推荐

  1. OpenStack入门篇(八)之镜像服务Glance

    一.Glance的概述 Glance是为虚拟机的创建提供镜像的服务,我们基于Openstack是构建基本的IaaS平台对外提供虚拟机,而虚拟机在创建时必须为选择需要安装的操作系统,Glance服务就是 ...

  2. 查询系统日期和时间(mysql)

    select current_date  --不带时间select sysdate()   或 SELECT NOW();  --带时间

  3. Python中的解决中文字符编码的问题

    python3中str默认为Unicode的编码格式 python2中str默认为bytes类型的编码格式 Unicode是一32位编码格式,不适合用来传输和存储,所以必须转换成utf-8,gbk等等 ...

  4. Mysql 单表主从同步

    先配主从同步,后将主库表老数据传输到从库 说明:api-server的数据库为主,其他harbor为从 1.master 配置文件更改 [mysqld] log-bin = mysql-bin ser ...

  5. Python3 匿名函数

    一 匿名函数 lambda函数也叫匿名函数,语法结构如下: lambda x:x+1 x --> 形参 x+1 --> 返回值,相当于return x+1 实例(Python3.0+): ...

  6. ES6对数组的扩展

    ECMAScript6对数组进行了扩展,为数组Array构造函数添加了from().of()等静态方法,也为数组实例添加了find().findIndex()等方法.下面一起来看一下这些方法的用法. ...

  7. Android开发第二阶段(6)

    今天:对sdcard的操作有了进一步的了解和深入,为程序可以自主扫描并添加sdcard的MP3格式文件 明天:最后的修正.

  8. Unicode 和 UTF-8 有何区别

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有,转载请联系作者获得授权. ========== ...

  9. HDU 5191 Building Blocks

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5191 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  10. ASP.NET Core 中的 Razor 页面介绍

    标题:ASP.NET Core 中的 Razor 页面介绍 地址:https://docs.microsoft.com/zh-cn/aspnet/core/razor-pages/index?view ...