*******优先队列********

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LMengi000/article/details/81543512

 

目录

 

优先队列用法介绍:

POJ  2431 Expedition

HDU 1285 确定比赛名次


优先队列用法介绍:

https://blog.csdn.net/annfan123/article/details/52201703

 //示例1
    priority_queue<int> qi;//普通的优先级队列,按从大到小排序,越小的整数优先级越低的优先队列

  //示例2
    priority_queue<int, vector<int>, greater<int> > qi2; //从小到大的优先级队列,越小的整数优先级越大,可将greater改为less,即为从大到小

  //示例3
    priority_queue<node> qn; //必须要重载运算符

**************************************************************************************************************************************

greater是升序排列,后面的大于前面的  1 2 3 4 5

less是降序排列,后面的小于前面的       5 4 3 2 1

在初始化优先级队列时默认是less

priority_queue<int,vector<int>,less<int> > que 与 priority_queue<int > que是一样的效果

也可以自己写比较函数

struct cmp{
    bool operator() ( int a , int b ){
return a< b;      //与greater是等价的
         }
};

priority_queue<int,vector<int>,cmp > p;

另外特别是在结构不是基本结构的比较时,最好自己写比较函数,比较速度能够提高不少

#include<iostream>
#include<string>
#include<queue>
#include<time.h>
#include<algorithm>
#include <functional>
using namespace std;
 
typedef pair<int,int>int2;
 
struct cmp{
    bool operator() ( int2 a, int2 b ){return a.first< b.first;}
};
int main()
{
	//priority_queue<int2,vector<int2>,greater<int2> > p1;
	priority_queue<int2,vector<int2>,cmp > p1;
	p1.push(int2(1,2));
	p1.push(int2(3,4));
	p1.push(int2(2,8));
	p1.push(int2(5,0));
	for(int i=0;i<4;i++)
	{
		int2 temp=p1.top();p1.pop();
		printf("(%d,%d)\n",temp.first,temp.second);
	}
	system("pause");
	return 0;
}

POJ  2431 Expedition

Description

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. 

Input

* 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

Output

* 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 Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

Hint

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

USACO 2005 U S Open Gold

题意:一卡车在总长度为L距离路行驶,有P单位的汽油,车每开一个单位就会消耗一个单位的汽油,途中有N个加油站,给出n个从加油站到终点的距离a  和 每个加油站可以加的油量b,油箱无限大,卡车是否能到达终点,如果可以,至少需要加多少次油?并输出最少的次数。

1.加油站的数量太多,需要采用更高效的方法。

2.求出起点到加油站的距离,并且讲加油站的位置从小到大排序,当车来到一个加油站,如果车里有油,就把这个加油站能加的油放进优先队列里,优先队列会把存进去的油从大到小排序,然后当车没有油的时候,就从队列中取出一个加油量最大的,给车加油。

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
const int maxn=1e6+10;
int n;
int l,p;
struct Node
{
	int a;
	int b;
	bool operator<(const Node & d) const
	{
		return a<d.a;
	}
}S[maxn];

void solve()
{
	S[n].a=l;
	S[n].b=0;
	n++;
	priority_queue<int>pq;
	int ans=0,pos=0,tank=p;
	for(int i=0;i<n;i++)
	{
		int d=S[i].a-pos;
		while(tank-d<0)
		{
			if(pq.empty())
			{
				puts("-1");
				return ;
			}
			tank+=pq.top();
			pq.pop();
			ans++;
		}
		tank-=d;
		pos=S[i].a;
        pq.push(S[i].b);
	}
	printf("%d\n",ans);
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&S[i].a,&S[i].b);
	}
	scanf("%d%d",&l,&p);
	for(int i=0;i<n;i++)
	{
		S[i].a=l-S[i].a;
	}
	sort(S,S+n);
	/*for(int i=0;i<n;i++)
	{
		printf("a=%d b=%d\n",S[i].a,S[i].b);
	}*/
	solve();
	return 0;
}

 注意:

1.排序是对加油站的位置排序,车每到达一个加油站,不加油就把油存放在优先队列中,优先队列把油从大到小排序。

2.

HDU 1285 确定比赛名次

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36939    Accepted Submission(s): 14431


 

Problem Description

有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。

 

Input

输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。

 

Output

给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。

 

Sample Input

 

4 3 1 2 2 3 4 3

 

Sample Output

 

1 2 4 3

 

Author

SmallBeer(CML)

 

Source

杭电ACM集训队训练赛(VII)

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  1233 2647 1232 3342 1811 

题目解析转:拓扑排序。 https://blog.csdn.net/LMengi000/article/details/82048190

用到了优先队列priority_queue<int,vector<int>,greater<int> >Q; 越小的整数优先级越大的优先队列

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int map[520][520];
int vis[520];
int n,m;
priority_queue<int,vector<int>,greater<int> >Q;//越小的整数优先级越大
void topo()
{
	for(int i=1;i<=n;i++)
	{
		if(vis[i]==0)//从入度为0的点开始,压入队列
		 Q.push(i);
	}
	int cnt=1;
	while(!Q.empty())
	{
		int v=Q.top();//优先队列里面存放的点从小到大,小的优先 ,取队头
		Q.pop();
		if(cnt!=n)
		{
			cout<<v<<" ";
			cnt++;
		}
		else
		{
			cout<<v<<endl;
		}
		for(int i=1;i<=n;i++)//寻找以点v为起点,与点v相连的点
		{
			if(!map[v][i])
			{
				continue;
			}
			vis[i]--;//因为v点已经输出了,i点的入度减少。
			if(!vis[i])  //入度为0的点,优先入队列 
			Q.push(i);
		}
	}
}
int main()
{
   while(scanf("%d%d",&n,&m)!=EOF)
   {
        int u,v;
     memset(map,0,sizeof(map));
     memset(vis,0,sizeof(vis));
     for(int i=0;i<m;i++)
     {
   	   scanf("%d%d",&u,&v);
   	    if(map[u][v])
   	     continue;
   	    map[u][v]=1;
   	    vis[v]++;//记录j点入度个数,拓扑就是要从入度为0的点开始 
			//printf("in[%d]=%d\n",j,in[j]);
     }
      topo();	
   } 
   return 0;
}

猜你喜欢

转载自blog.csdn.net/LMengi000/article/details/81543512