QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#314625#8138. Alice and Her Lost CatmshcherbaAC ✓20ms4228kbC++202.0kb2024-01-25 23:55:202024-01-25 23:55:21

Judging History

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

  • [2024-01-25 23:55:21]
  • 评测
  • 测评结果:AC
  • 用时:20ms
  • 内存:4228kb
  • [2024-01-25 23:55:20]
  • 提交

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 pair<int, int> PII;
typedef double db;

const int INF = 1'000'000'007;
const int N = 1 << 11;

int n;
int a[N], t[N];
VI g[N];

void read()
{
	cin >> n;
	FOR(i, 0, n)
		cin >> a[i];
	FOR(i, 1, n + 1)
		cin >> t[i];
	FOR(i, 0, n - 1)
	{
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		g[u].PB(v);
		g[v].PB(u);
	}
}

void updMin(int& x, int val)
{
	x = min(x, val);
}

vector<VI> dfs(int v, int p)
{
	bool isLeaf = true;
	VI dp1 = {0};
	vector<VI> dp2 = {{0}, {0}};
	for (int to : g[v])
	{
		if (to == p)
			continue;
		isLeaf = false;
		auto dpTo = dfs(to, v);
		assert(SZ(dpTo[0]) == SZ(dpTo[1]));
		int nsz = SZ(dp1) + SZ(dpTo[0]) - 1;
		VI ndp1(nsz, INF);
		vector ndp2(2, VI(nsz, INF));
		FOR(c1, 0, SZ(dp1))
		{
			FOR(c2, 0, SZ(dpTo[0]))
			{
				updMin(ndp1[c1 + c2], dp1[c1] + dpTo[1][c2]);
				FOR(i, 0, 2)
				{
					FOR(j, 0, 2 - i)
					{
						updMin(ndp2[i + j][c1 + c2], dp2[i][c1] + dpTo[j][c2]);
					}
				}
			}
		}
		dp1 = ndp1;
		dp2 = ndp2;
		assert(SZ(dp1) == SZ(dp2[0]) && SZ(dp1) == SZ(dp2[1]));
	}
	if (isLeaf)
	{
		return {{a[v], 0}, {0, 0}};
	}
	vector dp(2, VI(SZ(dp1)));
	FOR(c, 0, SZ(dp1))
	{
		dp[0][c] = min(dp2[0][c], a[v] + dp1[c]);
		dp[1][c] = min(dp2[1][c], dp[0][c]);
	}
	return dp;
}

void solve()
{
	auto res = dfs(0, -1)[1];
	int ans = INF;
	FOR(i, 0, SZ(res))
	{
		ans = min(ans, res[i] + t[i]);
	}
	cout << ans << "\n";
}

void clear()
{
	FOR(i, 0, n)
		g[i].clear();
}

