diff --git a/src/P5024/P5024.cpp b/src/P5024/P5024.cpp new file mode 100644 index 0000000..eebc567 --- /dev/null +++ b/src/P5024/P5024.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +using std::cin, std::cout; +using ll = int64_t; +const ll max_n = 1e5+5, inf = std::numeric_limits::max(); + +ll n, m, tn, dp[max_n][2], p[max_n], add; +ll a, x, b, y; +std::vector 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>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'; + } + +} \ No newline at end of file