QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#561239#3588. Hamilton - The MusicalYaremaRE 1ms3648kbC++201.9kb2024-09-12 21:25:132024-09-12 21:25:14

Judging History

你现在查看的是最新测评结果

  • [2024-09-12 21:25:14]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3648kb
  • [2024-09-12 21:25:13]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

const LL LINF = 1e18;

LL hungarian(const vector<vector<LL>>& a)
{
	int n = SZ(a), m = SZ(a[0]);
	assert(n <= m);
	VL u(n + 1), v(m + 1);
	VI p(m + 1, n), way(m + 1);
	FOR (i, 0, n)
	{
		p[m] = i;
		int j0 = m;
		VL minv(m + 1, LINF);
		VI used(m + 1);
		while (p[j0] != n)
		{
			used[j0] = true;
			int i0 = p[j0], j1 = -1;
			LL delta = LINF;
			FOR (j, 0, m)
			{
				if (!used[j])
				{
					int cur = a[i0][j] - u[i0] - v[j];
					if (cur < minv[j])
					{
						minv[j] = cur;
						way[j] = j0;
					}
					if (minv[j] < delta)
					{
						delta = minv[j];
						j1 = j;
					}
				}
			}
			assert(j1 != -1);
			FOR (j, 0, m + 1)
			{
				if (used[j])
				{
					u[p[j]] += delta;
					v[j] -= delta;
				}
				else
					minv[j] -= delta;
			}
			j0 = j1;
		}
		while (j0 != m)
		{
			int j1 = way[j0];
			p[j0] = p[j1];
			j0 = j1;
		}
	}
	VI ans(n + 1);
	FOR (j, 0, m)
		ans[p[j]] = j;
	LL res = 0;
	FOR (i, 0, n)
		res += a[i][ans[i]];
	assert(res == -v[m]);
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	vector<VI> v(n, VI(n));
	FOR (i, 0, n)
		FOR (j, 0, n)
			cin >> v[i][j];
	
	int m = (n + 1) / 2;
	vector<VL> a(m, VL(m));
	FOR (i, 0, n)
	{
		FOR (j, 0, n)
		{
			if (j)
				a[i / 2][j / 2] += 1000ll * v[i][j - 1];
			if (j != n - 1)
				a[i / 2][j / 2] += 1000ll * v[i][j + 1];
			j++;
		}
		i++;
	}
	cout << hungarian(a) / 1000 << '\n';
	
	return 0;
}



詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3648kb

input:

4
0 3 2 13
3 0 8 9
2 8 0 5
13 9 5 0

output:

16

result:

ok single line: '16'

Test #2:

score: -100
Runtime Error

input:

455
0 71154840 37432199 724743679 761809953 949789082 725973225 28455138 924138757 574256423 297516452 223475432 693485895 699629134 731875885 697643903 595490098 206757530 965177624 178416136 103223719 474785234 322984824 510200285 656185874 993023494 973542087 511171280 465648799 836547414 8145240...

output:


result: