QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#629273 | #7502. Painting the Roads | lsj2009 | TL | 60ms | 6264kb | C++20 | 2.3kb | 2024-10-11 10:11:22 | 2024-10-11 10:11:25 |
Judging History
answer
#include<bits/stdc++.h>
//#pragma GCC optimize(3,"Ofast","inline")
//#define int long long
#define i128 __int128
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define ld double
#define PII pair<int,int>
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define chkmax(a,b) a=max(a,b)
#define chkmin(a,b) a=min(a,b)
#define rep(k,l,r) for(int k=l;k<=r;++k)
#define per(k,r,l) for(int k=r;k>=l;--k)
#define cl(f,x) memset(f,x,sizeof(f))
#define pcnt(x) __builtin_popcount(x)
#define lg(x) (31-__builtin_clz(x))
using namespace std;
void file_IO() {
// system("fc .out .ans");
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
bool M1;
const int N=1e4+5;
int head[N],len;
struct node {
int to,c,w,nxt;
}; node edge[N<<1];
void add_edge(int u,int v,int c,int w) {
edge[++len]={v,c,w,head[u]}; head[u]=len;
}
int f[N][N],tmp[N],siz[N],n,m;
int cnt[N];
void dfs(int u,int fa) {
rep(i,-n,0)
f[u][n+i+cnt[u]]=0;
siz[u]=1;
for(int i=head[u];i;i=edge[i].nxt) {
int v=edge[i].to,c=edge[i].c,w=edge[i].w;
if(v!=fa) {
dfs(v,u);
rep(i,-n,+n) {
tmp[n+i]=f[u][n+i];
f[u][n+i]=INF;
}
rep(i,-n,+n) {
rep(j,-n,+n) {
if((abs(j)&1)==c)
chkmin(f[u][n+i+j],tmp[n+i]+f[v][n+j]+abs(j)*w);
}
}
siz[u]+=siz[v];
}
}
}
/*
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
*/
void solve() {
len=0;
rep(i,1,n)
head[i]=cnt[i]=0;
rep(i,1,n) {
rep(j,0,n+n)
f[i][j]=INF;
}
scanf("%d%d",&n,&m);
rep(i,1,n) {
rep(j,0,n+n)
f[i][j]=INF;
}
rep(i,2,n) {
int u,v,w,c;
scanf("%d%d%d%d",&u,&v,&w,&c);
add_edge(u,v,c,w);
add_edge(v,u,c,w);
}
while(m--) {
int x;
scanf("%d",&x);
++cnt[x];
}
dfs(1,0);
printf("%d\n",f[1][n]>=INF? -1:f[1][n]);
}
bool M2;
signed main() {
//file_IO();
int testcase=1;
scanf("%d",&testcase);
while(testcase--)
solve();
fprintf(stderr,"used time = %ldms\n",1000*clock()/CLOCKS_PER_SEC);
fprintf(stderr,"used memory = %lldMB\n",(&M2-&M1)/1024/1024);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3972kb
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: 0
Accepted
time: 0ms
memory: 6112kb
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 65 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:
ok 1000 numbers
Test #3:
score: 0
Accepted
time: 7ms
memory: 6104kb
input:
250 20 10 1 2 10 1 1 3 3 1 2 4 6 0 4 5 8 0 2 6 5 0 4 7 1 1 1 8 6 0 4 9 2 1 3 10 9 1 3 11 1 0 6 12 10 0 6 13 8 0 13 14 7 0 14 15 10 1 1 16 4 0 12 17 2 0 17 18 8 1 18 19 3 0 2 20 4 1 15 14 20 20 9 17 4 4 13 13 20 6 1 2 4 1 1 3 10 1 2 4 10 1 3 5 1 1 2 6 6 0 4 7 5 1 3 8 8 1 7 9 7 0 6 10 3 1 6 11 4 1 8 1...
output:
47 -1 84 68 61 112 39 -1 -1 48 71 -1 -1 77 -1 -1 59 97 -1 -1 74 52 -1 -1 -1 84 48 -1 68 60 57 -1 79 68 -1 79 51 -1 33 48 62 -1 -1 113 84 44 -1 79 -1 76 64 -1 -1 110 42 40 56 81 -1 73 68 -1 74 125 67 44 -1 103 -1 -1 -1 89 64 -1 85 -1 50 -1 64 -1 -1 94 46 75 -1 -1 -1 101 67 61 -1 -1 18 100 57 102 -1 -...
result:
ok 250 numbers
Test #4:
score: 0
Accepted
time: 60ms
memory: 6264kb
input:
100 50 43 1 2 7 1 1 3 5 0 2 4 9 0 1 5 8 0 2 6 5 1 6 7 8 1 1 8 10 0 5 9 2 1 1 10 4 0 2 11 3 0 11 12 6 0 2 13 9 1 7 14 5 0 12 15 7 0 2 16 5 0 15 17 4 1 1 18 6 1 16 19 5 1 5 20 7 1 10 21 6 1 14 22 2 0 14 23 3 1 18 24 2 1 22 25 8 1 12 26 6 0 5 27 1 0 21 28 1 1 27 29 9 1 25 30 7 0 22 31 9 0 9 32 7 0 26 3...
output:
149 233 -1 204 -1 145 164 -1 -1 -1 185 182 -1 145 252 -1 -1 160 186 163 -1 256 182 173 202 238 410 -1 169 -1 259 -1 103 117 171 -1 -1 -1 163 -1 -1 265 -1 -1 -1 162 136 140 -1 -1 217 211 193 114 246 251 133 176 -1 -1 219 139 195 -1 214 180 -1 223 132 153 -1 -1 -1 172 310 102 -1 200 -1 -1 195 -1 -1 20...
result:
ok 100 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
10 500 815 1 2 8 1 1 3 1 0 1 4 4 1 4 5 5 0 4 6 8 0 3 7 7 0 5 8 2 1 4 9 9 1 2 10 9 1 1 11 3 1 1 12 9 0 2 13 2 0 5 14 1 1 3 15 1 1 3 16 4 0 5 17 8 0 4 18 9 0 4 19 4 0 2 20 7 0 4 21 7 1 5 22 9 1 2 23 10 1 3 24 7 0 1 25 7 0 1 26 2 0 4 27 4 1 1 28 9 0 3 29 3 1 2 30 6 0 3 31 10 1 1 32 3 1 1 33 2 1 3 34 2 ...