题目:

Sample Input
1
a Z
19 a Z
5
A D
D X
A b
b c
c X
39 A X
-1
Sample Output
Case 1:
20
a-Z
Case 2:
44
A-b-c-X

题意:

  有两种节点,一种是大写字母,一种是小写字母。首先输入m条边,当经过小写字母时需要付一单位的过路费,当经过大写字母时,要付当前财务的1/20做过路费(向上取整)。问在起点最少需要带多少物品使到达终点时还有k个物品。当有多条符合条件的路径时输出字典序最小的一个。

分析:

  逆推进行最短路,输出时顺序输出并选择最小字典序即可。(超级讨厌字符输入有没有,TAT,RE好多遍,记得要long long)

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define Maxn 10100
#define LL long long int s,e; struct node
{
int x,y,next;
}t[**Maxn];int len; int first[Maxn];
LL dis[Maxn];
bool inq[Maxn]; queue<int > q; void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;
t[len].next=first[x];first[x]=len;
} int mymin(int x,int y) {return x<y?x:y;} void spfa(int s,int w)
{
memset(dis,,sizeof(dis));
memset(inq,,sizeof(inq));
while(!q.empty()) q.pop();
dis[s]=w;inq[s]=;q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();inq[x]=;
LL now;
if(x>) now=dis[x]+;
else now=(LL)ceil(dis[x]*1.0*/);
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>now)
{
dis[y]=now;
if(!inq[y])
{
q.push(y);
inq[y]=;
}
}
}
}
} void pri(int x)
{
if(x>) printf("%c",x-+'a');
else printf("%c",x-+'A'); }
void ffind(int x,int z)
{
pri(x);
if(x==z) return;printf("-");
int mn=;
LL a,b;
a=dis[x]-;
b=dis[x]-(LL)ceil(dis[x]*1.0/);
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(y==x) continue;
LL now;
if(y<=) now=b;
else now=a;
if(now==dis[y]) mn=mymin(mn,y);
}
if(mn<) ffind(mn,z);
} int main()
{
int n,kase=;
while()
{
scanf("%d",&n);getchar();
if(n==-) break;
char a,b,c;
int x,y;
memset(first,,sizeof(first));
len=;
for(int i=;i<=n;i++)
{
scanf("%c%c%c",&a,&c,&b);getchar();
if(a>='a'&&a<='z') x=a-'a'+;
else x=a-'A'+;
if(b>='a'&&b<='z') y=b-'a'+;
else y=b-'A'+;
ins(x,y);ins(y,x);
}
int p;scanf("%d%c%c%c%c",&p,&c,&a,&c,&b);getchar();
if(a>='a'&&a<='z') x=a-'a'+;
else x=a-'A'+;
if(b>='a'&&b<='z') y=b-'a'+;
else y=b-'A'+;
spfa(y,p);
printf("Case %d:\n",++kase);
printf("%lld\n",dis[x]);
ffind(x,y);printf("\n");
}
return ;
}

[UVA10537]

2016-04-06 13:35:13

【UVA10537】The Toll! Revisited (逆推最短路)的更多相关文章

  1. UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)

    题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可.现在如果要让p货物到达e,那么从起点出发最少要准备 ...

  2. UVA-10537 The Toll! Revisited (dijkstra)

    题目大意:每经过一个地方就要交出相应的货物作为过路费,问将一批货物从起点运到终点,最少需要携带多少货物? 题目分析:在每一站交的过路费由当前拥有的货物量来决定,所以,要以终点为源点,求一次单源最短路即 ...

  3. 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= ...

  4. UVa 10537 The Toll! Revisited (最短路)

    题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...

  5. UVA10537 Toll! Revisited

    difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  6. uva10537 dijkstra + 逆推

    21:49:45 2015-03-09 传送 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8& ...

  7. UVA116Unidirectional TSP(DP+逆推)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18206 题意:M*N的数阵,从左边一列到右边一列走过的数的和的最小.并输出路 ...

  8. UVA 10537 - The Toll! Revisited(dijstra扩张)

    UVA 10537 - The Toll! Revisited option=com_onlinejudge&Itemid=8&page=show_problem&catego ...

  9. HDU 5844 LCM Walk(数学逆推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...

随机推荐

  1. Linux 安装 Python3.5

    1. 准备 操作系统:Red Hat Enterprise Linux Server release 6.5 (Santiago) Python 安装包:Python-3.5.2.tgz 下载地址:h ...

  2. 如何正确合理的建立MYSQL数据库索引

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...

  3. oracle常用数据类型

    oracle中常用数据类型分为三大类:

  4. javascript实现继承的6种方式

    /*1.原型链继承*/ function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = funct ...

  5. html学习的一些问题

    1,什么是 W3C标准?w3c 标准不是一个标准,而是一系列标准,包括:结构标准,表现标准,动作标准. 2,内链元素和块状元素的区别内链元素允许与其他内链元素位于同一行,没有宽和高,如果想设置宽和搞, ...

  6. [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)

    点击下载 CsvHelper.rar 这个类是关于Csv文件的一些高级操作1.DataTable导出到CSV2.将Csv读入DataTable看下面代码吧 /// <summary> // ...

  7. SQL查询一些浅薄的结论

    一些简单的测试结论 在本机经过一些简单的测试,记录数6W条,得出以下结论,不同的硬件环境和数据记录数,可能会有不一样的结论 1.in, or, exists, like, not in , not e ...

  8. CoreAnimation5-图层时间和缓冲

    图层时间 动画的发生是需要持续一段时间的,所以计时对整个概念来说至关重要.在这一章中,我们来看看CAMediaTiming,看看Core Animation是如何跟踪时间的. CAMediaTimin ...

  9. ios专题 - GCD(1)

    什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像 NSOperationQueue,他们都允 ...

  10. ios专题 - CocoaPods - 初次体验

    [原创]http://www.cnblogs.com/luoguoqiang1985 这CocoaPods怎么用呢? 参考官方文章:guides.cocoapods.org/using/using-c ...