Exemplo n.º 1
0
int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d %d", &n, &m, &k);
		for (int i = 0; i < m; ++i) {
			scanf("%d %d %d", &a[i], &b[i], &c[i]);
		}
		int s = n * 2, t = n * 2 + 1;
		mcmf.init(n * 2 + 2, s, t);
		for (int i = 0; i < n; ++i) {
			mcmf.add(s, L(i), k, 0);
			mcmf.add(R(i), t, k, 0);
		}
		for (int i = 0; i < m; ++i) {
			mcmf.add(L(a[i]), R(b[i]), 1, c[i]);
		}
		std::pair<int, int> ans = mcmf.gao();
		if (ans.first < k * n) {
			puts("-1");
		} else {
			printf("%d\n", ans.second);
		}
	}
	return 0;
}
Exemplo n.º 2
0
int main() {
	int n;

	scanf("%s%d", t, &n);
	int S = 0, T = n + 27;
	mcmf.init(T+1, S, T);
	for (int i=1,c; i<=n; ++i) {
		scanf("%s%d", s, &c);
		mcmf.add_edge(S, i, c, 0);
		int cnt[26]={0};
		for (int k=0; s[k]; ++k) ++cnt[s[k]-'a'];
		for (int k=0; k<26; ++k) if (cnt[k]) mcmf.add_edge(i, k+1+n, cnt[k], i);
	}
	int sz = 0, cnt[26]={0};
	for (int i=0; t[i]; ++i) ++cnt[t[i]-'a'], ++sz;
	for (int i=0; i<26; ++i) if (cnt[i]) mcmf.add_edge(i+1+n, T, cnt[i], 0);
	mcmf.run();
	printf("%d\n", mcmf.maxflow == sz ? mcmf.mincost : -1);

	return 0;
}
int main() {
	int N,s,t,C,cid = 1;
	bool isFirst = true;
	while(cin>>N) {
		MinCostMaxFlow graph = MinCostMaxFlow(N);
		cin>>s>>t>>C;

		// read the input using AddEdge()
		while(C--) {
			int from, to; long cap;
			cin>>from>>to>>cap;
			graph.AddEdge(from,to,cap);
		}
		cout<<"Done adding edge"<<endl;

		long res = graph.GetMaxFlow(s,t).first;
		if(isFirst) isFirst = false;
		else cout<<endl;
		cout<<"Network "<<cid++<<endl;
		cout<<"The bandwidth is "<<res<<endl;
	}
	return 0;
}
Exemplo n.º 4
0
int main() {
        int r, c, n, k;
        int t, x, y;
        int p[16][16];

        while (scanf("%d%d%d%d", &r, &c, &n, &k) != EOF) {
                mcmf.init(N(), S(), T());
                mcmf.addEdge(S(), SX(), k, 0);
                for (int i = 0; i < r; ++i) {
                        for (int j = 0; j < c; ++j) {
                                scanf("%d", &p[i][j]);
                                if ((i + j) % 2 != 0) {
                                        mcmf.addEdge(ID(i, j), T(), 1, 0);
                                }
                        }
                }
                for (int i = 0; i < n; ++i) {
                        scanf("%d%d%d", &t, &x, &y);
                        --x;
                        --y;
                        mcmf.addEdge(SX(), ID(x, y), 1, 0);
                        for (int k = 0; k < 8; ++k) {
                                int xx = x + dx[k], yy = y + dy[k];
                                int cost = 0;
                                switch (t) {
                                        case 1: cost = p[x][y] * p[xx][yy]; break;
                                        case 2: cost = p[x][y] + p[xx][yy]; break;
                                        case 3: cost = max(p[x][y], p[xx][yy]); break;
                                }
                                if (VALID(xx, yy)) {
                                        mcmf.addEdge(ID(x, y), ID(xx, yy), 1, cost);
                                }
                        }
                }
                pair<int, int> cw = mcmf.gao();
                fprintf(stderr, "%d %d\n", cw.first, cw.second);
                if (cw.first != k) {
                        puts("-1");
                } else {
                        printf("%d\n", cw.second);
                }
        }

        return 0;
}