您的位置:首页 > 其它

POJ 1456 Supermarket

2010-12-06 22:24 232 查看
解题思路:贪心+并查集优化

欢迎review

#include <iostream>
using namespace std;

int root[10001], pro[10001];
struct Prod{
int p, d;
}node[10000];
int cmp(const void *a, const void *b)
{
return (*(Prod*)a).p < (*(Prod*)b).p ? 1 : -1;
}
int findroot(int x)
{
if(root[x] != x)
root[x] = findroot(root[x]);
return root[x];
}
inline void Insert(Prod& cur)
{
int t, x = cur.d;
if(t = findroot(x))pro[t]=cur.p, root[t] = t - 1;
}
int main()
{
int n, i, ans;
while (scanf("%d", &n)==1)
{
for(i = 0; i < n; i++)
scanf("%d %d", &node[i].p, &node[i].d);
qsort(node, n, sizeof(Prod), cmp);
for(i=0; i < 10001; i++)root[i]=i, pro[i]=0;
for(i=0; i < n; i++) Insert(node[i]);
for(i=ans=0;i<10001; i++)ans+=pro[i];
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: