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
随机推荐
- Shell编程中括号判断中赋值语句和判断语句
#!/bin/bash declare var="xxx" # without space and use one = #1.judge whether the assignmen ...
- 【iCore3 双核心板】例程十五:USB_CDC实验——高速数据传输
实验指导书及代码包下载: http://pan.baidu.com/s/1gemYjz9 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 点击某个按钮弹出 photoswip
var openPhotoSwipe = function() { var pswpElement = document.querySelectorAll('.pswp')[0]; // build ...
- springmvc Failed to load resource: the server responded with a status of 404 (Not Found)
jsp页面导入css.js提示上述问题. Spring对静态资源的请求做专门处理 <!-- 对静态资源的请求 --><mvc:resources location="/js ...
- Ubuntu 常用工具、指令安装
修改source list,使用阿里云的软件源 sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list sed - ...
- SqlServer 不是主键 如何自增
SqlServer 不是主键 如何自增:INSERT INTO dbo.表 VALUES('14-19周',0,(select COUNT(1) from dbo.表)+1) (select COUN ...
- Any changes made by a writer will not be seen by other users of the database until the changes have been completed
https://en.wikipedia.org/wiki/Multiversion_concurrency_control Multiversion concurrency control (MCC ...
- Docker三剑客之Swarm介绍
DockOne技术分享(二十): 我用swarm在多台物理机调度管理容器,用ovs实现跨主机的容器互联问题 [编者的话]Swarm项目是Docker公司发布三剑客中的一员,用来提供容器集群服务,目的是 ...
- http://bbs.tianya.cn/post-stocks-1665898-1.shtml
http://bbs.tianya.cn/post-stocks-1295707-1.shtml 写过交易计划吗?有没有提前关注过某些板块或者某些股票呢?自选股里边有多少只股票?你平时复盘都是什么习惯 ...
- fiddler实现手机端抓包(代理)
一.fiddler设置代理 1.打开Fiddler->Tools->Fiddler Options在Connection面板里将 Allow remote computers to con ...