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
随机推荐
- bzoj1150: [CTSC2007]数据备份Backup--贪心+优先队列维护堆
题目大意:将k对点两两相连,求最小长度 易证得,最优方案中,相连的办公楼一定是取相邻的比取不相邻的要更优 然后就可以用贪心来做这道题了.. 之前向CZL大神学习了用堆来贪心的做法orz 大概思路就是将 ...
- zju(3)内核编译与运行
1.实验目的 学习和掌握Linux配置和编译的基本步骤. 二.实验内容 1. 对Linux内核及用户程序进行配置: 2. 编译生成内核映像文件: 3. 把编译的映像文件烧写到FLASH中,查看运行结果 ...
- A+B Problem III-(涉及误差)NYOJ-477
描述求A+B是否与C相等. 输入 T组测试数据. 每组数据中有三个实数A,B,C(-10000.0<=A,B<=10000.0,-20000.0<=C<=20000.0) ...
- Linux内核设计第一周 ——从汇编语言出发理解计算机工作原理
Linux内核设计第一周 ——从汇编语言出发理解计算机工作原理 作者:宋宸宁(20135315) 一.实验过程 图1 编写songchenning5315.c文件 图2 将c文件汇编成32位机器语言 ...
- 盒模型中--border
三要素:宽border-width,形状border-style,颜色border-color <style> div{ width:300px; height:300px; backgr ...
- php递归遍历目录计算其大小(文件包括目录和普通文件)
<?php function countdir($path){ $size = 0; //size = 0; 跟 size = null; 怎么结果不一样 $path = rtrim($path ...
- cocos2dx 3.x(移动修改精灵坐标MoveTo与MoveBy)
// // MainScene.cpp // helloworld // // Created by apple on 16/11/8. // // #include "MainScene. ...
- 20145320《Java程序设计》第四次实验报告
20145320<Java程序设计>第四次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.26 15: ...
- Number类型方法
//1.toString(); 转换成字符串 var s=123; console.log(typeof s.toString()); //string //2.toLocaleString() ...
- Leetcode: Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...