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
随机推荐
- Jquery各个版本的区别
一: 一般原则是越新越好,jQuery版本是在不断进步和发展的,最新版是当时最高技术水平,也是最先进的技术理念. 但个人的角度来看.是最新版本x.x.0的上一版本最好.比如说1.10.0版,上一版本是 ...
- C# 在 vs2010 上开发 ActiveX 控件 【千辛万苦啊~】
由于B/S项目中需要使用到读卡器的功能,但是由于厂家只有提供一个 读卡的dll,那么怎样能在客户端使用读卡器呢,那么进过一番查找,最总决定使用ActiveX 控件来做.由于是第一次接触到 Active ...
- Python 基础 - 对文本的处理
Python 对文本文件的处理. 对文本操作之前,必须要先open 这个文件,open完成之后需要close . # -*- coding: utf-8 -*- f=open('test.txt',' ...
- SpringMVC表单标签简介
在使用SpringMVC的时候我们可以使用Spring封装的一系列表单标签,这些标签都可以访问到ModelMap中的内容.下面将对这些标签一一介绍. 在正式介绍SpringMVC的表单标签之前,我们需 ...
- Mysql日期时间大全
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,--7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...
- wa~哭笑天使
#include <stdio.h> #define M 301 int r[M], c[M]; int main() { int k, m, n, i, j, sum, flag, t; ...
- 杭电ACM 1998奇数阶魔方
#include<stdio.h>#include <string.h>int main(){ int n,m; int a[40][40]={0}; scanf(" ...
- C# 计时器
一.Stopwatch 主要用于测试代码段使用了多少时间 使用方法: Stopwatch sw=new Stopwatch(); sw.Start(); ... sw.Stop(); Console. ...
- A trip through the Graphics Pipeline 2011_13 Compute Shaders, UAV, atomic, structured buffer
Welcome back to what’s going to be the last “official” part of this series – I’ll do more GPU-relate ...
- my first go
package main import "fmt" func main() { var num1 int =5 for num1>0 { fmt.Println(" ...