Tube

UVA--10249 The Grand Dinner

May 20th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. /****************************************************
  2. Problem: UVA--10249 The Grand Dinner
  3. Author: Bruce Chen
  4. Language: C++
  5. Time: over 5 sec (TLE)
  6.  
  7. *This code got TLE in UVA, I post it here
  8. just to record that this problem can be solved
  9. by maximum flow(although it got TLE).
  10. It's just another thought.
  11. (p.s this problem can be solved by greedy method,
  12. which is faster & easier for us to implement)
  13. ***************************************************/
  14.  
  15. #include <iostream>
  16. #include <cstdio>
  17. #include <algorithm>
  18. #include <queue>
  19. #include <cstring>
  20.  
  21. using namespace std;
  22.  
  23. int team, table, N;
  24. int cap[200][200];
  25. int flow[200][200];
  26. int prev[200];
  27. bool visited[200];
  28. bool seat[200][200];
  29.  
  30. void init()
  31. {
  32.     memset(cap, 0, sizeof(cap));
  33.     memset(flow, 0, sizeof(flow));
  34.     memset(visited, false, sizeof(visited));
  35.     memset(seat, false, sizeof(seat));
  36.     memset(prev, -1, sizeof(prev));
  37. }
  38.  
  39. bool BFS (int start, int goal)
  40. {
  41.     memset(visited, false, sizeof(visited));
  42.     queue<int> Q;
  43.     Q.push(start);
  44.  
  45.     while(!Q.empty())
  46.     {
  47.         int now = Q.front();
  48.         Q.pop();
  49.  
  50.         for(int i = 1 ; i <= N+1 ; i++)
  51.         {
  52.             if(visited[i]) continue;
  53.             if(cap[now][i] - flow[now][i] > 0 || flow[i][now] > 0)
  54.             {
  55.                 prev[i] = now;
  56.                 visited[i] = true;
  57.                 Q.push(i);
  58.             }
  59.         }
  60.     }
  61.     if(visited[goal]) return true;
  62.     else return false;
  63. }
  64.  
  65. int FindFlow(int start, int goal)
  66. {
  67.     int f = 1000000000;
  68.     int now = goal;
  69.  
  70.     while(now != start)
  71.     {
  72.         int pre = prev[now];
  73.  
  74.         if(pre <= team) seat[pre][now] = true;
  75.         if(now <= team) seat[now][pre] = false;
  76.  
  77.         if(cap[pre][now] - flow[pre][now] > 0) f = min(f, cap[pre][now] - flow[pre][now]);
  78.         else f = min(f, flow[now][pre]);
  79.  
  80.         now = pre;
  81.     }
  82.  
  83.     now = goal;
  84.  
  85.     while(now != start)
  86.     {
  87.         int pre = prev[now];
  88.  
  89.         if(cap[pre][now] - flow[pre][now] > 0) flow[pre][now] += f;
  90.         else flow[now][pre] -= f;
  91.  
  92.         now = pre;
  93.     }
  94.     return f;
  95. }
  96.  
  97. int MaxFlow(int start, int goal)
  98. {
  99.     int ret = 0;
  100.     while(BFS(start, goal))
  101.     {
  102.         ret += FindFlow(start, goal);
  103.     }
  104.     return ret;
  105. }
  106.  
  107. int main()
  108. {
  109.     int count = 1;
  110.     while(~scanf("%d%d", &team, &table))
  111.     {
  112.         if(!team && !table) return 0;
  113.  
  114.         N = team + table;
  115.         int G = N + 1;
  116.         int S = 0;
  117.         init();
  118.  
  119.         int people = 0;
  120.         for(int i = 1 ; i <= team ; i++)
  121.         {
  122.             int temp;
  123.             scanf("%d", &temp);
  124.             cap[S][i] = temp;
  125.             people += temp;
  126.  
  127.             for(int j = team+1 ; j <= N ; j++)
  128.             {
  129.                 cap[i][j] = 1;
  130.             }
  131.         }
  132.        
  133.         for(int i = team+1 ; i <= N ; i++)
  134.         {
  135.             int temp;
  136.             scanf("%d", &temp);
  137.             cap[i][G] = temp;
  138.         }
  139.  
  140.         int ans = MaxFlow(S, G);
  141.         if(ans == people)
  142.         {
  143.             puts("1");
  144.             for(int i = 1 ; i <= team ; i++)
  145.             {
  146.                 bool blank = false;
  147.                 for(int j = team+1 ; j <= N ; j++)
  148.                 {
  149.                     if(seat[i][j])
  150.                     {
  151.                         if(blank) printf(" %d", j-team);
  152.                         else
  153.                         {
  154.                             printf("%d", j-team);
  155.                             blank = true;
  156.                         }
  157.                     }
  158.                 }
  159.                 puts("");
  160.             }
  161.         }
  162.         else puts("0");
  163.     }
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment