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;
}
}
#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;
}
}
相关文章推荐
- UVALive 7392 Bundles of Joy【bitset】【类树形DP】【杂题】
- UVALive-7392 - Bundles of Joy【树型DP】【深搜】【好题】
- Bundles of Joy
- UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- uva12373 - Pair of Touching Circles
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
- (intermediate) 平面区域 UVA Art of War
- UVA - 591 Box of Bricks
- uva 759 - The Return of the Roman Empire
- UVa 10499 - The Land of Justice
- UVA 10344 23 out of 5(搜索+全排列)
- UVA - 10420 List of Conquests
- UVA 10891 - Game of Sum*
- uva 10891 - Game of Sum
- uva10891 Game of Sum
- UVa 10891 Game of Sum
- uva 993 - Product of digits
- UVA 1364 - Knights of the Round Table (找双连通分量 + 二分图染色法判断)
- UVA - 10891 Game of Sum(区间dp)
- UVA 10420 - List of Conquests