difkstra + 路径输出

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [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

[Submit]   [Go Back]   [Status]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; typedef long long int LL;
const LL INF=4557430888798830399LL; int getID(char c)
{
if(c>='A'&&c<='Z')
return c-'A'+1;
else return c-'a'+27;
} struct Edge
{
int to,next;
}edge[6000]; int Adj[60],Size=0; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void Add_Edge(int u,int v)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} int n,S,E;
LL dist[60],toll;
bool used[60]; LL get_toll(int E,LL toll)
{
if(E>=27)
{
return toll+1LL;
}
else
{
LL t=toll/19LL;
if(toll%19LL) t++;
return toll+t;
}
} int dijkstra()
{
memset(dist,63,sizeof(dist));
memset(used,false,sizeof(used));
dist[E]=toll;
for(int j=1;j<=60;j++)
{
int mark=-1;
LL mindist=INF;
for(int i=1;i<=52;i++)
{
if(used[i]) continue;
if(dist[i]<mindist)
{
mark=i; mindist=dist[i];
}
}
if(mark==-1) break;
used[mark]=true;
LL temp=get_toll(mark,dist[mark]);
for(int i=Adj[mark];~i;i=edge[i].next)
{
int v=edge[i].to;
if(used[v]) continue;
if(dist[v]>temp)
dist[v]=temp;
}
}
} vector<int> road; LL get_down(int E,LL toll)
{
if(E>=27)
{
return 1;
}
else
{
LL t=toll/20LL;
if(toll%20LL) t++;
return t;
}
} void dfs(int u,int fa)
{
road.push_back(u);
int temp=9999;
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
if(dist[u]-get_down(v,dist[u])==dist[v])
{
temp=min(temp,v);
}
}
if(temp!=9999)
dfs(temp,u);
} int main()
{
int cas=1;
while(scanf("%d",&n)!=EOF&&~n)
{
init();
char opp[2][30];
for(int i=0;i<n;i++)
{
scanf("%s%s",opp[0],opp[1]);
int u=getID(opp[0][0]);
int v=getID(opp[1][0]);
Add_Edge(u,v);
Add_Edge(v,u);
}
scanf("%lld%s%s",&toll,opp[0],opp[1]);
S=getID(opp[0][0]); E=getID(opp[1][0]);
dijkstra();
printf("Case %d:\n%lld\n",cas++,dist[S]);
road.clear();
dfs(S,S);
for(int i=0,sz=road.size();i<sz;i++)
{
if(i) putchar('-');
char xxx;
if(road[i]<=26)
xxx='A'+road[i]-1;
else
{
road[i]-=27;
xxx='a'+road[i];
}
printf("%c",xxx);
}
putchar(10);
}
return 0;
}

UVA10537 Toll! Revisited的更多相关文章

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

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

  2. uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

    Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...

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

    题目: Sample Input1a Z19 a Z5A DD XA bb cc X39 A X-1Sample OutputCase 1:20a-ZCase 2:44A-b-c-X 题意: 有两种节 ...

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

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

  5. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...

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

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

  7. 【Toll!Revisited(uva 10537)】

    题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra!       在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...

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

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

  9. The Toll! Revisited UVA - 10537(变形。。)

    给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...

随机推荐

  1. SQL开发中容易忽视的一些小地方(二)

    原文:SQL开发中容易忽视的一些小地方(二) 目的:继上一篇:SQL开发中容易忽视的一些小地方(一) 总结SQL中的null用法后,本文我将说说表联接查询. 为了说明问题,我创建了两个表,分别是学生信 ...

  2. Run Book Automation - RBA系统介绍

    什么是RBA系统? 介绍这个系统之前, 先介绍一下开发这个系统的公司. BMC BMC软件公司是全球率先的业务服务管理解决方式的开发及供应商,现为全球第六大独立软件公司. 通过帮助用户主动提升服务.降 ...

  3. 有关windows在调试ODOO8.0有些问题

    继Ubuntu建筑物8.0调试环境后,,尝试windows设置开发环境. 最后的调试和开发,或将返回Linux环境,由于前一段时间手贱,改变分区表,该grub搞哈.哎!后来重建mbr,手动检索分区表( ...

  4. hdu3836联通的强还原性点

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  5. 接收一个IT招聘促销信息,试着想参加,有兴趣的可以携手并进,共同。

    时隆重举行! 招聘的企业: (个人认为,中智.也买酒还是非常有吸引力的) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHdia2Zj/font/5a6L ...

  6. 文件I/O之-打开文件在内核中的表示

    在unix系统中,一切皆文件.系统把文件夹.字符设备.块设备.套接字都当做文件来对待.对于文件的操作,使用I/O函数,这里所说的I/O函数是指系统调用.大多数文件的I/O要用到这5个函数:open.r ...

  7. ASP.NET MVC:Razor 引入命名空间

    原文:ASP.NET MVC:Razor 引入命名空间 页面中引用 c# @using MvcApplication83.Models @using MvcApplication83.Common 行 ...

  8. UVA11080- Place the Guards(二分图染色)

    题目链接 题意:放最少的士兵去监视全部的道路, 但士兵不可相邻,符合的话,就输出最少的士兵数,否则输出-1 思路:事实上就是二分图染色,即黑白染色,然后选择黑白染色最少的那个颜色累加,但要注意可能有多 ...

  9. zoj 3203 Light Bulb,三分之二的基本问题

    Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...

  10. 面向对象三大特征之继承(extends)——Java笔记(六)

    继承:            从一般到特殊的关系,是一种拓展关系,子类对象是父类的一种,也可称为”is a“的关系 泛化:         把子类里的共性抽取到父类里的来的过程 特化:         ...