标签:
Description
Input
Output
Sample Input
4 50 2 10 1 20 2 30 1 7 20 1 2 1 10 3 100 2 8 2 5 20 50 10
Sample Output
80 185
Hint
#include<stdio.h> #include<algorithm> #include<string.h> #include<iostream> using namespace std; const int maxn = 1e5+200; struct Product{ int p,d; }products[maxn]; struct Set{ int pa; }sets[maxn]; bool cmp(Product a,Product b){ return a.p>b.p; } int Find(int x){ if(x == sets[x].pa){ return x; } int tmp = sets[x].pa; sets[x].pa = Find(tmp); //路径压缩 return sets[x].pa; } int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i = 1; i <= maxn-10;i++){ sets[i].pa = i; } for(int i = 1; i <= n; ++i){ scanf("%d%d",&products[i].p,&products[i].d); } sort(products+1,products+1+n,cmp); int sum = 0; for(int i = 1; i <= n;i++){ int rootx = Find( products[i].d ); if(rootx <= 0){ continue; } sets[rootx].pa = rootx -1; sum += products[i].p; } printf("%d\n",sum); } return 0; }
POJ 1456——Supermarket——————【贪心+并查集优化】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4910610.html