【UVA10537】The Toll! Revisited (逆推最短路)

时间:2022-05-03 09:18:27

题目:

【UVA10537】The Toll! Revisited (逆推最短路)

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