记录编号 394778 评测结果 AAAAAAAAAA
题目名称 [网络流24题] 圆桌聚餐 最终得分 100
用户昵称 GravatarHeHe 是否通过 通过
代码语言 C++ 运行时间 0.248 s
提交时间 2017-04-14 14:59:39 内存使用 0.32 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 600;
const int INF = 0x7fffffff;

inline int in(void){
	char tmp = getchar();
	int res = 0;
	while(!isdigit(tmp))tmp = getchar();
	while(isdigit(tmp))
		res = (res + (res << 2) << 1) + (tmp ^ 48),
		tmp = getchar();
	return res;
}

struct EDGE{
	int fr, to, ne;
	int cap; 
	
	EDGE(){;}
	EDGE(int a, int b, int c, int d){
		fr = a, to = b, ne = c;
		cap = d;
	}
};

inline void add_edge(int fr, int to, int cap);
inline bool BFS(void);
inline int get_flow(void);
inline void print(void);

vector<EDGE> edge;
int head[MAXN], pre[MAXN];
bool vis[MAXN];
int S, T, max_flow;
int M, N, tmp, tot;

int main(){ 
#ifndef LOCAL
	freopen("roundtable.in", "r", stdin);
	freopen("roundtable.out", "w", stdout);
#else
	freopen("test.in", "r", stdin);
#endif
	memset(head, 0xff, sizeof(head));

	M = in(), N = in();
	S = 0, T = M + N + 1;
	for(int i = 1; i <= M; ++i){
		add_edge(S, i, tmp = in());
		tot += tmp;
	}

	for(int i = M + 1; i < T; ++i){
		add_edge(i, T, in());
	}

	for(int i = 1; i <= M; ++i){
		for(int j = M + 1; j < T; ++j){
			add_edge(i, j, 1);
		}
	}

	while(BFS()){
		max_flow += get_flow();
	}

	if(max_flow != tot){
		printf("0\n");
		return 0;
	}
	printf("1\n");
	print();

	return 0;
}

inline void add_edge(int fr, int to, int cap){
	edge.push_back(EDGE(fr, to, head[fr], cap)), head[fr] = edge.size() - 1;
	edge.push_back(EDGE(to, fr, head[to], 0)), head[to] = edge.size() - 1;
	return ;
}

inline bool BFS(void){
	memset(pre, 0xff, sizeof(pre));
	memset(vis, 0x00, sizeof(vis));
	int now, to;
	queue<int> que;

	que.push(S);
	vis[S] = true;
	
	while(!que.empty()){
		now = que.front();
		if(now == T)return true;
		que.pop();

		for(int i = head[now]; i != -1; i = edge[i].ne){
			to = edge[i].to;
			if(vis[to] || edge[i].cap <= 0)continue;
			vis[to] = true;
			pre[to] = i;
			que.push(to);
		}
	}

	return vis[T];
}

inline int get_flow(void){
	int now = pre[T];
	int min_flow = INF;
	while(now != -1){
		min_flow = min(min_flow, edge[now].cap);
		now = pre[edge[now].fr];
	}

	now = pre[T];
	while(now != -1){
		edge[now].cap -= min_flow;
		edge[now ^ 1].cap += min_flow;
		now = pre[edge[now].fr];
	}

	return min_flow;
}

inline void print(void){
	for(int i = 1; i <= M; ++i){
		for(int j = head[i]; j != -1; j = edge[j].ne){
			if(edge[j].cap)continue;
			printf("%d ", edge[j].to - M);
		}
		putchar('\n');
	}
	return ;
}