This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e9+7;
int n,q;
template <typename T>
bool minimize(T &a,T b)
{
if(a>b)
{
a=b;
return true;
}
return false;
}
struct edge
{
int x,value_x,value_y;
edge(int _x=0,int _value_x=0,int _value_y=0)
{
x=_x,value_x=_value_x,value_y=_value_y;
}
};
vector<vector<edge> >a;
struct SUB1
{
int n;
vector<vector<int> >dp;
SUB1(int _n=0)
{
n=_n;
dp.resize(n+7,vector<int>(2));
}
void dfs(int x,int par)
{
dp[x][0]=0;
dp[x][1]=inf;
for(edge need:a[x])
{
int node=need.x;
int value_x=need.value_x;
int value_y=need.value_y;
if(node==par)continue;
dfs(node,x);
dp[x][1]=dp[x][1]+dp[node][0]+value_x;
minimize(dp[x][1],dp[node][1]+dp[x][0]+value_y);
dp[x][0]+=dp[node][0]+value_x;
}
minimize(dp[x][1],dp[x][0]);
}
void calc()
{
dfs(1,0);
cout<<dp[1][1];
}
};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
a.resize(n+7);
for(int i=1;i<=n-1;i++)
{
int x,y,value_x,value_y;
cin>>x>>y>>value_x>>value_y;
a[x].push_back({y,value_x,value_y});
a[y].push_back({x,value_y,value_x});
}
int q;
cin>>q;
if(q==1)
{
int x;
cin>>x;
if(x==1)
{
SUB1 s(n);
s.calc();
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |