【POJ-1456】 Supermarket
作者:互联网
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int SIZE = 1 << 20;
vector<pair <int,int> > vec;
int fa[SIZE];
bool cmp(pair<int,int> a,pair<int,int> b) {
return a.first > b.first;
}
int get(int x) {
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
void merge(int x,int y) {
fa[get(x)] = get(y);
}
int main() {
int n;
while (cin >> n) {
int ans = 0, maxn = 0; vec.clear();
int a, b;
for (int i = 1; i <= n; ++i) {
cin >> a >> b;
vec.push_back(make_pair(a,b));
maxn = max(maxn, b);
}
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i <= maxn ; ++i) {
fa[i] = i;
}
for (int i = 0; i < n; ++i) {
int w = vec[i].first, v = vec[i].second;
int root = get(v);
if (root > 0) {
ans += w; merge(root,root-1);
}
}
cout << ans << endl;
}
return 0;
}
想飞的小菜鸡
发布了26 篇原创文章 · 获赞 3 · 访问量 2340
私信
关注
标签:return,get,int,Supermarket,fa,1456,POJ,vec,maxn 来源: https://blog.csdn.net/BX2359575459/article/details/104082745