# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]==-1) return x; return fa[x]=find(fa[x]); } int main() { int i,n,sum; while(~scanf("%d",&n)) { memset(fa,-1,sizeof(fa)); for(i=0;i<n;i++) scanf("%d%d",&a[i].p,&a[i].d); sort(a,a+n,cmp); sum=0; for(i=0;i<n;i++) { int x=find(a[i].d); if(x>0) { fa[x]=x-1; sum+=a[i].p; } } printf("%d\n",sum); } return 0; }
poj 1456 Supermarket (贪心+并查集),布布扣,bubuko.com
原文地址:http://blog.csdn.net/lp_opai/article/details/38355889