UVa Problem Solution: 10037 – Bridge

來源:互聯網
上載者:User

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. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.