Uva 10537 过路费
题目链接:http://vjudge.net/contest/143062#problem/C
题意:
给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交1,给定起点终点和到目标地点要剩下的货物,问最少要带多少货物上路,并输出路径,如果有多种方案,要求字典序最小.
分析:
逆向最短路: 因为要求的就是起点的货量,从终点出发求最短路,但是,费用的计算要处理,重新定义d 数组,d[i] 表示,进入节点 I 以后,还要多少 d[i] 个 货品 才能到达终点。d[T] = P;
然后就是怎么求 d[i] 了,根据 i 的类型, i >=26 ,就需要 d[i] + 1;
否则就是 20 里面抽 1;
#include <bits/stdc++.h>
using namespace std; const int maxn = + ;
const long long INF = 1LL << ; int n;
int G[maxn][maxn];
bool mark[maxn];
int p;
int src;
int dest;
long long d[maxn]; int read_node()
{
char ch[];
scanf("%s", ch);
if(ch[] >= 'A' && ch[] <= 'Z') return ch[] - 'A';
else return ch[] - 'a' + ;
} char format_node(int u)
{
return u < ? 'A' + u : 'a' + (u - );
} // 拿着item个东西去结点next,还剩多少个东西
long long forward(long long item, int next)
{
if(next < ) return item - (item + ) / ;
return item - ;
} // 至少要拿着多少个东西到达结点u,交税以后还能剩d[u]个东西
long long back(int u)
{
if(u >= ) return d[u]+;
long long X = d[u] * / ; // 初始值
while(forward(X, u) < d[u]) X++; // 调整
return X;
} void solve()
{
n = ; // 总是有52个结点
memset(mark, , sizeof(mark));
d[dest] = p;
mark[dest] = ;
for(int i = ; i < n; i++) if(i != dest)
{
d[i] = INF;
if(G[i][dest]) d[i] = back(dest);
} // Dijkstra主过程,逆推
while(!mark[src])
{
// 找最小的d
int minu = -;
for(int i = ; i < n; i++) if(!mark[i])
{
if(minu < || d[i] < d[minu]) minu = i;
}
mark[minu] = ;
// 更新其他结点的d
for(int i = ; i < n; i++) if(!mark[i])
{
if(G[i][minu]) d[i] = min(d[i], back(minu));
}
} printf("%lld\n", d[src]);
printf("%c", format_node(src));
int u = src;
long long item = d[src];
while(u != dest)
{
int next;
for(next = ; next < n; next++) // 找到第一个可以走的结点
if(G[u][next] && forward(item, next) >= d[next]) break;
item = d[next];
printf("-%c", format_node(next));
u = next;
}
printf("\n");
} int main()
{
int kase = ;
while(scanf("%d", &n) == && n >= )
{
memset(G, , sizeof(G));
for(int i = ; i < n; i++)
{
int u = read_node();
int v = read_node();
if(u != v) G[u][v] = G[v][u] = ;
}
scanf("%d", &p);
src = read_node();
dest = read_node();
printf("Case %d:\n", ++kase);
solve();
}
return ;
}
Uva 10537 过路费的更多相关文章
- 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 过路费(最短路,经典变形)
题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可.现在如果要让p货物到达e,那么从起点出发最少要准备 ...
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
- 【Toll!Revisited(uva 10537)】
题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra! 在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...
- uva 10537 Toll! Revisited(优先队列优化dijstra及变形)
Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...
- UVa 10537 The Toll! Revisited (最短路)
题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...
- The Toll! Revisited UVA - 10537(变形。。)
给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...
- 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= ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
随机推荐
- super()和this()的区别
1)调用super()必须写在子类构造方法的第一行,否则编译不通过.每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错. 2)supe ...
- NeuSoft(4)编写字符设备驱动
1.要求:实现简单的字符设备驱动程序 2.源码清单 #include <linux/module.h> #include <linux/types.h> #include &l ...
- Vue 双向数据绑定原理分析 以及 Object.defineproperty语法
第三方精简版实现 https://github.com/luobotang/simply-vue Object.defineProperty 学习,打开控制台分别输入以下内容调试结果 userInfo ...
- Android课程---课下练习(表格、线性和相对布局)
1.表格布局 练习代码: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns: ...
- Oracle 常用数据类型(转)
varchar2(6) 张三 --在jbk中是两个字节,在utm中是三个字节char(6) 张 三 --可以确定长度的用charclob --大存储,没事少用,当多余4000字节时,会用lob来存储, ...
- 【iCore3 双核心板】例程三:EXTI中断输入实验——读取ARM按键状态
实验指导书及代码包下载: http://pan.baidu.com/s/1o6xToN4 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- JSTL标签出错:<c:forEach var="book" items="${requestScope.books}" varStatus="status">
今天在运行书里的JSTL标签代码的时候出错,总结一下: 问题1.The JSP specification requires that an attribute name is preceded by ...
- spring和mybatis集成,自动生成model、mapper,增加mybatis分页功能
软件简介 Spring是一个流行的控制反转(IoC)和面向切面(AOP)的容器框架,在java webapp开发中使用广泛.http://projects.spring.io/spring-frame ...
- larave5.1l队列
官方文档http://laravel.com/docs/5.1/queues#dealing-with-failed-jobs 1.队列容器设置为数据库 config/queue.php 'defau ...
- android NumberPicker 数组越界的坑
被这个问题耽误了一个多小时... 直接上解决方案,参考红色部分. private void initViews() { wheel = (NumberPicker) findViewById(R.id ...