POJ2431 優先隊列+貪心 - biaobiao88

如下代碼可對結構體數組中的元素進行排序,也差很少算是一個小小的模板了吧node

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	int x;
	int y;
	bool operator<(const node &a) const//此操做是對操做符"<"進行重構 
	{
		return x < a.x;//對結構體數組x進行從大到小排序 
//		return y > a.y;//對結構體數組y進行從大到小排序 
	}
}s[100];

//bool cmp(int x,int y)//這個重構函數不能用在結構體數組中 
//{
//	return x > y;
//}

int main()
{
	int n;
	cin >> n;
	for(int i = 0;i < n;i++)
		cin >> s[i].x >> s[i].y;
	
	sort(s,s+n);
	for(int i = 0;i < n;i++)
		cout << s[i].x << " " << s[i].y << endl;
	cout << endl;
	return 0;
}

運行結果:ios

 

 

 也能夠這樣數組

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int x;
    int y;
    bool operator<(const node &a) const 
    {
        return x > a.x; 
    }
}s[100];

bool cmp(node m,node n)
{
    m.x < n.x;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0;i < n;i++)
        cin >> s[i].x >> s[i].y;
    
    sort(s,s+n);
    for(int i = 0;i < n;i++)
        cout << s[i].x << " " << s[i].y << endl;
    cout << endl;
    return 0;
}

對優先隊列的應用,POJ2431是一個很好的題目,此題用了優先隊列+貪心函數

Expedition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29720   Accepted: 8212

Descriptionui

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

Inputthis

* Line 1: A single integer, N

* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.

* Line N+2: Two space-separated integers, L and P

Outputspa

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Inputrest

4
4 4
5 2
11 5
15 10
25 10

Sample Outputcode

2

Hintblog

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

Source

思路:貪心思路爲,在假設汽車一路向前,在途中有通過加油站的話,把加油站進行排序,符合條件的加油站中的汽油從大到小排序,存到優先隊列中,在汽車用完時,即隨時能夠在隊列的隊頭取汽油,使得汽車儘量加最多的汽油,停最少次
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int dis;
    int fuel;
    bool operator<(const node &a) const//此重構操做,請參考上面的代碼 
    {
        return dis > a.dis;//返回dis,說明是對dis這個數組進行排序操做 
    }
}stop[10005];

priority_queue<int> que;

int main()
{
    int n,l,p;
    cin >> n;
    for(int i = 0;i < n;i++)
        cin >> stop[i].dis >> stop[i].fuel;
    cin >> l >> p;
    int ans = 0;
    sort(stop,stop + n);//對加油站距離dis數組進行從大到小排序 
    que.push(p);//隊列自動從大到小排序,即排序汽油p 
    int temp = 0;
    while(l > 0 && !que.empty())//卡車未到達終點而且卡車在當前汽油用完前有路過加油站 
    {
        ans++;//卡車停下加一次油計數器 
        l -= que.top();//加油,更新一次汽油用盡後距離終點的距離 
        que.pop();//刪除已用的加油站的汽油 
        while(l <= stop[temp].dis && temp < n)//卡車距離終點的距離小於等於最近加油站的距離而且這個加油站的位置在終點加油站前面,這裏假設終點也爲一個加油站。//l <= stop[i].dis意思是卡車能通過離它最近的一個加油站,若是大於的話,說明卡車停下時沒有加油站可加油  
            que.push(stop[temp++].fuel);//將通過的加油站壓入優先隊列,要使用的時候就取隊頭元素(隊頭中存的汽油最大) //若是通過加油站,則必定要將該加油站的可加油量添加到優先隊列當中 
    }//temp++說明離卡車最近的加油站,卡車繼續往前開,加油站點也依次日後,因此變量temp須要自增
    if(l > 0)//若是卡車距離終點的距離還大於0的話,即經過不了終點 
        cout << "-1" << endl;
    else
        cout << ans - 1 << endl;//在起點深度時候記爲一次加油,這裏須要減去1 
    return 0;
}

其中代碼有詳細的註釋,但願註釋能加深理解

代碼參考於:博客園-小小菜鳥

相關文章
相關標籤/搜索