HDU 2923-Einbahnstrasse(floyd&&字符串输入)

来源:互联网 发布:淘宝推广文案怎么写 编辑:程序博客网 时间:2024/06/05 02:46

address : http://acm.hdu.edu.cn/showproblem.php?pid=2923

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

题目意思就是 第一行3个数 m n t
代表了有m个地点, n个车坏的地点, t条道路(并且道路分单向和双向)

接下来一行有 n+1 个 字符串代表了 第一个是 修车厂, 剩余n个是车坏的地点

下面有 t行 代表t条道路, a <-20-> b 代表a 到 b 有一个双向的 运费为20

求解从汽车修理总部将所有坏掉的车运回来的最小花费。

用的 floyd 过了 这道题肯定是用(Dijkstra) 更省时间一点。

难点:
对输入的处理, 输入都以自己串形式, 所以我们可以用map来存储
map< string , int > 类型的 string 对应地点的位置, int 对应其位置的下标。 然后存储到 建立的邻接矩阵中

看学姐 是用的vector 来存储车坏的地点的位置 , 但正常用数组也是一样的。 所以在这我用的也是vector。

直接看代码吧:、

#include <iostream>#include <bits/stdc++.h>using namespace std;#define maxn 10010const int Max = 0x3f3f3f3f;int n,m,t;int Map[maxn][maxn];int dist[maxn];int vist[maxn];int en;void floyd(){    int i,j,k;    for (k=1; k<=n; k++)        for(i=1; i<=n; i++)            for (j=1; j<=n; j++)                Map[i][j]=min( Map[i][j],Map[i][k]+Map[k][j]);}int main(){    int s=0;    while (scanf("%d%d%d",&n,&m,&t))    {        char str[12], str1[12], cost[12];        map<string, int>st;        vector<int>v;        int num=1;        if (n==0&&m==0&&t==0)            break;        for (int i=1; i<=n; i++)   //初始化        {            for (int j=1; j<=n; j++)            {                Map[i][j] = Max;                if (i==j)                    Map[i][j] = 0;            }        }        for (int i=0; i<=m; i++)        {            scanf("%s",str);            if (!st[str])            {                //cout<<st[str]<<endl;                st[str] = num++;            }            v.push_back(st[str]);        }        for (int i=0; i<t; i++)        {            int flag_go=0, flag_back=0, sum=0;            scanf("%s%s%s",str,cost,str1);            if (!st[str])                st[str] = num++;            if (!st[str1])                st[str1] = num++;            for(int i=0; i<strlen(cost); i++)            {                if (cost[i]=='>')                    flag_go = 1;                if (cost[i] == '<')                    flag_back = 1;                if (cost[i]>='0'&&cost[i]<='9')                    sum  = sum*10 + cost[i]-'0';            }            if (flag_go == 1)            {                //cout<<"!"<<sum<<" "<<st[str]<<" "<<st[str1]<<endl;                if (sum < Map[st[str]][st[str1]])                    Map[st[str]][st[str1]] = sum;            }            if (flag_back == 1)            {                if (sum<Map[st[str1]][st[str]])                    Map[st[str1]][st[str]] = sum;            }        }        floyd();        int ans=0;        for (int i=0; i<v.size(); i++)        {            //cout<<Map[1][v[i]]<<" "<<Map[v[i]][1]<<endl;            ans += Map[1][v[i]]+Map[v[i]][1];        }        printf("%d. %d\n",++s,ans);    }    return 0;}