UVa Problem Solution: 10037 - Bridge


Given the two fastest people as f1 and f2, f1 < f2, and the two slowest people as s1 and s2, s1 < s2. We want to use the two fastest people to bring the two slowest people to cross the bridge. There are two efficient ways of doing this:
  1) f1, f2 go --> f1 back --> s1, s2 go --> f2 back: time1 = f2 + f1 + s2 + f2
  2) f1, s1 go --> f1 back --> f1, s2 go --> f1 back: time2 = s1 + f1 + s2 + f1
Then, we can compare time1 with time2 to choose the better way.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10037 C++ "Bridge" */
  6. #include <algorithm>
  7. #include <deque>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <list>
  11. #include <map>
  12. #include <queue>
  13. #include <set>
  14. #include <stack>
  15. #include <string>
  16. #include <vector>
  17. using namespace std;
  18.      
  19. int const timecount = 1000;
  20.      
  21. void cross_bridge(int *times, int ntimes, int strategy[][2])
  22. {
  23.   sort(times, times + ntimes);
  24.   int i = 0;
  25.   for (; i < ntimes / 2 - 1; ++i) {
  26.     int time1 = times[1] + times[0] + times[ntimes-2*i-1] + times[1];
  27.     int time2 = times[ntimes-2*i-1] + times[0] + times[ntimes-2*i-2] + times[0];
  28.     if (time1 < time2) {
  29.       strategy[4*i+1][0] = times[0];
  30.       strategy[4*i+1][1] = times[1];
  31.       strategy[4*i+2][0] = times[0];
  32.       strategy[4*i+3][0] = times[ntimes-2*i-2];
  33.       strategy[4*i+3][1] = times[ntimes-2*i-1];
  34.       strategy[4*i+4][0] = times[1];
  35.       strategy[0][0] += time1;
  36.     } else {
  37.       strategy[4*i+1][0] = times[0];
  38.       strategy[4*i+1][1] = times[ntimes-2*i-1];
  39.       strategy[4*i+2][0] = times[0];
  40.       strategy[4*i+3][0] = times[0];
  41.       strategy[4*i+3][1] = times[ntimes-2*i-2];
  42.       strategy[4*i+4][0] = times[0];
  43.       strategy[0][0] += time2;
  44.     }
  45.   }
  46.   strategy[4*i+1][0] = times[0];
  47.   strategy[4*i+1][1] = times[1];
  48.   strategy[0][0] += times[1];
  49.   if (ntimes % 2 == 1) {
  50.     strategy[4*i+2][0] = times[0];
  51.     strategy[4*i+3][0] = times[0];
  52.     strategy[4*i+3][1] = times[2];
  53.     strategy[0][0] += times[0] + times[2];
  54.   }
  55. }
  56.      
  57. int main(int argc, char *argv[])
  58. {
  59. #ifndef ONLINE_JUDGE
  60.   filebuf in, out;
  61.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  62.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  63. #endif
  64.   int ncases;
  65.   cin >> ncases;
  66.   while (ncases-- > 0) {
  67.     int ntimes;
  68.     cin >> ntimes;
  69.     int times[timecount];
  70.     for (int i = 0; i < ntimes; ++i) cin >> times[i];
  71.     if (ntimes < 2) {
  72.       cout << times[0] << '/n' << times[0] << '/n';
  73.     } else {
  74.       int strategy[(timecount-1)*2][2] = {{0}};
  75.       cross_bridge(times, ntimes, strategy);
  76.       for (int i = 0; i < (ntimes - 1) * 2; ++i) {
  77.         cout << strategy[i][0];
  78.         if (i % 2 == 1) cout << ' ' << strategy[i][1];
  79.         cout << '/n';
  80.       }
  81.     }
  82.     if (ncases > 0) cout << '/n';
  83.   }
  84.   return 0;
  85. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章