QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188834#7502. Painting the RoadsHDUT01#WA 2ms3980kbC++171.7kb2023-09-26 15:01:092023-09-26 15:01:10

Judging History

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

  • [2023-09-26 15:01:10]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3980kb
  • [2023-09-26 15:01:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=5010;
const int INF=1e9;
int T,n,m,x,y;
int l[N],col[N],sum[N],dp[N][N<<1],sz[N],s[N],f[N<<1];
int q[N<<1];
vector<pair<int,int>>e[N];
void dfs(int x,int y,int z,int w){
	sz[x]=1;s[x]=0;
	dp[x][m]=0;
	for (int i=0;i<e[x].size();i++){
		int u=e[x][i].first;
		int id=e[x][i].second;
		if (u==y) continue;
		dfs(u,x,l[id],col[id]);
		for (int j=m-s[x]-s[u];j<=m+sz[x]+sz[u];j++) f[j]=INF;
		for (int j=-s[x];j<=sz[x];j++)
			for (int k=-s[u];k<=sz[u];k++)
				f[j+k+m]=min(f[j+k+m],dp[x][j+m]+dp[u][k+m]);
		for (int j=m-s[x]-s[u];j<=m+sz[x]+sz[u];j++) dp[x][j]=f[j];
		sz[x]+=sz[u];
		s[x]+=s[u];
	}
	for (int j=m-s[x]-sum[x];j<=m+sz[x];j++) f[j]=INF;
	int l=1,r=0;
	for (int j=m+sz[x];j>=m-s[x]-sum[x];j--){
		q[++r]=j;
		while (r>l&&dp[x][q[r]]<=dp[x][q[r-1]]){
			r--; q[r]=j;
		}
		while (l<=r&&q[l]>j+sum[x]) l++;
		f[j]=dp[x][q[l]];
	}
	for (int j=m+1;j<=m+sz[x];j++) f[j]=min(f[j-1],f[j]);
	for (int j=m-s[x]-sum[x];j<=m+sz[x];j++) dp[x][j]=f[j];
	for (int j=m-s[x]-sum[x];j<=m+sz[x];j++) 
		if (dp[x][j]!=INF) dp[x][j]=min(INF,abs(j-m)*z+dp[x][j]);
	for (int j=m-s[x]-sum[x];j<=m+sz[x];j++)
		if ((abs(j-m)&1)!=w) dp[x][j]=INF;
	s[x]+=sum[x];
}
int main(){
	scanf("%d",&T);
	while (T--){
		scanf("%d%d",&n,&m);
		for (int i=1;i<=n;i++) e[i].clear(),sum[i]=0;
		for (int i=1;i<=n;i++)
			for (int j=0;j<=n+m;j++) dp[i][j]=INF;
		for (int i=1;i<n;i++){
			scanf("%d%d%d%d",&x,&y,&l[i],&col[i]);
			e[x].push_back(make_pair(y,i));
			e[y].push_back(make_pair(x,i));
		}
		for (int i=1;i<=m;i++){
			scanf("%d",&x); sum[x]++;
		}
		dfs(1,0,0,0);
		if (dp[1][m]==INF) puts("-1");
		else
		printf("%d\n",dp[1][m]);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

3
9
21
-1
42

result:

ok 5 number(s): "3 9 21 -1 42"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3980kb

input:

1000
5 5
1 2 4 1
2 3 9 0
3 4 10 1
3 5 8 1
1 5 2 5 1
5 3
1 2 7 1
1 3 7 0
2 4 9 0
3 5 4 1
3 4 3
5 3
1 2 7 1
2 3 1 0
1 4 7 1
4 5 5 1
4 4 3
5 1
1 2 3 1
1 3 6 0
2 4 10 0
2 5 7 0
1
5 3
1 2 10 1
1 3 10 0
1 4 1 1
3 5 4 0
2 5 2
5 5
1 2 7 0
1 3 5 0
2 4 8 1
2 5 10 0
2 2 3 5 4
5 4
1 2 6 1
1 3 4 0
3 4 4 0
1 5 5 ...

output:

22
-1
19
3
11
8
11
-1
-1
0
38
1
1
-1
-1
28
-1
-1
19
16
26
13
-1
-1
9
18
16
14
10
12
24
0
11
-1
17
-1
-1
14
27
-1
11
-1
6
6
15
18
46
0
14
9
-1
-1
8
22
-1
-1
17
-1
25
6
0
24
6
15
21
15
22
-1
6
0
-1
20
5
-1
20
0
20
-1
18
-1
10
0
-1
-1
27
-1
-1
11
11
4
-1
20
11
0
8
20
31
-1
-1
-1
8
-1
11
-1
9
13
-1
-1
1...

result:

wrong answer 8th numbers differ - expected: '7', found: '-1'