POJ 1456 Supermarket
2010-12-06 22:24
232 查看
解题思路:贪心+并查集优化
欢迎review
欢迎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; }
相关文章推荐
- G - Supermarket poj1456
- poj 1456 Supermarket (最短路程)
- POJ - 1456 Supermarket
- POJ 1456 Supermarket 并查集(想不到的思路)
- poj 1456 - Supermarket(贪心+并查集)
- POJ 1456 Supermarket
- POJ 1456 Supermarket(贪心算法,可用并查集优化)
- poj 1456 Supermarket (贪心, 并查集)
- Supermarket POJ - 1456 (堆)
- POJ-1456 Supermarket(贪心,并查集优化)
- POJ 1456 Supermarket
- POJ 1456 Supermarket
- POJ-1456 Supermarket(贪心,并查集优化)
- POJ 1456 Supermarket
- POJ 1456 Supermarket (贪心+并查集)
- poj 1456 Supermarket 贪心 并查集优化
- POJ-1456 Supermarket (贪心)
- POJ 1456 Supermarket解题总结
- 【POJ 1456 Supermarket】
- POJ - 1456 Supermarket (贪心)