Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************
- Problem: UVA--10249 The Grand Dinner
- Author: Bruce Chen
- Language: C++
- Time: over 5 sec (TLE)
- *This code got TLE in UVA, I post it here
- just to record that this problem can be solved
- by maximum flow(although it got TLE).
- It's just another thought.
- (p.s this problem can be solved by greedy method,
- which is faster & easier for us to implement)
- ***************************************************/
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- #include <cstring>
- using namespace std;
- int team, table, N;
- int cap[200][200];
- int flow[200][200];
- int prev[200];
- bool visited[200];
- bool seat[200][200];
- void init()
- {
- memset(cap, 0, sizeof(cap));
- memset(flow, 0, sizeof(flow));
- memset(visited, false, sizeof(visited));
- memset(seat, false, sizeof(seat));
- memset(prev, -1, sizeof(prev));
- }
- bool BFS (int start, int goal)
- {
- memset(visited, false, sizeof(visited));
- queue<int> Q;
- Q.push(start);
- while(!Q.empty())
- {
- int now = Q.front();
- Q.pop();
- for(int i = 1 ; i <= N+1 ; i++)
- {
- if(visited[i]) continue;
- if(cap[now][i] - flow[now][i] > 0 || flow[i][now] > 0)
- {
- prev[i] = now;
- visited[i] = true;
- Q.push(i);
- }
- }
- }
- if(visited[goal]) return true;
- else return false;
- }
- int FindFlow(int start, int goal)
- {
- int f = 1000000000;
- int now = goal;
- while(now != start)
- {
- int pre = prev[now];
- if(pre <= team) seat[pre][now] = true;
- if(now <= team) seat[now][pre] = false;
- if(cap[pre][now] - flow[pre][now] > 0) f = min(f, cap[pre][now] - flow[pre][now]);
- else f = min(f, flow[now][pre]);
- now = pre;
- }
- now = goal;
- while(now != start)
- {
- int pre = prev[now];
- if(cap[pre][now] - flow[pre][now] > 0) flow[pre][now] += f;
- else flow[now][pre] -= f;
- now = pre;
- }
- return f;
- }
- int MaxFlow(int start, int goal)
- {
- int ret = 0;
- while(BFS(start, goal))
- {
- ret += FindFlow(start, goal);
- }
- return ret;
- }
- int main()
- {
- int count = 1;
- while(~scanf("%d%d", &team, &table))
- {
- if(!team && !table) return 0;
- N = team + table;
- int G = N + 1;
- int S = 0;
- init();
- int people = 0;
- for(int i = 1 ; i <= team ; i++)
- {
- int temp;
- scanf("%d", &temp);
- cap[S][i] = temp;
- people += temp;
- for(int j = team+1 ; j <= N ; j++)
- {
- cap[i][j] = 1;
- }
- }
- for(int i = team+1 ; i <= N ; i++)
- {
- int temp;
- scanf("%d", &temp);
- cap[i][G] = temp;
- }
- int ans = MaxFlow(S, G);
- if(ans == people)
- {
- puts("1");
- for(int i = 1 ; i <= team ; i++)
- {
- bool blank = false;
- for(int j = team+1 ; j <= N ; j++)
- {
- if(seat[i][j])
- {
- if(blank) printf(" %d", j-team);
- else
- {
- printf("%d", j-team);
- blank = true;
- }
- }
- }
- puts("");
- }
- }
- else puts("0");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment