QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#629471#7502. Painting the RoadsOccDreamerWA 4ms5704kbC++142.7kb2024-10-11 12:12:372024-10-11 12:12:39

Judging History

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

  • [2024-10-11 12:12:39]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:5704kb
  • [2024-10-11 12:12:37]
  • 提交

answer

//Mashiro
#include<bits/stdc++.h>

#define vc vector
#define db double
#define fi first
#define se second
#define ll long long
#define mk make_pair
#define pb push_back
#define RI register int
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << "   -_-   " << endl
#define debug cerr << " ------------------- " << endl

#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)

#define NO puts("No")
#define YES puts("Yes")

//#define OccDreamer
//#define int long long

using namespace std;

namespace IO{
	inline int read(){
		int X=0, W=0; char ch=getchar();
		while(!isdigit(ch)) W|=ch=='-', ch=getchar();
		while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
		return W?-X:X;
	}
	inline void write(int x){
		if(x<0) x=-x, putchar('-');
		if(x>9) write(x/10);
		putchar(x%10+'0');
	}
	inline void sprint(int x){write(x), putchar(32);}
	inline void eprint(int x){write(x), putchar(10);}
}using namespace IO;

const int MAXN = 5005;
const int mod = 998244353;

int n, m, f[MAXN][MAXN<<1], siz[MAXN], g[MAXN<<1], sz[MAXN];
int head[MAXN], ne[MAXN<<1], to[MAXN<<1], weight[MAXN<<1], ban[MAXN<<1], cnt;

inline void add(int x, int y, int w, int c){++cnt;to[cnt]=y;ne[cnt]=head[x];head[x]=cnt;weight[cnt]=w; ban[cnt]=c;}

inline void dfs(int x, int fa){
	//cerr << "dfs:" << x << ' ' << fa << endl;
	sz[x]=1; f[x][sz[x]-1]=0; int ouf, co, news;
	for(int i=0;i<=siz[x];++i) f[x][i+sz[x]]=0;
	// val range : [0,siz[x]+sz[x]]
	// f[i][j] 表示在 i 这一个点,j>sz[x] 表示向上出去了 j-sz[x] 条,j<=sz[x] 表示上面进来了 sz[x]-j 条 
	for(int i=head[x];i;i=ne[i]){
		if(to[i]==fa) continue; dfs(to[i],x);	
		for(int j=0;j<=sz[x]+siz[x];++j) g[j]=f[x][j], f[x][j]=1e9;
		news=sz[x]+sz[to[i]];
		for(int j=0;j<=sz[x]+siz[x];++j){
			for(int k=0;k<=sz[to[i]]+siz[to[i]];++k){
				if(ban[i]==((k-sz[to[i]])&1)) continue;
				ouf=k-sz[to[i]];
				co=abs(ouf)*weight[i];
				ouf+=j-sz[x];
				f[x][news+ouf]=min(g[j]+f[to[i]][k]+co,f[x][news+ouf]);
			}
		}
		sz[x]+=sz[to[i]]; siz[x]+=siz[to[i]];
	}
	return ;
}

inline void solve(){
	n=read(), m=read(); cnt=0;
	for(int i=1;i<=n;++i) head[i]=0;
	for(int i=1;i<=n;++i) memset(f[i],127/3,sizeof f[i]);
	for(int i=1;i<=n;++i) siz[i]=0;
	for(int i=2;i<=n;++i){
		int x, y, w, c;
		x=read(), y=read(), w=read(), c=read();
		add(x,y,w,!c), add(y,x,w,!c);
	}
	int x;
	for(int i=1;i<=m;++i) x=read(), siz[x]++;
	dfs(1,0); 
	int ans=f[1][n];
	if(ans>n*10) eprint(-1);
	else eprint(ans);
}

signed main(){
	int t=read();
	while(t--) solve();
	return 0;
}




















































Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 4ms
memory: 3844kb

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
7
8
0
10
1
1
7
5
28
12
-1
19
16
12
13
-1
32
9
18
16
14
10
12
16
0
11
-1
17
-1
9
14
27
8
11
-1
6
6
15
18
46
0
14
9
-1
5
8
22
-1
-1
17
-1
25
6
0
24
6
15
21
15
22
-1
6
0
-1
20
5
28
20
0
20
19
18
-1
10
0
16
9
19
6
21
11
11
4
6
20
11
0
8
8
31
8
23
-1
8
-1
11
-1
9
13
-1
-1
19
9
20
19
6
...

result:

wrong answer 71st numbers differ - expected: '65', found: '-1'