1 class Solution 2 { 3 struct cmp 4 { 5 bool operator ()(vector<int> &a, const vector<int> &b) 6 { 7 // < :大顶堆 8 // > :小顶堆 9 return a[0]+a[1] > b[0]+b[1]; 10 } 11 }; 12 public: 13 vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) 14 { 15 priority_queue<vector<int>, vector<vector<int>>, cmp> pq; 16 for(auto n1:nums1) 17 { 18 for(auto n2:nums2) 19 { 20 pq.push({{n1,n2}}); 21 } 22 } 23 while(!pq.empty()) 24 { 25 cout << (pq.top())[0] << " " << (pq.top())[1] << endl; 26 pq.pop(); 27 } 28 vector<vector<int>> res; 29 for(int i = 0; i < k; ++i) 30 { 31 if(pq.empty()) break; 32 res.push_back(pq.top()); 33 pq.pop(); 34 } 35 return res; 36 } 37 };