zoukankan      html  css  js  c++  java
  • 【面试题】Round A China New Grad Test 2014总结

    我也有够懒的,今天才跑来写总结,自觉面壁中…

    上一篇是Practice Round,今天是Round A,五道题。

    每次做完都想说,其实题不难。。但在做的过程中总是会各种卡,只有自己一行一行实现了,才算真正做过一道题,忌眼高手低啊~没做过的先自己做做吧。

    Problem A. Read Phone Number

    Do you know how to read the phone numbers in English? Now let me tell you.

    For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:

    150 1223 3444 reads one five zero one double two three three triple four.

    150 122 33444 reads one five zero one double two double three triple four.

    Here comes the problem:

    Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.

    Rules:

    Single numbers just read them separately.

    2 successive numbers use double.

    3 successive numbers use triple.

    4 successive numbers use quadruple.

    5 successive numbers use quintuple.

    6 successive numbers use sextuple.

    7 successive numbers use septuple.

    8 successive numbers use octuple.

    9 successive numbers use nonuple.

    10 successive numbers use decuple.

    More than 10 successive numbers read them all separately.

    Input

    The first line of the input gives the number of test cases, TT lines|test cases follow. Each line contains a phone number N and the dividing format F, one or more positive integers separated by dashes (-), without leading zeros and whose sum always equals the number of digits in the phone number.

    Output

    For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.

    Limits

    1 ≤ T ≤ 100.

    Small dataset

    1 ≤ length of N ≤ 10.

    Large dataset

    1 ≤ length of N ≤ 100.

    Sample


    Input 
     
     
    3
    15012233444 3-4-4
    15012233444 3-3-5
    12223 2-3
    


    Output 
     
    Case #1: one five zero one double two three three triple four
    Case #2: one five zero one double two double three triple four
    Case #3: one two double two three
    
     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <vector>
     5 using namespace std;
     6 
     7 void ReadPhoneNumber()
     8 {
     9     string times[9] = {"double", "triple", "quadruple", "quintuple", "sextuple", "septuple", "octuple", "nonuple", "decuple"};
    10     string number[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    11     int T;
    12     cin >> T;
    13     for(int i = 1; i <= T; ++i)
    14     {
    15         cout << "Case #" << i << ": ";
    16         string N, F;
    17         cin >> N >> F;
    18         int FInd1 = 0;
    19         int FInd2 = 0;
    20         int NInd1 = 0;
    21         int NInd2 = 1;
    22         int index = 0;
    23         while(FInd2 < F.length())
    24         {
    25             int divNum = 0;
    26             while(FInd2 < F.length() && F[FInd2] != '-')
    27                 FInd2++;
    28             while(FInd1 < FInd2)
    29             {
    30                 divNum = 10 * divNum + F[FInd1] - '0';
    31                 FInd1++;
    32             }
    33             index += divNum;
    34             FInd2++;
    35             FInd1 = FInd2;
    36             int repNum = 1;
    37             int j = 0;
    38             while(j < divNum)
    39             {
    40                 while(NInd2 < index && N[NInd2] == N[NInd1])
    41                 {
    42                     repNum++;
    43                     NInd2++;
    44                 }
    45                 if(NInd2 == N.length())
    46                 {
    47                     if(repNum > 1 && repNum < 11)
    48                     {
    49                         cout << times[repNum-2] << ' ';
    50                         cout << number[N[NInd1]-'0'];
    51                     }
    52                     else
    53                     {
    54                         while(NInd1 < NInd2 - 1)
    55                         {
    56                             cout << number[N[NInd1]-'0'] << ' ';
    57                             NInd1++;
    58                         }
    59                         cout << number[N[NInd1]-'0'];
    60                     }
    61                 }
    62                 else{
    63                     if(repNum > 1 && repNum < 11)
    64                     {
    65                         cout << times[repNum-2] << ' ';
    66                         cout << number[N[NInd1]-'0'] << ' ';
    67                     }
    68                     else
    69                     {
    70                         while(NInd1 < NInd2)
    71                         {
    72                             cout << number[N[NInd1]-'0'] << ' ';
    73                             NInd1++;
    74                         }
    75                     }
    76                 }
    77                 j += repNum;
    78                 repNum = 1;
    79                 NInd1 = NInd2;
    80                 NInd2++;
    81             }
    82         }
    83         if(i < T)
    84             cout << endl;
    85     }
    86 }
    87 
    88 int main()
    89 {
    90     freopen("A-large-practice.in", "r", stdin);
    91     freopen("A-large-practice.out", "w", stdout);
    92     ReadPhoneNumber();
    93     return 0;
    94 }
    View Code

    Problem B. Rational Number Tree

    Consider an infinite complete binary tree where the root node is 1/1 and left and right childs of node p/q are p/(p+q) and (p+q)/q, respectively. This tree looks like:
             1/1
        ______|______
        |           |
       1/2         2/1
     ___|___     ___|___
     |     |     |     |
    1/3   3/2   2/3   3/1
    ...
    
    It is known that every positive rational number appears exactly once in this tree. A level-order traversal of the tree results in the following array:
    1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, ...

    Please solve the following two questions:

    1. Find the n-th element of the array, where n starts from 1. For example, for the input 2, the correct output is 1/2.
    2. Given p/q, find its position in the array. As an example, the input 1/2 results in the output 2.

    Input

    The first line of the input gives the number of test cases, TT test cases follow. Each test case consists of one line. The line contains a problem id (1 or 2) and one or two additional integers:

    1. If the problem id is 1, then only one integer n is given, and you are expected to find the n-th element of the array.
    2. If the problem id is 2, then two integers p and q are given, and you are expected to find the position of p/q in the array. 

    Output

    For each test case:

    1. If the problem id is 1, then output one line containing "Case #x: p q", where x is the case number (starting from 1), and pq are numerator and denominator of the asked array element, respectively.
    2. If the problem id is 2, then output one line containing "Case #x: n", where x is the case number (starting from 1), and n is the position of the given number. 

    Limits

    1 ≤ T ≤ 100; p and q are relatively prime.

    Small dataset

    1 ≤ npq ≤ 216-1; p/q is an element in a tree with level number ≤ 16.

    Large dataset

    1 ≤ npq ≤ 264-1; p/q is an element in a tree with level number ≤ 64.

    Sample


    Input 
     

    Output 
     
    4
    1 2
    2 1 2
    1 5
    2 3 2
    
    Case #1: 1 2
    Case #2: 2
    Case #3: 3 2
    Case #4: 5
    
     1 #include <iostream>
     2 #include <fstream>
     3 using namespace std;
     4 
     5 void RationalNumberTreeId1(unsigned long long n, unsigned long long &p, unsigned long long &q)
     6 {
     7     if (n == 1)
     8     {
     9         p = 1;
    10         q = 1;
    11         return;
    12     }
    13     RationalNumberTreeId1(n/2, p, q);
    14     if((n & 1) == 1)
    15     {
    16         p = p+q;
    17         return;
    18     }
    19     else
    20     {
    21         q = p+q;
    22         return;
    23     }
    24 }
    25 
    26 void RationalNumberTreeId2(unsigned long long p, unsigned long long q, unsigned long long &n)
    27 {
    28     if(p == 1 && q == 1)
    29     {
    30         n = 1;
    31         return;
    32     }
    33     if(p > q)
    34     {
    35         RationalNumberTreeId2(p - q, q, n);
    36         n = 2 * n + 1;
    37     }
    38     else
    39     {
    40         RationalNumberTreeId2(p, q - p, n);
    41         n = 2 * n;
    42     }
    43 }
    44 
    45 void RationalNumberTree()
    46 {
    47     int T;
    48     cin >> T;
    49     for(int i = 1; i <= T; ++i)
    50     {
    51         cout << "Case #" << i << ": ";
    52         int id;
    53         cin >> id;
    54         switch(id)
    55         {
    56         case(1):
    57             unsigned long long n1;
    58             cin >> n1;
    59             unsigned long long p1,q1;
    60             RationalNumberTreeId1(n1, p1, q1);
    61             cout << p1 << " " << q1;
    62             break;
    63         case(2):
    64             unsigned long long p2, q2;
    65             cin >> p2 >> q2;
    66             unsigned long long n2 = 0;
    67             RationalNumberTreeId2(p2, q2, n2);
    68             cout << n2;
    69             break;
    70         }
    71         if(i < T)
    72             cout << endl;
    73     }
    74 }
    75 
    76 int main()
    77 {
    78     freopen("B-large-practice.in","r",stdin);
    79     freopen("B-large-practice.out","w",stdout);
    80     RationalNumberTree();
    81     return 0;
    82 }
    View Code

    Problem C. Sorting

    Alex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their father thinks it is good to promote exchanges if they can put their books together. Thus he has bought an one-row bookshelf for them today and put all his sons' books on it in random order. He labeled each position of the bookshelf the owner of the corresponding book ('Alex' or 'Bob').

    Unfortunately, Alex and Bob went outside and didn't know what their father did. When they were back, they came to realize the problem: they usually arranged their books in their own orders, but the books seem to be in a great mess on the bookshelf now. They have to sort them right now!!

    Each book has its own worth, which is represented by an integer. Books with odd values of worth belong to Alex and the books with even values of worth belong to Bob. Alex has a habit of sorting his books from the left to the right in an increasing order of worths, while Bob prefers to sort his books from the left to the right in a decreasing order of worths.

    At the same time, they do not want to change the positions of the labels, so that after they have finished sorting the books according their rules, each book's owner's name should match with the label in its position.

    Here comes the problem. A sequence of N values s0s1, ..., sN-1 is given, which indicates the worths of the books from the left to the right on the bookshelf currently. Please help the brothers to find out the sequence of worths after sorting such that it satisfies the above description.

    Input

    The first line of input contains a single integer T, the number of test cases. Each test case starts with a line containing an integer N, the number of books on the bookshelf. The next line contains N integers separated by spaces, representing s0s1, ..., sN-1, which are the worths of the books.

    Output

    For each test case, output one line containing "Case #X: ", followed by t0t1, ..., tN-1 in order, and separated by spaces. X is the test case number (starting from 1) and t0t1, ...,tN-1 forms the resulting sequence of worths of the books from the left to the right.

    Limits

    1 ≤ T ≤ 30.

    Small dataset

    1 ≤ N ≤ 100
    -100 ≤ si ≤ 100

    Large dataset

    1 ≤ N ≤ 1000
    -1000 ≤ si ≤ 1000

    Sample


    Input 
     

    Output 
     
    2
    5
    5 2 4 3 1
    7
    -5 -12 87 2 88 20 11
    
    Case #1: 1 4 2 3 5
    Case #2: -5 88 11 20 2 -12 87
    
     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <vector>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 bool myfunction (int i,int j)
     9 {
    10     return (i > j);
    11 }
    12 
    13 void Sorting()
    14 {
    15     int T;
    16     cin >> T;
    17     for(int i = 1; i <= T; ++i)
    18     {
    19         cout << "Case #" << i << ": ";
    20         int pos1[1000], pos2[1000];
    21         memset(pos1, -1, 1000);
    22         memset(pos1, -1, 1000);
    23         int ord1[1000], ord2[1000];
    24         int num1 = 0;
    25         int num2 = 0;
    26         int book[1000];
    27         int N;
    28         cin >> N;
    29         for(int j = 0; j < N; ++j)
    30         {
    31             cin >> book[j];
    32             if(book[j] & 1 == 1)
    33             {
    34                 pos1[num1] = j;
    35                 ord1[num1] = book[j];
    36                 num1++;
    37             }
    38             else
    39             {
    40                 pos2[num2] = j;
    41                 ord2[num2] = book[j];
    42                 num2++;
    43             }
    44         }
    45         vector<int> myvector1 (ord1, ord1 + num1);
    46         sort (myvector1.begin(), myvector1.begin() + num1);
    47         vector<int> myvector2 (ord2, ord2 + num2);
    48         sort (myvector2.begin(), myvector2.begin() + num2, myfunction);
    49         int index = 0;
    50         for (vector<int>::iterator it=myvector1.begin(); it!=myvector1.end(); ++it)
    51         {
    52             book[pos1[index++]] = *it;
    53         }
    54         index = 0;
    55         for (vector<int>::iterator it=myvector2.begin(); it!=myvector2.end(); ++it)
    56         {
    57             book[pos2[index++]] = *it;
    58         }
    59         for(int j = 0; j < N-1; ++j)
    60         {
    61             cout << book[j] << ' ';
    62         }
    63         cout << book[N-1];
    64         if(i < T)
    65             cout << endl;
    66     }
    67 }
    68 
    69 int main()
    70 {
    71     freopen("C-large-practice.in", "r", stdin);
    72     freopen("C-large-practice.out", "w", stdout);
    73     Sorting();
    74     return 0;
    75 }
    View Code

    Problem D. Cross the maze

    Edison, a robot, does not have a right hand or eyes. As a brave robot, he always puts his left hand on the wall no matter he walks or turns around. Because he thinks it is too dangerous, Edison does not walk backward.

    Assume that Edison has found himself in a square-shaped maze of NxN square cells which is surrounded by walls from the outside. In the maze, some of the cells are also walls. Edison can only move between two empty cells in four directions, north, south, west and east. In order to get out of the maze, he drafts a plan. He uses his left hand to lean on the wall and goes by following the wall.

    Here is the question, is Edison able to get out of the maze in at most 10,000 steps? If he can make it, output the path. By getting out of the maze, he only needs to be in the exit cell. If the starting cell is the same as the exit, Edison won't need to move and can directly get out of the maze.

    Input

    The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with an integer NN is the size of the maze. The following N lines, each line contains N characters which may be '.' or '#'. '.' is an empty cell, '#' is a wall. Followed by a line which contains four integers: sxsyexey. (sxsy) means that Edison is standing on row sx and column sy as his starting cell, (exey) is the exit of the maze. (sxsy) is guaranteed to be at one of the 4 corners of the maze, and Edison can only touch the wall on 4 adjacent cells(not 8) initially. (exey) can be anywhere in the maze. Note that the top-left corner is at position (1,1).

    Output

    For each test case, output a line containing "Case #x: y", where x is the case number (starting from 1) and y is "Edison ran out of energy." (without the quotes) if Edison can't reach the exit of the maze in at most 10,000 steps, otherwise y should be the number of steps followed by another line which contains y characters to describe the path (each character should be E for east, S for south, W for west or N for north). There is no character to represent the turning around. We don't care about the turning around steps, please only output the path of how Edison will cross the maze.

    Limits

    1 ≤ T ≤ 30.
    1 ≤ sxsyexey ≤ N.
    The starting cell and the exit of the maze will always be an empty cell. And the starting cell and the exit of the maze won't be the same.

    Small dataset

    2 ≤ N ≤ 10.

    Large dataset

    2 ≤ N ≤ 100.

    Sample


    Input 
     

    Output 
     
    3
    2
    .#
    #.
    1 1 2 2
    5
    .##.#
    .....
    ...#.
    .###.
    ...#.
    1 1 5 3
    3
    ...
    .#.
    ...
    1 1 3 3
    
    Case #1: Edison ran out of energy.
    Case #2: 22
    SEEENSESSSNNNWWSWWSSEE
    Case #3: 4
    EESS
    
    Note: 
    In the 2nd test case after moving 1 cell down from his starting cell, Edison will still be able to lean on the wall at the cell (1,2) by his left hand. 
    In the third test case, due to Edison can't touch the wall at cell (2,2) initially, so he has to go east in his first step.
      1 #include <iostream>
      2 #include <fstream>
      3 #include <string>
      4 #include <vector>
      5 using namespace std;
      6 
      7 bool Adjacent(int x1, int y1, int x2, int y2)
      8 {
      9     if(x1 - x2 > 1 || x2 - x1 > 1 || y1 - y2 >1 || y2 - y1 > 1)
     10         return false;
     11     return true;
     12 }
     13 
     14 void CrossTheMaze()
     15 {
     16     int T;
     17     cin >> T;
     18     for(int i = 1; i <= T; ++i)
     19     {
     20         cout << "Case #" << i << ": ";
     21         int N;
     22         cin >> N;
     23         char **maze;
     24         maze = new char*[N];
     25         for(int j = 0; j < N; ++j)
     26             maze[j] = new char[N];
     27         for(int j = 0; j < N; ++j)
     28         {
     29             for(int k = 0; k < N; ++k)
     30                 cin >> maze[j][k];
     31         }
     32         int sx, sy, ex, ey;
     33         cin >> sx >> sy >> ex >> ey;
     34         sx -= 1;
     35         sy -= 1;
     36         ex -= 1;
     37         ey -= 1;
     38         int cx = sx, cy = sy;
     39         vector<char> path;
     40         char flag;
     41         int wx, wy;
     42         if(sx == 0)
     43         {
     44             if(sy == 0)
     45             {
     46                 wx = -1;
     47                 wy = 0;
     48                 flag = 'e';
     49             }
     50             else
     51             {
     52                 wx = 0;
     53                 wy = N;
     54                 flag = 's';
     55             }
     56         }
     57         else
     58         {
     59             if(sy == 0)
     60             {
     61                 wx = N-1;
     62                 wy = -1;
     63                 flag = 'n';
     64             }
     65             else
     66             {
     67                 wx = N;
     68                 wy = N-1;
     69                 flag = 'w';
     70             }
     71         }
     72         int turn = 0;
     73         for(int j = 1; j <= 10000 && (cx != ex || cy != ey);)
     74         {
     75             bool n = (cx > 0 && maze[cx-1][cy] == '.') && (((cy == 0 || maze[cx][cy-1] == '#') && Adjacent(cx,cy-1,wx,wy)) || (cy > 0 && maze[cx-1][cy-1] == '#' && flag == 'e'));
     76             bool s = (cx < N-1 && maze[cx+1][cy] == '.') && (((cy == N-1 || maze[cx][cy+1] == '#') && Adjacent(cx,cy+1,wx,wy)) || (cy < N-1 && maze[cx+1][cy+1] == '#' && flag == 'w'));
     77             bool w = (cy > 0 && maze[cx][cy-1] == '.') && (((cx == N-1 || maze[cx+1][cy] == '#') && Adjacent(cx+1,cy,wx,wy)) || (cx < N-1 && maze[cx+1][cy-1] == '#' && flag == 'n'));
     78             bool e = (cy < N-1 && maze[cx][cy+1] == '.') && (((cx == 0 || maze[cx-1][cy] == '#') && Adjacent(cx-1,cy,wx,wy)) || (cx > 0 && maze[cx-1][cy+1] == '#' && flag == 's'));
     79             if(n||s||w||e)
     80             {
     81                 if(n && flag != 's')
     82                 {
     83                     turn = 0;
     84                     ++j;
     85                     cx -= 1;
     86                     path.push_back('N');
     87                     flag = 'n';
     88                     if(cy == 0 || maze[cx][cy-1] == '#')
     89                     {
     90                         wx = cx;
     91                         wy = cy-1;
     92                     }
     93                     else
     94                     {
     95                         wx = cx+1;
     96                         wy = cy-1;
     97                     }
     98                 }
     99                 else if(s && flag != 'n')
    100                 {
    101                     turn = 0;
    102                     ++j;
    103                     cx += 1;
    104                     path.push_back('S');
    105                     flag = 's';
    106                     if(cy == N-1 || maze[cx][cy+1] == '#')
    107                     {
    108                         wx = cx;
    109                         wy = cy+1;
    110                     }
    111                     else
    112                     {
    113                         wx = cx-1;
    114                         wy = cy+1;
    115                     }
    116                 }
    117                 else if(w && flag != 'e')
    118                 {
    119                     turn = 0;
    120                     ++j;
    121                     cy -= 1;
    122                     path.push_back('W');
    123                     flag = 'w';
    124                     if(cx == N-1 || maze[cx+1][cy] == '#')
    125                     {
    126                         wx = cx+1;
    127                         wy = cy;
    128                     }
    129                     else
    130                     {
    131                         wx = cx+1;
    132                         wy = cy+1;
    133                     }
    134                 }
    135                 else if(e && flag != 'w')
    136                 {
    137                     turn = 0;
    138                     ++j;
    139                     cy += 1;
    140                     path.push_back('E');
    141                     flag = 'e';
    142                     if(cx == 0 || maze[cx-1][cy] == '#')
    143                     {
    144                         wx = cx-1;
    145                         wy = cy;
    146                     }
    147                     else
    148                     {
    149                         wx = cx-1;
    150                         wy = cy-1;
    151                     }
    152                 }
    153             }
    154             else
    155             {
    156                 if(flag == 'n' && Adjacent(cx-1,cy,wx,wy))
    157                 {
    158                     flag = 'e';
    159                     wx = cx-1;
    160                     wy = cy;
    161                     ++turn;
    162                 }
    163                 else if(flag == 's' && Adjacent(cx+1,cy,wx,wy))
    164                 {
    165                     flag = 'w';
    166                     wx = cx+1;
    167                     wy = cy;
    168                     ++turn;
    169                 }
    170                 else if(flag == 'w' && Adjacent(cx,cy-1,wx,wy))
    171                 {
    172                     flag = 'n';
    173                     wx = cx;
    174                     wy = cy-1;
    175                     ++turn;
    176                 }
    177                 else if(flag == 'e' && Adjacent(cx,cy+1,wx,wy))
    178                 {
    179                     flag = 's';
    180                     wx = cx;
    181                     wy = cy+1;
    182                     ++turn;
    183                 }
    184                 else
    185                     break;
    186                 if(turn == 4)
    187                     break;
    188             }
    189         }
    190         if(cx == ex && cy == ey)
    191         {
    192             cout << path.size() << endl;
    193             for(vector<char>::iterator it = path.begin(); it != path.end(); ++it)
    194                 cout << *it;
    195         }
    196         else
    197             cout << "Edison ran out of energy.";
    198         if(i < T)
    199             cout << endl;
    200         for(int j = 0; j < N; ++j)
    201             delete [] maze[j];
    202         delete [] maze;
    203     }
    204 }
    205 
    206 int main()
    207 {
    208     freopen("D-large-practice.in","r",stdin);
    209     freopen("D-large-practice.out","w",stdout);
    210     CrossTheMaze();
    211     return 0;
    212 }
    View Code

    Problem E. Spaceship Defence

    The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: teleporters andturbolifts.

    Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.

    Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:

    • Turbolifts are not two-way: if a turbolift moves soldiers from room a to room b, the same turbolift cannot move soldiers from room b to room a, although there might be another turbolift that does that.
    • More than one soldier can use the same turbolift, and they do not interfere with each other in any way.

    You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.

    Input

    The first line of the input gives the number of test cases, TT test cases follow.

    For every test case:

    The first line of every test case contains an integer N, which is the number of rooms in your spaceship. The rooms are numbered from 1 to N. The following N lines each contain a string telling the color of the rooms, from room 1 to room N. The strings only contain characters a-z (the lower-case English letters) and 0-9 (the number 0 to 9), and the length of each string will be less than or equal to 2.

    The next line in the test case is an integer M, which indicates the number of turbolifts in your spaceship. The following M lines each contain 3 space-separated integers aibiti, telling us that there is a turbolift that can transport soldiers from room ai to room bi in tiseconds.

    The next line in the test case contains an integer S, which is the number of soldiers at your command. The following S lines each contain two integers: the location and destination of one soldier, pj and qj.

    Output

    For each test case, output one line containing only the string "Case #x:", where x is the number of the test case (starting from 1). On the next S lines, output a single integer: on line j, the smallest number of seconds it could take for a soldier to travel from pj to qj. If there is no path from pj to qj, the integer you output should be -1.

    Limits

    1 ≤ S ≤ 100.
    1 ≤ ai, bi ≤ N.
    0 ≤ ti ≤ 1000.
    1 ≤ pj, qj ≤ N.

    Small dataset

    1 ≤ T ≤ 10.
    1 ≤ N ≤ 1000.
    0 ≤ M ≤ 3000.

    Large dataset

    T = 1.
    1 ≤ N ≤ 80000.
    0 ≤ M ≤ 3000.

    Sample


    Input 
     

    Output 
     
    3
    3
    gl
    t3
    t3
    3
    1 2 217
    3 2 567
    1 1 21
    2
    2 1
    2 3
    4
    ca
    bl
    bl
    8z
    0
    3
    1 2
    2 3
    1 1
    8
    re
    b7
    ye
    gr
    0l
    0l
    ye
    b7
    7
    4 1 19
    2 4 21
    2 5 317
    4 5 34
    4 7 3
    4 8 265
    8 6 71
    3
    4 3
    2 6
    1 4
    
    Case #1:
    -1
    0
    Case #2:
    -1
    0
    0
    Case #3:
    3
    55
    -1
    
     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <vector>
     5 #include <map>
     6 using namespace std;
     7 
     8 int shortest[1300][1300];
     9 
    10 void SpaceshipDefence()
    11 {
    12     int T;
    13     cin >> T;
    14     for(int i = 1; i <= T; ++i)
    15     {
    16         cout << "Case #" << i << ":" << endl;
    17         long N;
    18         cin >> N;
    19         int *roomColor;
    20         roomColor = new int[N];
    21         for(int j = 0; j < 1300; ++j)
    22         {
    23             for(int k = 0; k < 1300; ++k)
    24                 shortest[j][k] = 0x7FFF;
    25             shortest[j][j] = 0;
    26         }
    27         string color;
    28         map<string, int> colorMap;
    29         for(long j = 0; j < N; ++j)
    30         {
    31             cin >> color;
    32             if(!colorMap.count(color))
    33                 colorMap.insert(pair<string,int>(color, colorMap.size()));
    34             roomColor[j] = colorMap[color];
    35         }
    36         int M;
    37         cin >> M;
    38         unsigned long a, b, t;
    39         for(int j = 1; j <= M; ++j)
    40         {
    41             cin >> a >> b >> t;
    42             a -= 1;
    43             b -= 1;
    44             if(shortest[roomColor[a]][roomColor[b]] > t)
    45                 shortest[roomColor[a]][roomColor[b]] = t;
    46         }
    47         for(long u = 0; u < colorMap.size(); ++u)
    48         {
    49             for(long v = 0; v < colorMap.size(); ++v)
    50             {
    51                 for(long w = 0; w < colorMap.size(); ++w)
    52                 {
    53                     if(shortest[v][u] + shortest[u][w] < shortest[v][w])
    54                         shortest[v][w] = shortest[v][u] + shortest[u][w];
    55                 }
    56             }
    57         }
    58         int S;
    59         cin >> S;
    60         long p, q;
    61         for(int j = 1; j <= S; ++j)
    62         {
    63             cin >> p >> q;
    64             p -= 1;
    65             q -= 1;
    66             if(shortest[roomColor[p]][roomColor[q]] == 0x7FFF)
    67                 cout << -1 << endl;
    68             else
    69                 cout << shortest[roomColor[p]][roomColor[q]] << endl;
    70         }
    71         delete [] roomColor;
    72     }
    73 }
    74 
    75 int main()
    76 {
    77     freopen("E-large-practice.in","r",stdin);
    78     freopen("E-large-practice.out","w",stdout);
    79     SpaceshipDefence();
    80     return 0;
    81 }
    View Code

    这个里面一个刚开始没想到的问题是,既然同color间传送耗时为0,那么就不用计算所有room之间的最短距离,只需计算所有color间的最短距离即可,这样求最短路的矩阵大小就不用N×N了(不然large dataset,N=80000就hold不住了),而color最多是36*36种,也就一千多种。另外,这么大的矩阵貌似就不能在函数间传递或者在调用的函数中申请了,用全局变量就可以了。

  • 相关阅读:
    <!--[if IE 9]>....<!end if-->
    背景颜色设置
    UIImageView 响应UIButton的点击事件
    响应键盘return事件
    iOS 封装数据请求,解析数据(异步)
    UIImageView 圆角
    JS 控制文本框必须输入值才能查询
    MVC4 @Html.Raw()
    JS加成显示
    随机生成人物名称
  • 原文地址:https://www.cnblogs.com/shirley130912/p/3359543.html
Copyright © 2011-2022 走看看