给定图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. php 批量载入文件的几种方式

    方式1:spl_autoload_register // Register the autoloader. /** * Contains the functionality for auto-load ...

  2. equals和==方法比较(二)--Long中equals源码分析

    接上篇,分析equals方法在Long包装类中的重写,其他类及我们自定义的类,同样可以根据需要重新equals方法. equals方法定义 equals方法是Object类中的方法,java中所有的对 ...

  3. OpenLDAP介绍

    首先LDAP是一个轻量级的产品(LightWeight),是一个Directory(D),存取的协议(Access Protocol). 我要着重指出,LDAP是一个数据库,但是又不是一个数据库.说他 ...

  4. java学习笔记-01.对象入门

    1.面向对象编程简称是OOP. 2.继承是通过 extends关键字实现的,接口是通过implements关键字实现的. 3.public:意味着后续的定义任何人均可使用. private:意味着除了 ...

  5. 建立 Python 虚拟环境

    1.1 安装依赖包 $ yum -y install wget gcc epel-release git 1.2 安装 Python3.6和pip $ yum -y install python36 ...

  6. NO.1:自学python之路------Hello world、判断、循环

    引言 人工智能如今越来越贴近生活,在这里将记录我自学python与tensorflow的过程.编程使用IDE:visual studio 2017,python版本3.6.4,tensorflow版本 ...

  7. hive on hbase 数据表关联

    有时,数据可以容易的存储在hive中,但是要导入到hbase里,可以不用写MR程序来操作,可以使用hive on hbase方式来创建相应的表关联关系来将hive中的数据导入到对应的hbase的表里, ...

  8. C++ 名字空间namespace的使用

    A namespace is a scope.C++ provides namespaces to prevent name conflicts.A namespace is a mechanism ...

  9. Scrum立会报告+燃尽图 04

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2194 一.小组介绍 组长:王一可 组员:范靖旋,王硕,赵佳璐,范洪达,祁玉 ...

  10. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...