This commit is contained in:
Zengtudor 2024-11-16 08:10:55 +08:00
parent 8353d5b6d9
commit 5ed151aa4f

68
src/P5024/P5024.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <limits>
#include <vector>
using std::cin, std::cout;
using ll = int64_t;
const ll max_n = 1e5+5, inf = std::numeric_limits<unsigned int>::max();
ll n, m, tn, dp[max_n][2], p[max_n], add;
ll a, x, b, y;
std::vector<ll> edges[max_n];
char type;
void dfs(const ll &ft, const ll &now){
dp[now][1] = p[now];
if(now==a){
dp[now][1] = (x==0? inf: 0);
}else if(now==b){
dp[now][1] = (y==0? inf: 0);
}
for(const ll &next: edges[now]){
if(ft == next)continue;
dfs(now, next);
dp[now][0] += dp[next][1];
dp[now][1] += std::min(dp[next][0], dp[next][1]);
}
}
int main(){
std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>m>>type>>tn;
for(ll i{1};i<=n;i++){
cin>>p[i];
}
for(ll i{1};i<n;i++){
ll u,v;
cin>>u>>v;
edges[u].push_back(v);
edges[v].push_back(u);
}
for(ll i{1};i<=m;i++){
cin>>a>>x>>b>>y;
add = 0;
if(x==1)add+=p[a];
if(y==1)add+=p[b];
if(
x==0 &&
y==0 &&
std::find(edges[a].begin(), edges[a].end(), b)
!=edges[a].end()
){
cout<<"-1\n";
continue;
}
for(ll i{0};i<=n;i++){
for(ll j{0};j<2;j++){
dp[i][j] = 0;
}
}
dfs(0, 1);
cout<<(std::min(dp[1][0], dp[1][1])+add)<<'\n';
}
}