比赛 20161215 评测结果 AAAATTTEEEEWWWA
题目名称 相遇时间 最终得分 33
用户昵称 confoo 运行时间 6.606 s
代码语言 C++ 内存使用 3.09 MiB
提交时间 2016-12-16 21:13:58
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cctype>
#define file(x) "meeting." #x
namespace I {
	const int L = 1 << 15;
	char buf[L], *s, *t;
	inline char gc() {
		if (s == t) t = (s = buf) + fread(buf, 1, L, stdin);
		if (s == t) return EOF;
		return *s++;
	}
	inline int gi() {
		int r = 0, ch = gc();
		while (!isdigit(ch)) ch = gc();
		while (isdigit(ch)) r = r*10 + ch - '0', ch = gc();
		return r;
	}
}using I::gi;
const int V = 110, E = V*V;
struct EDGE{int u, v, w[2];}st[E];
int n, m, hed[V], nxt[E], sz, upb;
void add(int u, int v, int w1, int w2) {
	st[++sz].u = u, st[sz].v = v, st[sz].w[0] = w1, st[sz].w[1] = w2;
	nxt[sz] = hed[u], hed[u] = sz;
}
bool f[2][V][V*V], dd[V];
int d;
void dfs(int u) {
	if (dd[u]) return;
	for (int e = hed[u]; e; e = nxt[e]) dfs(st[e].v);
	for (int t = 0; t <= upb; t++) {
		for (int e = hed[u]; e; e = nxt[e]) {
			if (t >= st[e].w[d] && f[d][st[e].v][t - st[e].w[d]]) {
				f[d][u][t] = 1;
				break;
			}
		}
	}
	dd[u] = 1;
}
int main() {
	freopen(file(in),  "r", stdin);
	freopen(file(out), "w", stdout);
	n = gi(), m = gi();
	for (int i = 1; i <= m; i++) {
		int u = gi(), v = gi(), w1 = gi(), w2 = gi();
		upb += w1 > w2 ? w1 : w2;
		add(v, u, w1, w2);
	}
	f[0][dd[1] = 1][0] = 1;
	dfs(n);
	memset(dd, 0, sizeof(dd));
	f[d = 1][dd[1] = 1][0] = 1;
	dfs(n);
	for (int t = 0; t <= upb; t++) if (f[0][n][t] && f[1][n][t]) {
		printf("%d", t);
		return 0;
	}
	puts("IMPOSSIBLE");
}