int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	cout << fixed << setprecision(15);
	int tc;
	cin >> tc;
	while (tc--)
	{
		read();
		solve();
		clear();
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

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

input:

2
8
4 2 5 2 4 2 3 2
5 5 6 7 8 9 10 13
1 2
2 3
1 4
1 5
4 6
3 7
5 8
8
4 2 3 3 2 4 4 3
4 6 8 8 9 9 14 17
1 2
2 3
3 4
3 5
4 6
3 7
3 8

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

3
1
10
5
3
2 1 4
1 2 3
1 2
2 3
10
2 1 4 3 5 6 7 8 9 3
1 1 2 3 4 5 6 7 8 8
1 2
2 3
1 4
1 5
5 6
4 7
1 8
1 9
7 10

output:

0
0
2

result:

ok 3 number(s): "0 0 2"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3556kb

input:

5
10
1 1 3 4 4 2 3 3 3 1
4 4 7 9 17 17 19 19 19 20
1 2
2 3
1 4
2 5
1 6
3 7
1 8
2 9
2 10
10
2 3 3 3 4 1 3 3 1 3
2 4 5 6 9 10 12 15 19 20
1 2
2 3
2 4
1 5
5 6
6 7
7 8
1 9
5 10
10
3 3 3 3 3 3 1 4 2 3
1 4 10 10 14 15 16 17 18 20
1 2
1 3
3 4
1 5
2 6
4 7
6 8
2 9
3 10
10
1 2 1 3 2 3 1 2 1 2
1 1 2 3 4 5 9 9 ...

output:

2
5
5
2
2

result:

ok 5 number(s): "2 5 5 2 2"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3624kb

input:

5
20
3 5 1 1 3 4 7 8 3 7 1 1 7 1 3 3 5 1 5 7
1 14 25 27 30 34 39 42 45 48 50 50 55 61 63 69 74 87 89 100
1 2
1 3
3 4
1 5
2 6
6 7
6 8
1 9
7 10
5 11
7 12
7 13
12 14
12 15
1 16
5 17
4 18
9 19
2 20
20
3 8 2 4 7 6 1 1 2 2 7 5 1 1 2 6 4 5 7 1
2 10 24 25 26 28 28 37 47 47 52 64 79 80 84 86 90 93 96 100
1 2...

output:

10
9
17
11
9

result:

ok 5 number(s): "10 9 17 11 9"

Test #5:

score: 0
Accepted
time: 9ms
memory: 3916kb

input:

10
1400
9827328 3447883 6979832 2361516 3201742 9881490 185103 6613510 7140147 9824105 7999173 649272 9056196 6021519 9367368 8153964 9840232 5449142 8119900 4715774 987231 1057668 5380695 9747965 2253752 1322003 2341542 2845358 5610842 8241778 4692550 7564041 2602876 4842402 3872716 3869113 7268396...

output:

342781747
371288274
187920801
21955588
250360535
236318821
224802283
390524828
281444264
291421823

result:

ok 10 numbers

Test #6:

score: 0
Accepted
time: 8ms
memory: 3832kb

input:

10
453
5186175 1420962 8950921 3420584 8352122 1606964 3615305 1662335 7129112 3853538 6275884 9171435 3518623 3492528 7692505 896099 815548 2768295 8173599 7502498 2580478 8521940 1367535 3474343 5647192 5476807 9769184 4719283 7538595 7137045 9438506 1272015 1397402 2647766 1270571 1122883 2075228...

output:

206244715
341230840
244108106
365394739
358176901
115691440
169329823
18005551
319842026
69815045

result:

ok 10 numbers

Test #7:

score: 0
Accepted
time: 7ms
memory: 3892kb

input:

10
1505
6803198 9426809 954777 705060 3502503 3299671 7078275 6678392 7118077 4206683 8294418 7660830 1788411 930769 6017642 7380058 8049040 152984 8227298 321991 4173725 2309924 7387143 942545 2815224 3406202 7196826 6560441 9466349 6032311 4184462 4947221 3868216 6711306 8701195 4602061 623884 797...

output:

361208793
43741741
261474287
336596228
308022071
311882982
354011854
278470501
88889642
332327962

result:

ok 10 numbers

Test #8:

score: 0
Accepted
time: 20ms
memory: 4228kb

input:

10
2000
3006604 5311265 2859626 3609818 8193678 7844282 2223127 7294340 1308287 6762790 88400 7428240 18325 6641350 8972559 2660020 9868202 7449155 8047535 5532457 6643387 6091653 3134704 500907 6052842 9516604 8558965 1950203 3278845 9863131 9361606 5492957 9812317 7335192 339790 4364441 4093591 98...

output:

377336374
388737941
381821708
333910519
360165603
344789836
391740730
364782918
385405245
382161455

result:

ok 10 numbers

Test #9:

score: 0
Accepted
time: 18ms
memory: 4016kb

input:

10
2000
3115656 6928288 865473 5613675 5510922 2994662 3948601 4466366 66168 6718986 4150601 5672183 4765896 1103778 6410800 1017925 6384929 4715414 5432225 5553388 9495648 7684900 6889920 2778691 9811988 2975580 2779304 9377845 5152771 1823653 1998696 238912 9745699 9838775 8145154 1762297 1314593 ...

output:

330389721
372008389
369537673
368481583
364593955
355194354
347294289
366850614
363318922
379964359

result:

ok 10 numbers

Test #10:

score: 0
Accepted
time: 20ms
memory: 3928kb

input:

10
2000
3224709 2287136 8838552 7584764 6537222 8145042 5608539 7896568 5114994 6740719 4470978 7657949 3288059 5631742 3849042 9343062 9127064 5690730 2751378 5607087 2315141 9212611 645136 8765531 3505598 6369021 675932 6805487 768520 3751407 893963 4984868 3453673 8567765 2208694 5451097 8535594 ...

output:

406240490
396273822
373075313
373215271
366225425
372414756
349190757
388338626
357010416
385577925

result:

ok 10 numbers

Test #11:

score: 0
Accepted
time: 20ms
memory: 4016kb

input:

10
2000
3333761 3904159 3069807 5846797 3854466 3295422 1075837 1359538 131051 6729684 8533179 5934660 8068398 3835993 7578227 7668199 5611024 2924221 136067 5693554 5134633 805858 8109408 4785139 941032 3504285 4863504 4233129 2609678 5711928 9854766 3472648 7128879 1104115 14057 2848952 2047540 28...

output:

351661825
367417601
363942333
380444399
377825080
363473980
361578810
379150119
338648231
364417836

result:

ok 10 numbers

Extra Test:

score: 0
Extra Test Passed