poj 1456 supermarket
2011-11-29 16:32
225 查看
贪心+并查集
#include<stdio.h> #include<stdlib.h> struct Product { int p, t; }pro[10005]; int f[10005],n; int compare(const void *m,const void *n) { return (*(Product *)n).p-(*(Product *)m).p; } void init() { for (int i = 0; i <10005; i++) f[i] = i; } int find(int i) { if(f[i]!=i) f[i]=find(f[i]); return f[i]; } void unionSet(int m,int n) { int i=find(m); int j=find(n); f[i]=j; } int main() { while (scanf("%d",&n)!=EOF) { for (int i = 0; i < n; i++) scanf("%d%d",&pro[i].p,&pro[i].t); qsort(pro,n,sizeof(Product),compare); init(); int sum=0; for(int i=0;i<n;i++) { int temp=find(pro[i].t); if(temp!=0) { sum+=pro[i].p; unionSet(temp,temp-1); } } printf("%d\n",sum); } }
相关文章推荐
- poj 1456 Supermarket
- POJ 1456 Supermarket
- 【POJ 1456】Supermarket
- POJ 1456 Supermarket
- 【经典贪心】poj 1456 Supermarket (贪心, 并查集)
- POJ-1456 Supermarket【贪心】
- POJ 1456 Supermarket(贪心)
- Poj 1456 Supermarket
- Supermarket poj 1456 贪心+并查集优化
- poj1456 Supermarket
- poj 1456 supermarket
- Supermarket poj 1456
- POJ 1456 (贪心+并查集) Supermarket
- Poj 1456 Supermarket(2)
- Supermarket POJ - 1456
- POJ 1456 Supermarket
- POJ-1456 Supermarket 贪心问题 有时间限制的最小化惩罚问题
- POJ 1456 Supermarket 【贪心】
- poj1456——Supermarket
- [POJ 1456]Supermarket[贪心][优先队列or并查集优化]