比赛 20120709 评测结果 TWAWATTTTTWW
题目名称 聪明的推销员 最终得分 16
用户昵称 CC 运行时间 6.004 s
代码语言 C++ 内存使用 2.67 MiB
提交时间 2012-07-09 11:46:46
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
const int INF = 1000000000,end = 3049;
struct edge {
	int y;
	edge *next,*opt;
	edge (int y,edge *next): y(y),next(next) {};
}*d[3050];
int n,m,R,ans = INF;
int t[3050],g[3050][200];
bool ok[3050],w[3050];
void bfs1(int u) {
	int q[10000] = {0},head = 0,tail = 0;
	bool done[3050] = {0};
	q[0] = u;
	done[u] = 1;
	g[u][0] = 1;
	g[u][1] = u;
	while (head <= tail) {
		int now = q[head++];
		for (edge *p = d[now];p;p = p->next) 
			if (!done[p->y]) {
				done[q[++tail] = p->y] = 1;
				g[u][++g[u][0]] = p->y;
			}
	}
}
int pan() {
	int tmp = 0,tot = 0;
	memset(ok,0,sizeof(ok));
	for (int i = 1;i <= n;i++) 
		if (t[i] && w[i]) {
			tmp += t[i];
			for (int j = 1;j <= g[i][0];j++) 
				if (!ok[g[i][j]]) {
					tot++;
					ok[g[i][j]] = 1;
				}
		}
	if (tot == n) return tmp;
	return 0;
}

void dfs(int u) {
	if (u > m) {
		int tmp = pan();
		if (tmp && tmp < ans) ans = tmp;
		return;
	}
	dfs(u + 1);
	w[u] = 1;
	dfs(u + 1);
	w[u] = 0;
}


int main() {
	freopen("salenet.in","r",stdin);
	freopen("salenet.out","w",stdout);
	scanf("%d%d", &n, &m);
	int p,q;
	for (int i = 1;i <= m;i++) {
		scanf("%d%d", &p, &q);
		t[p] = q;
	}
	scanf("%d", &R);
	for (int i = 1;i <= R;i++) {
		scanf("%d%d", &p, &q);
		d[p] = new edge(q,d[p]);
	}
	for (int i = 1;i <= n;i++) 
		if (t[i]) {
			bfs1(i);
			for (int j = 1;j <= g[i][0];j++) 
				ok[g[i][j]] = 1;
		}
	for (int i = 1;i <= n;i++) if (!ok[i]) {
		printf("NO\n%d",i);
		return 0;
	}
	dfs(1);
	printf("YES\n%d", ans);
	return 0;
}