QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#306755 | #7511. Planar Graph | vavka | WA | 0ms | 3644kb | C++23 | 1.5kb | 2024-01-17 08:32:57 | 2024-01-17 08:32:57 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e-6
#define FOR(i, a, b) for(int i = a; i <= b; i ++)
#define REP(i, a, b) for(int i = a; i >= b; i --)
#define pb push_back
#define fr first
#define sd second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define N 5010
int n, m;
struct edge {int y, w, c;};
vector<edge>e[N];
int dp[N][N << 1];
int sz[N], s[N];
int tmp[N << 1];
void dfs(int x)
{
dp[x][s[x] + n] = 0; sz[x] = 1;
for(auto tmp : e[x]){int y = tmp.y; dfs(y);}
for(auto Tmp : e[x])
{
int y = Tmp.y, w = Tmp.w, c = Tmp.c;
FOR(i, -n, m)tmp[i + n] = inf;
FOR(i, -sz[x], s[x])if(dp[x][i + n] != inf)FOR(j, -sz[y], s[y])if(dp[y][j + n] != inf)
{
int tj = j;
if((abs(j) & 1) != c)tj --;
tmp[i + tj + n] = min(tmp[i + tj + n], dp[x][i + n] + dp[y][j + n] + abs(tj) * w);
}
sz[x] += sz[y]; s[x] += s[y];
FOR(i, -sz[x], s[x])dp[x][i + n] = tmp[i + n];
}
REP(i, s[x] - 1, -sz[x])dp[x][i + n] = min(dp[x][i + n + 1], dp[x][i + n]);
}
void sol()
{
cin >> n >> m;
FOR(i, 1, n - 1)
{
int x, y, w, c;
cin >> x >> y >> w >> c;
e[x].push_back((edge){y, w, c});
}
FOR(x, 1, n)FOR(i, -n, m)dp[x][i + n] = inf;
FOR(i, 1, m){int x; cin >> x; s[x] ++;}
dfs(1);
int ans = inf;
FOR(i, 0, m)ans = min(ans, dp[1][i + n]);
if(ans == inf)cout << "-1\n";
else cout << ans << '\n';
FOR(i, 1, n)e[i].clear(), s[i] = sz[i] = 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T;
while(T --)sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3644kb
input:
4 1 3 -2 0 0 2 2 0 0 1 0 3 1 2 2 3 1 3
output:
0 0 0 0
result:
wrong answer 1st lines differ - expected: '111', found: '0'