您的位置:首页 > 其它

UVA 7392 Bundles of Joy(乱搞)

2016-08-20 14:40 232 查看
给定蛋糕店的蛋糕,问花最少的钱买所有的n个蛋糕

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

struct node
{
ll dp;
ll now;
vector<int>v;
vector<int>tree;
};

bool com(const node &x, const node &y)
{
return x.v.size() < y.v.size();
}
int fa[500];
node a[2000];

void init()
{
memset(fa, -1, sizeof fa);
for (int i = 0; i < 200; i++)
{
a[i].dp = -1;
a[i].v.clear();
a[i].tree.clear();
}
}

int main()
{
int T;
cin >> T;
while (T--)
{
int n, m;
init();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
a[0].v.push_back(i);
a[0].now = 999999999999;
int cnt = 0;
for (int i = 1; i <= m; i++)
{
int x;
scanf("%lld%d", &a[i].now, &x);
for (int j = 0; j < x; j++)
{
int y;
scanf("%d", &y);
a[i].v.push_back(y);
}
}
sort(a, a + m + 1, com);
for (int i = 0; i <= m; i++)
{
node &z = a[i];
set<int>ss;
ll ans = 0; int flag = 0;
for (int j = 0; j < z.v.size(); j++)
{
if (fa[z.v[j]] == -1)flag = 1;
else ss.insert(fa[z.v[j]]);
fa[z.v[j]] = i;
}
set<int>::iterator po;
for (po = ss.begin(); po != ss.end(); po++)
{
z.tree.push_back(*po);
ans += a[*po].dp;
}
if (!flag)z.dp = min(z.now, ans);
else z.dp = z.now;
}
cout << a[m].dp << endl;
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVA