HDU 2923-Einbahnstrasse

来源:互联网 发布:淘宝商城舞蹈鞋 编辑:程序博客网 时间:2024/05/24 05:53

Einbahnstrasse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3791    Accepted Submission(s): 1215


Problem Description
Einbahnstra  e (German for a one-way street) is a street on which vehicles should only move in one direction. One reason for having one-way streets is to facilitate a smoother flow of traffic through crowded areas. This is useful in city centers, especially old cities like Cairo and Damascus. Careful planning guarantees that you can get to any location starting from any point. Nevertheless, drivers must carefully plan their route in order to avoid prolonging their trip due to one-way streets. Experienced drivers know that there are multiple paths to travel between any two locations. Not only that, there might be multiple roads between the same two locations. Knowing the shortest way between any two locations is a must! This is even more important when driving vehicles that are hard to maneuver (garbage trucks, towing trucks, etc.)

You just started a new job at a car-towing company. The company has a number of towing trucks parked at the company's garage. A tow-truck lifts the front or back wheels of a broken car in order to pull it straight back to the company's garage. You receive calls from various parts of the city about broken cars that need to be towed. The cars have to be towed in the same order as you receive the calls. Your job is to advise the tow-truck drivers regarding the shortest way in order to collect all broken cars back in to the company's garage. At the end of the day, you have to report to the management the total distance traveled by the trucks.
 

Input
Your program will be tested on one or more test cases. The first line of each test case specifies three numbers (N , C , and R ) separated by one or more spaces. The city has N locations with distinct names, including the company's garage. C is the number of broken cars. R is the number of roads in the city. Note that 0 < N < 100 , 0<=C < 1000 , and R < 10000 . The second line is made of C + 1 words, the first being the location of the company's garage, and the rest being the locations of the broken cars. A location is a word made of 10 letters or less. Letter case is significant. After the second line, there will be exactly R lines, each describing a road. A road is described using one of these three formats:


A -v -> B
A <-v - B
A <-v -> B


A and B are names of two different locations, while v is a positive integer (not exceeding 1000) denoting the length of the road. The first format specifies a one-way street from location A to B , the second specifies a one-way street from B to A , while the last specifies a two-way street between them. A , ``the arrow", and B are separated by one or more spaces. The end of the test cases is specified with a line having three zeros (for N , C , and R .)

The test case in the example below is the same as the one in the figure. 

 

Output
For each test case, print the total distance traveled using the following format:


k . V


Where k is test case number (starting at 1,) is a space, and V is the result.

Sample Input
4 2 5
NewTroy Midvale Metrodale
NewTroy   <-20-> Midvale
Midvale   --50-> Bakerline
NewTroy    <-5-- Bakerline
Metrodale <-30-> NewTroy
Metrodale  --5-> Bakerline
0 0 0
 


Sample Output
1. 80


题意:
Einbahnstra e(德国的单行道)是一条车辆只能向一个方向移动的街道。在两个地点之间可能有多条道路。
知道两个地点之间最短的路是必须的!你刚刚在一家汽车公司开始了一份新工作。该公司有许多拖曳的卡车停在公司的车库。一辆拖车一次只能拖一辆破车,以将其直接拉回公司的车库。你接到了来自城市各个地区的电话,这些电话都是告诉你破车的位置。你的工作是建议司机们以最快的方式把所有的破车都送回公司的车库。
在一天结束的时候,你必须向管理人员报告卡车运输的总距离。

有多组实例测试,每个测试用例的第一行有三个数字(N、C和R)。N代表这个城市有N个地点,名字不同,包括公司的车库。C是坏车的数量。R是城市中道路的数量。
注意,0 < N < 100,0 <=C < 1000,R < 10000。
第二行是C + 1字,第一个是公司车库的位置,其余的是破车的位置。
一个位置是一个由10个字母组成的单词(大小写都有)。
在第二行之后,将有R行,每行描述一条路。
一条路用这三种格式之一来描述:
A - v - >B
A< - v - B
A< - v - > B
A和B是两个不同位置的名称,而v是一个正整数(不超过1000),表示道路的长度。
第一种格式指定从位置A到B的单行道,第二种格式指定从B到A单行道,而最后一种指定了它们之间的双向街道。
测试用例以三个0结束。


分析:
本题需用一个 map 来映射字符串和整数的关系,但是本题需注意的是,我们给地点的编号要从 1 开始,不能从 0 开始,因为 map 数组初始值就全部是 0 ,因为这一点昨天夜晚一直过不了测试数据,如果有和我昨天夜晚一样困惑的宝宝们,可以多加注意一下。因为本题走的路不确定,所以得用弗洛伊德 求出任意两点之间的最短路。



#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<map>using namespace std;#define INF 0x3f3f3f3fint map1[105][105];char cun[1005][15];int n,c,r;int main(){    int l,t,sum,cas=0,bj;    char s[15],h[15];    char st,ed;    while(~scanf("%d %d %d",&n,&c,&r)&&(n+c+r))    {        cas++;        map<string,int>q;        l=1;        for(int i=0;i<=n;i++)        {            for(int j=0;j<=n;j++)            {                if(i==j)                    map1[i][j]=0;                else                    map1[i][j]=INF;            }        }        for(int i=0;i<=c;i++)            scanf("%s",cun[i]);///存车库的位置和破车的位置        for(int i=0;i<r;i++)        {            scanf("%s %c-%d-%c %s",s,&st,&t,&ed,h);            if(!q[s])                q[s]=l++;            if(!q[h])                q[h]=l++;            if(st=='<')                map1[q[h]][q[s]]=min(map1[q[h]][q[s]],t);            if(ed=='>')                map1[q[s]][q[h]]=min(map1[q[s]][q[h]],t);        }        for(int k=1;k<=n;k++)        {            for(int i=1;i<=n;i++)            {                for(int j=1;j<=n;j++)                {                    if(map1[i][j]>map1[i][k]+map1[k][j])                        map1[i][j]=map1[i][k]+map1[k][j];                }            }        }        sum=0;        bj=q[cun[0]];        for(int i=1;i<=c;i++)        {            sum+=map1[bj][q[cun[i]]]+map1[q[cun[i]]][bj];        }        printf("%d. %d\n",cas,sum);    }    return 0;}


 
原创粉丝点击