天天看點

HDU 2923-Einbahnstrasse(floyd&&字元串輸入)

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 10010
const int Max = ;
int n,m,t;
int Map[maxn][maxn];
int dist[maxn];
int vist[maxn];
int en;

void floyd()
{
    int i,j,k;
    for (k=; k<=n; k++)
        for(i=; i<=n; i++)
            for (j=; j<=n; j++)
                Map[i][j]=min( Map[i][j],Map[i][k]+Map[k][j]);
}


int main()
{
    int s=;
    while (scanf("%d%d%d",&n,&m,&t))
    {
        char str[], str1[], cost[];
        map<string, int>st;
        vector<int>v;
        int num=;

        if (n==&&m==&&t==)
            break;
        for (int i=; i<=n; i++)   //初始化
        {
            for (int j=; j<=n; j++)
            {
                Map[i][j] = Max;
                if (i==j)
                    Map[i][j] = ;
            }
        }

        for (int i=; i<=m; i++)
        {
            scanf("%s",str);
            if (!st[str])
            {
                //cout<<st[str]<<endl;
                st[str] = num++;
            }
            v.push_back(st[str]);
        }

        for (int i=; i<t; i++)
        {
            int flag_go=, flag_back=, sum=;
            scanf("%s%s%s",str,cost,str1);
            if (!st[str])
                st[str] = num++;
            if (!st[str1])
                st[str1] = num++;

            for(int i=; i<strlen(cost); i++)
            {
                if (cost[i]=='>')
                    flag_go = ;
                if (cost[i] == '<')
                    flag_back = ;

                if (cost[i]>='0'&&cost[i]<='9')
                    sum  = sum* + cost[i]-'0';
            }

            if (flag_go == )
            {
                //cout<<"!"<<sum<<" "<<st[str]<<" "<<st[str1]<<endl;
                if (sum < Map[st[str]][st[str1]])
                    Map[st[str]][st[str1]] = sum;
            }

            if (flag_back == )
            {
                if (sum<Map[st[str1]][st[str]])
                    Map[st[str1]][st[str]] = sum;
            }
        }
        floyd();

        int ans=;
        for (int i=; i<v.size(); i++)
        {
            //cout<<Map[1][v[i]]<<" "<<Map[v[i]][1]<<endl;
            ans += Map[][v[i]]+Map[v[i]][];
        }
        printf("%d. %d\n",++s,ans);

    }
    return ;
}