HDU 2923 Einbahnstrasse

Source: Internet
Author: User
Tags cas hash int size strlen time limit

Link:

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

Topic:

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1117 accepted submission (s): 31

Problem Description

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

Input

Your program would be tested to one or more test cases. The ' a ' of each test case specifies three numbers (N, C, and R) separated by one or more spaces. The city has N locations and 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 < 0<=c < 1000, and R < 10000. The second line is made of C + 1 words, the the being of the location of the "Company" garage, and the rest being the Locat Ions of the broken cars. A location is a word made of letters or less. Letter case is significant. After the second line, there'll 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 are a positive integer (not exceeding 1000) denoting the length of th E Road. The "I" format specifies a one-way street from location A to B, the second specifies a one-way street from B to a, whi Le 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 has three zeros (for N, C, and R.)

The test case in the example below are 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 in 1,) is a spaces, 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

The main effect of the topic:

There is a C car on the road, from the trailer to one after another from the garage to the destination and then drag the bad car back to the garage. Ask all bad cars to return the shortest total distance.

Analysis and Summary:

The problem for input processing is more troublesome ("<-20->" part), the better way is to use sscanf, first input this part into a string, and then the middle of the number with the start from the 3rd, so sscanf (str+2, "%d", &w), Can be extracted from the middle of the number saved to the W.

Then the judgment is two-way or one-way, and it is easy to implement only the first and last character of the string.

After the input processing is complete, use the Floyd algorithm, and then add those paths to the answer.

Code:

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
typedef char STATE[12];  
const int VN = 105;  
const int EN = 10105;  
const int hashsize = 10003;  
     
const int INF = 0x7fffffff;  
int n,c,r;  
int size;  
int HEAD[VN];  
int car[vn*10];  
int POS;  
     
int W[VN][VN];  
        Class hash{Public:void init () {rear = 1;  
    Memset (Head,-1, sizeof (head));  
        int Insert (state &s) {int h = hash (s);  
        int u = head[h];  
            while (U!=-1) {if (strcmp (st[u), s) ==0) return u;  
        U = next[u];  
        } strcpy (St[rear], s);  
        Next[rear] = head[h];  
        HEAD[H] = rear;  
    return rear++;  
        } private:int Hash (char *p) {int sum=0;  
        while (*p) {sum = sum*131 + *p++;}  
    Return (SUM&AMP;0X7FFFFFFF)%hashsize;  
    int rear;  
    int head[hashsize];  
    int NEXT[VN]; State ST[VN];  
     
}hash;  
    inline void init () {size=pos=0;  
    Memset (Head,-1, sizeof (head));  
    Hash.init ();  
        for (int i=0; i<=n; ++i) {w[i][i] = INF;  
    for (int j=i+1; j<=n; ++j) w[i][j] = w[j][i] = INF;  } inline void Floyd () {for (int k=1; k<=n; ++k) for (int i=1; i<=n; ++i) for (int j=1; j<=n;  
    ++J) {if (W[i][k]!=inf && w[k][j]!=inf) w[i][j] = min (w[i][j],w[i][k]+w[k][j));  
    the int main () {State name1, name2;  
    Char str1[20];  
    int U,v,cost,cas=1;  
        while (scanf ("%d%d%d", &n,&c,&r) &&n+c+r) {init ();  
        scanf ("%s", name1);  
        Hash.insert (NAME1);  
            for (int i=0; i<c; ++i) {scanf ("%s", name1);  
        car[pos++] = Hash.insert (name1);  
            for (int i=0; i<r; ++i) {scanf ('%s%s%s ', name1, str1, name2); U = Hash.insert (name1), V = Hash.insert (name2);  
            SSCANF (str1+2, "%d", &cost);  
                if (str1[0]== ' < ' &&str1[strlen (str1) -1]== ' > ') {if (w[u][v]>cost) w[u][v] = cost;  
            if (w[v][u]>cost) w[v][u] = cost;  else if (str1[0]== '-' &&str1[strlen (str1) -1]== ' > ') {if (w[u][v]>cost) w[u][v]  
            = Cost;  
            } else{if (w[v][u]>cost) w[v][u] = cost;  
        } Floyd ();  
        int ans=0;  
        for (int i=0; i<pos; ++i) {ans + = w[1][car[i]]+w[car[i]][1];  
    printf ("%d.%d\n", Cas++,ans);  
return 0; }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.