Cod sursa(job #2338691)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 7 februarie 2019 18:35:53
Problema Paznici2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>
using namespace std;

const int DIM = 405;
const int INF = 1000000005;

vector<int> edg[DIM]; bitset<DIM> oki;
int cap[DIM][DIM], fth[DIM], dst[DIM], cst[DIM][DIM];

void addEdge(int x, int y, int c) {
	edg[x].push_back(y); cap[x][y] = 1; cst[x][y] = c;
	edg[y].push_back(x); cap[y][x] = 0; cst[y][x] = -c; }

int bellmanFord(int s, int d) {
	static deque<int> que;
	for (int i = 0; i < DIM; ++i) {
		dst[i] = INF; }
	dst[s] = 0; que.assign(1, s); oki[s] = true;
	while (que.size()) {
		int x = que.front();
		for (int y : edg[x]) {	
			if (dst[y] > dst[x] + cst[x][y] and cap[x][y]) {
				dst[y] = dst[x] + cst[x][y]; fth[y] = x;
				if (!oki[y]) {
					oki[y] = true; que.push_back(y); } } } 
		oki[x] = false; que.pop_front(); }
	return dst[d]; }

int maxFlow(int s, int d) {
	int c = 0;
	while (bellmanFord(s, d) != INF) {		
		int mn = INF;
		for (int x = d; x != s; x = fth[x]) {
			mn = min(mn, cap[fth[x]][x]); }
		for (int x = d; x != s; x = fth[x]) {
			c += mn * cst[fth[x]][x];
			cap[fth[x]][x] -= mn; cap[x][fth[x]] += mn; } }
	return c; }

int main(void) {
	freopen("paznici2.in", "r", stdin);
	freopen("paznici2.out", "w", stdout);
	int n; cin >> n;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			int x; cin >> x;
			addEdge(i, j + n, x); }
		addEdge(0, i, 0); addEdge(i + n, 2 * n + 1, 0); }
	cout << maxFlow(0, 2 * n + 1) << "\n";
	for (int i = 1; i <= n; ++i) {	
		static vector<int> lst;
		bellmanFord(i + n, 2 * n + 1); lst.clear();
		for (int j = 1; j <= n; ++j) {
			if (dst[j] == cst[i + n][j]) {
				lst.push_back(j); } }	
		cout << lst.size() << " ";
		for (int x : lst) {
			cout << x << " "; }
		cout << "\n"; }	
	return 0; }