結果

問題 No.519 アイドルユニット
ユーザー snrnsidysnrnsidy
提出日時 2021-06-12 02:23:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,664 bytes
コンパイル時間 2,916 ms
コンパイル使用メモリ 208,524 KB
実行使用メモリ 11,972 KB
最終ジャッジ日時 2023-08-21 17:15:07
合計ジャッジ時間 6,820 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int N = 26;
const int S = N - 2;
const int E = N - 1;
const int INF = 1000000000;

int n, m;
int c[N][N] = { 0, };
int d[N][N] = { 0, };
int f[N][N] = { 0, };
vector <int> adj[N];
int board[25][25];

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n,m;

	cin >> m;

	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> board[i][j];
		}
	}
	n = m / 2;
	vector <int> perm(m, 0);
	for (int i = 0; i < n; i++)
	{
		perm[i] = 1;
	}

	sort(perm.begin(), perm.end());

	int ret = 0;

	do
	{
		memset(c, 0, sizeof(c));
		memset(d, 0, sizeof(d));
		for (int i = 0; i < N; i++)
		{
			adj[i].clear();
		}
		vector <int> a, b;
		for (int i = 0; i < m; i++)
		{
			if (perm[i] == 0)
			{
				a.push_back(i);
			}
			else
			{
				b.push_back(i);
			}
		}
		for (int i = 0;i<a.size(); i++)
		{
			c[S][a[i]] = 1;
			d[S][a[i]] = 0;
			d[a[i]][S] = 0;

			adj[S].push_back(a[i]);
			adj[a[i]].push_back(S);
		}
		for (int i = 0;i<b.size();i++)
		{
			c[b[i]][E] = 1;
			d[b[i]][E] = 0;
			d[E][b[i]] = 0;

			adj[b[i]].push_back(E);
			adj[E].push_back(b[i]);
		}
		for (int i = 0; i < a.size(); i++)
		{
			for (int j = 0; j < b.size(); j++)
			{
				//cout << a[i] << ' ' << b[j] << ' ' << board[a[i]][b[j]] << '\n';
				adj[a[i]].push_back(b[j]);
				adj[b[j]].push_back(a[i]);
				d[a[i]][b[j]] = -board[a[i]][b[j]];
				d[b[j]][a[i]] = board[a[i]][b[j]];
				c[a[i]][b[j]] = 1;
			}
		}

		int res = 0;
		int cnt = 0;

		memset(f, 0, sizeof(f));
		while (1)
		{
			int prev[N], dist[N];
			bool inqueue[N];
			memset(inqueue, false, sizeof(inqueue));
			queue <int> que;
			memset(prev, -1, sizeof(prev));

			for (int i = 0; i < N; i++)
			{
				dist[i] = 1e9;
			}

			que.push(S);
			dist[S] = 0;
			inqueue[S] = true;
			while (!que.empty())
			{
				int now = que.front();
				que.pop();

				inqueue[now] = false;
				for (auto next : adj[now])
				{
					//cout << now << ' ' << next << ' ' << c[now][next] << ' ' << f[now][next] << ' ' << dist[now] << ' ' << dist[next] << ' ' << d[now][next] << '\n';
					if (c[now][next] - f[now][next] > 0 && dist[next] > dist[now] + d[now][next])
					{
						dist[next] = dist[now] + d[now][next];
						prev[next] = now;

						if (!inqueue[next])
						{
							que.push(next);
							inqueue[next] = true;
						}
					}
				}
			}

			if (prev[E] == -1)
			{
				break;
			}

			for (int i = E; i != S; i = prev[i])
			{
				res += d[prev[i]][i];
				f[prev[i]][i]++;
				f[i][prev[i]]--;
			}
			cnt++;
		}
		ret = max(ret, abs(res));
	} while (next_permutation(perm.begin(), perm.end()));

	cout << ret << '\n';
	return 0;
}
0