Beispiel #1
0
int main()
{
	int Test;
	scanf("%d", &Test);
	while (Test--) {
		cin >> N >> M;
		int source = 0, sink = N + M + 1;
		sap.init(N + M + 2);
		for (int i = 1; i <= N; i++)
		{
			int t;
			cin >> t;
			sap.addCap(M + i, sink, t);
		}
		int Sum = 0;
		for (int i = 1; i <= M; i++)
		{
			int a, b, c;
			cin >> a >> b >> c;
			sap.addCap(i, M + a, inf);
			sap.addCap(i, M + b, inf);
			sap.addCap(source, i, c);
			Sum += c;
		}
		int tmp = sap.solve(source, sink);
		cout << Sum - tmp << endl;
	}
	return 0;
}
Beispiel #2
0
int main()
{
	freopen("a", "r", stdin);
	int t, n, m;
	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		sap.init(2 * n + 2);
		int source = 2 * n, sink = 2 * n + 1;
		int tmp;
		for (int i = 0; i < n; i++)
		{
			cin >> tmp;
			sap.addCap(n + i, sink, tmp);
		}
		for (int i = 0; i < n; i++)
		{
			cin >> tmp;
			sap.addCap(source, i, tmp);
		}
		int a, b;
		for (int i = 0; i < m; i++)
		{
			cin >> a >> b;
			a--; b--;
			sap.cap[a][n + b] = inf;
		}
		cout << sap.solve(source, sink) << endl;
		sap.output(source, sink);
		if (t > 0) cout << endl;
	}
	return 0;
}
Beispiel #3
0
int main()
{
	int x, y;
	while (scanf("%d%d", &x, &y) == 2)
	{
		memset(first, -1, sizeof(first));
		SAP sap;
		for (int i = 1; i <= x; i++)
			scanf("%d", &pigs[i]);
		for (int i = 1; i <= y; i++)
		{
			int a;
			scanf("%d", &a);
			int flow = 0;
			for (int j = 0; j < a; j++)
			{
				int key;
				scanf("%d", &key);
				if (first[key] == -1)
				{
					flow += pigs[key];
					first[key] = i;
				}
				else sap.insert(first[key], i, inf);
			}
			if (flow) sap.insert(0, i, flow);
			int b;
			scanf("%d", &b);
			sap.insert(i, y+1, b);
		}
		n = y+2;
		printf("%d\n", sap.max_flow(0, n-1));
	}
	return 0;
}
Beispiel #4
0
int main() {
    int n, m;
    while(~scanf("%d%d", &m, &n)) {
        s.init(1, n, n + 10);
        for(int i = 0; i < m; i++) {
            int x, y, c; scanf("%d%d%d", &x, &y, &c);
            s.add(x, y, c);
        }
        printf("%d\n", s.sap());
    }
    return 0;
}
Beispiel #5
0
bool Check(long long lim)
{
	sap.init(2 * N + 2);
	int source = 0, sink = 2 * N + 1;
	for (int i = 1; i <= N; i++)
	{
		sap.addCap(source, i, a[i]);
		sap.addCap(N + i, sink, b[i]);
	}
	for (int i = 1; i <= N; i++)
	for (int j = 1; j <= N; j++)
	if (dis[i][j] <= lim)
	{
		sap.addCap(i, N + j, inf);
		sap.addCap(j, N + i, inf);
	}
	int maxflow = sap.solve(source, sink);
	return maxflow == tot;
}
Beispiel #6
0
	int solve(int source, int sink)
	{
		sap.init(n + 2);
		int tot = 0;
		
		upper[sink][source] = inf;
		lower[sink][source] = 0;

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (upper[i][j] < lower[i][j]) return -1;
				sap.addCap(i, j, upper[i][j] - lower[i][j]);
				sap.addCap(n, j, lower[i][j]);
				sap.addCap(i, n + 1, lower[i][j]);
				tot += lower[i][j];
			}
		}
		if (sap.solve(n, n + 1) != tot) return -1;
		sap.solve(source, sink);
		for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			flow[i][j] = upper[i][j] - sap.cap[i][j];
		tot = 0;
		for (int i = 0; i < n; i++)
			tot += flow[source][i];
		return tot;
	}
Beispiel #7
0
int main()
{
	freopen("a", "r", stdin);
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &M, &N);
		int source = M, sink = N;
		sap.init(M + 1);
		for (int i = 0; i < M; i++)
		{
			char str[10];
			int num;
			scanf("%s %d\n", str, &num);
			if (str[0] == 'I') 
			{
				sap.addCap(source, i, inf);
				//cout << "source " << i << " " << inf << endl;
			}
			for (int j = 1; j <= num; j++)
			{
				int k;
				scanf("%d", &k);
				sap.addCap(i, k, inf);
				sap.addCap(k, i, 1);
				//cout << i << " " << k << " " << inf << endl;
				//cout << k << " " << i << " " << 1 << endl;
			}
		}
		int tmp = sap.solve(source, sink);
		if (tmp >= inf) printf("PANIC ROOM BREACH\n");
		else printf("%d\n", tmp);
	}
	return 0;
}
Beispiel #8
0
int main()
{
	freopen("a", "r", stdin);
	while (scanf("%d%d", &N, &M) != EOF)
	{
		int source = 0, sink = N - 1;
		sap.init(N);
		for (int i = 1; i <= M; i++)
		{
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			sap.addCap(u, v, w);
		}
		int tmp = sap.solve(source, sink);
		sap.dfs1(source);
		sap.dfs2(sink);
		sap.output();
	}
	return 0;
}