#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll
#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2
int w[17][17];
int used[17][17];
int ans[17];
vector<int> G[17];
vector<pii> v[17];
void dfs(int index,int now,int p)
{
for (int i:G[now])
if (i!=p)
{
v[index].pb(mkp(i,now));
//used[now][i]++;
dfs(index,i,now);
}
}
int32_t main()
{
IOS
int n;
cin>>n;
for (int i=1;i<=n-1;i++)
{
int x,y,z,v;
cin>>x>>y>>z>>v;
w[x][y]=z;
w[y][x]=v;
G[x].pb(y);
G[y].pb(x);
}
for (int i=1;i<=n;i++) dfs(i,i,-1);
for (int i=0;i<=n;i++) ans[i]=inff;
for (int i=1;i<(1ll<<n);i++)
{
memset(used,0,sizeof(used));
for (int j=0;j<n;j++)
{
if (i&(1ll<<j))
{
for (pii k:v[j+1]) used[k.F][k.S]++;
}
}
int sum=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (w[i][j]!=0&&used[i][j]==0) sum+=w[i][j];
//cout<<i<<' '<<sum<<endl;
ans[__builtin_popcountll(i)]=min(ans[__builtin_popcountll(i)],sum);
}
int q;
cin>>q;
while (q--)
{
int x;
cin>>x;
cout<<ans[x]<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
83 ms |
376 KB |
Output is correct |
3 |
Correct |
83 ms |
504 KB |
Output is correct |
4 |
Correct |
85 ms |
376 KB |
Output is correct |
5 |
Correct |
84 ms |
376 KB |
Output is correct |
6 |
Correct |
83 ms |
376 KB |
Output is correct |
7 |
Correct |
84 ms |
376 KB |
Output is correct |
8 |
Correct |
83 ms |
376 KB |
Output is correct |
9 |
Correct |
82 ms |
376 KB |
Output is correct |
10 |
Correct |
85 ms |
376 KB |
Output is correct |
11 |
Correct |
74 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Runtime error |
5 ms |
504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Runtime error |
6 ms |
504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
83 ms |
376 KB |
Output is correct |
3 |
Correct |
83 ms |
504 KB |
Output is correct |
4 |
Correct |
85 ms |
376 KB |
Output is correct |
5 |
Correct |
84 ms |
376 KB |
Output is correct |
6 |
Correct |
83 ms |
376 KB |
Output is correct |
7 |
Correct |
84 ms |
376 KB |
Output is correct |
8 |
Correct |
83 ms |
376 KB |
Output is correct |
9 |
Correct |
82 ms |
376 KB |
Output is correct |
10 |
Correct |
85 ms |
376 KB |
Output is correct |
11 |
Correct |
74 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Runtime error |
5 ms |
504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Runtime error |
5 ms |
504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
83 ms |
376 KB |
Output is correct |
3 |
Correct |
83 ms |
504 KB |
Output is correct |
4 |
Correct |
85 ms |
376 KB |
Output is correct |
5 |
Correct |
84 ms |
376 KB |
Output is correct |
6 |
Correct |
83 ms |
376 KB |
Output is correct |
7 |
Correct |
84 ms |
376 KB |
Output is correct |
8 |
Correct |
83 ms |
376 KB |
Output is correct |
9 |
Correct |
82 ms |
376 KB |
Output is correct |
10 |
Correct |
85 ms |
376 KB |
Output is correct |
11 |
Correct |
74 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Runtime error |
5 ms |
504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Halted |
0 ms |
0 KB |
- |