uva 10537 - The Toll! Revisited (逆向思维最短路)

C - The Toll! Revisited
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit  Status

Description

Problem G

Toll! Revisited

Input: Standard Input

Output: Standard Output

Time Limit: 1 Second

 

Sindbad the Sailor sold 66 silver spoons to the Sultan of Samarkand. The selling was quite easy; but delivering was complicated. The items were transported over land, passing through several towns and villages. Each town and village demanded an entry toll. There were no tolls for leaving. The toll for entering a village was simply one item. The toll for entering a town was one piece per 20 items carried. For example, to enter a town carrying70 items, you had to pay 4 items as toll. The towns and villages were situated strategically between rocks, swamps and rivers, so you could not avoid them.

 

Figure 1: To reach Samarkand with 66 spoons, traveling through a town followed by two villages, you must start with 76 spoons.

 

Figure 2: The best route to reach X with 39 spoons, starting from A, is A->b->c->X, shown with arrows in the figure on the left. The best route to reach X with 10 spoons is A->D->X, shown in the figure on the right. The figures display towns as squares and villages as circles.

 

Predicting the tolls charged in each village or town is quite simple, but finding the best route (the cheapest route) is a real challenge. The best route depends upon the number of items carried. For numbers up to 20, villages and towns charge the same. For large numbers of items, it makes sense to avoid towns and travel through more villages, as illustrated in Figure 2.

 

You must write a program to solve Sindbad’s problem. Given the number of items to be delivered to a certain town or village and a road map, your program must determine the total number of items required at the beginning of the journey that uses a cheapest route. You will also have to find the cheapest route. If there is more than one such route, print the lexicographically smallest one (A-n-d is smaller than a-n-d).


Input

The input consists of several test cases. Each test case consists of two parts: the roadmap followed by the delivery details.

 

The first line of the roadmap contains an integer n, which is the number of roads in the map (0 <= n). Each of the next n lines contains exactly two letters representing the two endpoints of a road. A capital letter represents a town; a lower case letter represents a village. Roads can be traveled in either direction.

 

Following the roadmap is a single line for the delivery details. This line consists of three things: an integer p (0 < p < 1000000000) for the number of items that must be delivered, a letter for the starting place, and a letter for the place of delivery. The roadmap is always such that the items can be delivered.

 

The last test case is followed by a line containing the number -1.

 

Output

The output consists of three lines for each test case. First line displays the case number, second line shows the number of items required at the beginning of the journey and third line shows the path according to the problem statement above. Actually, the path contains all the city/village names that Sindbad sees along his journey. Two consecutive city/village names in the path are separated by a hyphen.

 

Sample Input                             Output for Sample Input

1

a Z

19 a Z

5

A D

D X

A b

b c

c X

39 A X

-1

Case 1:

20

a-Z

Case 2:
44

A-b-c-X

Orignal Problem: ACM ICPC World Finals 2003, Enhanced by SM, Member of EPP

题意:运送物品需要过路费,进入村庄收费1,进入城镇收费1/20,离开不收费,求出发时携带最少数目。

分析:从终点往前推,dijksta,dis[j]=min(dis[j],worth(dis[mark],mark))更新节点数据(小心数据大小,用long long)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
using namespace std;
const int MAXN=60;
const ll INF=1ll<<62;
bool Map[MAXN][MAXN];
ll dis[MAXN];
bool vis[MAXN];
int n,m,s,t;
int p[MAXN];
ll want;

ll worth(ll num,int a)
{
	if(a>=26)return num+1;
	ll tmp=(ll)num*20/19;
	while(tmp-(tmp+19)/20<num)tmp++;
	return tmp;
}
void dist(int s,int t)
{
    int mark,i,j;
	ll temp;
	memset(vis,0,sizeof(vis));
	for(i=0;i<n;i++)
		dis[i]=INF;
	for(i=0;i<n;i++)
		if(Map[s][i])dis[i]=worth(want,s),p[i]=s;
	dis[s]=want;p[s]=-1;
	vis[s]=1;
	for(i=1;i<n;i++)
	{
		temp=INF;
		for(j=0;j<n;j++)
			if(!vis[j]&&dis[j]<temp)
				temp=dis[mark=j];
		if(temp==INF)break;
		vis[mark]=1;

		for(j=0;j<n;j++)
			if(!vis[j]&&Map[mark][j]&&dis[j]>worth(dis[mark],mark))
				dis[j]=worth(dis[mark],mark),p[j]=mark;
	}
}
void showpath(int x)
{
	if(p[x]==-1){printf("%c",x>=0&&x<=25?x+'A':x-26+'a');return ;}
	printf("%c-",x>=0&&x<=25?x+'A':x-26+'a');
	showpath(p[x]);
}
int main()
{
	//freopen("123.txt","r",stdin);
	int i,cas=0;char c1,c2;
	map<char,int>index;
	for(i=0;i<26;i++)
	{
		index['A'+i]=i;
		index['a'+i]=i+26;
	}
	n=52;
	while(~scanf("%d",&m))
	{
		if(m==-1)break;
		memset(Map,0,sizeof Map);
		for(i=0;i<m;i++)
		{
			scanf("\n%c %c",&c1,&c2);
			Map[index[c1]][index[c2]]=Map[index[c2]][index[c1]]=1;
		}
		scanf("%lld %c %c",&want,&c1,&c2);
		if(m==0){printf("Case %d:\n%lld\n",++cas,want);printf("%c\n",c1);continue;}
		s=index[c1];t=index[c2];
		dist(t,s);
		printf("Case %d:\n%lld\n",++cas,dis[s]);
		showpath(s);
		printf("\n");
	}
	return 0;
}




你可能感兴趣的:(uva 10537 - The Toll! Revisited (逆向思维最短路))