您的位置:首页 > 其它

优先队列

2015-09-20 21:23 239 查看
知识点讲解详细的博客:http://www.cnblogs.com/heqinghui/archive/2013/07/30/3225407.html

优先级高的先出队列

模板题:HDU 1896 Stones

【优先队列用法】

1、普通

结果:从大到小输出 最大优先

priority_queue<int>qi;


2、传入一个比较函数,使用functional.h函数对象作为比较函数

<pre name="code" class="cpp">#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
.....
priority_queue<int,vector<int>,greater<int> >q1;//最小优先   ">>"会被判错,要用空格分开
priority_queue<int,vector<int>,less<int> >q2;//最大优先
....


3、自定义优先级

struct node
{
    friend bool operator< (node n1, node n2) // friend是友元函数   写成‘>’会判错
    {
        return n1.priority < n2.priority;//最小优先
		return n1.priority > n2.priority;//最大优先 
    }
    int priority;// 优先级 
    int value;//数值 
};
。。。。
priority_queue<node>q;



struct node
{
	int x,y,step;
};
struct cmp
{
	bool operator ()(const node a,const node b)
	{
		return a.step>b.step;//最小优先
		return a.step<a.step;//最大优先 
	}	
};
。。。。。
priority_queue<node,vector<node>,cmp>q;


【题目】

HDU 1434 幸福列车

HDU 1509 Windows Message Queue

HDU 1873 看病要排队

HDU 4006 The kth great number(维护大小是k的优先队列)

UVA 10954 (贪心+优先队列)

UVA 1203

UVA 136(这一题一开始我想到的是用set)

UVA 11997(这题感觉挺难的,详细做法见大白书P189)

HDU 1285 确定比赛名次(拓扑排序+优先队列)

HDU 4261 Estimation(dp+优先队列 这一题我是看百度题解的 ,用了双队列,如果知道更好的方法,就给我留一下言啊啊啊啊



UVA 11995(大白书P186)

UVA 12382

UVA 1316(这一题TLE了好多次,原本以为是代码不够完美,结果只要读入输出用cin cout就过了,用scanf printf就TLE了)

UVA 12266

(题意比较难懂,股票交易中,有人愿意抛售,有人愿意买,当愿意买的价格大于卖的价格,那么这个交易就可以进行,一直交易,知道交易不能进行,输出剩下的愿意卖的最小的价钱,愿意买的最大的价钱,以及上一次交易的成交额)

UVA 1153

HDU 5437 Alisha’s Party(题目中的t是要先算小的,再算大的,当t相等时,那么就算总数,如果直接sort会超时,可以开一个vis[ ]数组,记录第i个人到的时候,一共要让几个人进城堡,题目中最后的第n个进城堡这个n是一定存在的)

UVALive 4254(这题得好好消化,一开始并不会做)

UVA 757(贪心+优先队列)

【搜索+优先队列】

HDU 1242 Rescue

HDU 1428 漫步校园(最短路+dfs+优先队列)

UVA 10047

(较难,抓住一点,那就是下一步该怎么走,如果按原方向走,那么位置,时间以及颜色要改变,如果旋转,那么改变的就是方向,时间,开一个四维数组vis[x][y][direction][color],记录这个状态是否被访问过)

【最短路+优先队列】

HDU 2544 http://blog.163.com/jiongjiong_zier/blog/static/192279231201171934931645/

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <string> 
#include <functional>//优先队列传入一个比较函数时必须有这个头文件,否则就会ce
#include <cstring>
using namespace std;
const int N = 105;
const int INF = 0x1fffffff;
int map

,D
,flag
;
int n, m;
//普通的最短路,没有用优先队列优化的 
/*void dijkstra()
{
	memset(flag, 0, sizeof(flag));
	for(int i = 1; i <= n; i++)
		D[i] = map[1][i];
	for(int t = 1; t < n; t++)
	{
		int min = INF;
		int index = -1;
		for(int i = 1; i <= n; i++)//此处是找最短的一条路,可用优先队列优化 
		{
			if(flag[i]) continue;
			if(min > D[i])
			{
				min = D[i];
				index = i;
			}
		}
		flag[index] = 1;
		for(int i=1;i<=n;i++)
		{
			if(flag[i]) continue;
			if(D[i]>D[index]+map[i][index]) D[i] = D[index]+map[i][index];
		}
	}
}*/

//优化后的
typedef pair<int,int>node;//排序的时候先比较第一个 
void dijkstra()
{
	memset(flag,0,sizeof(flag));
	for(int i=1;i<=n;i++) D[i]=INF;
	D[1]=0;
	priority_queue<node,vector<node>,greater<node> >q;//最小优先 
	q.push(node(0,1));//或写成q.push(make_pair(0,1)) 
	while(!q.empty())
	{
		node t=q.top();
		q.pop();
		int now=t.second; 
		if(flag[now]) continue;
		flag[now]=1;
		for(int i=1;i<=n;i++)
		{
			if(flag[i]) continue;
			if(D[i]>D[now]+map[i][now])
			{
				D[i] = D[now]+map[i][now];
				q.push(node(D[i],i));
			}
		}
	}
}
int main()
{
	while(scanf("%d%d", &n, &m), n || m)
	{
		memset(map,0,sizeof(map));
		for(int i = 0; i < N; i++)
		{ 
			for(int j = 0; j < N; j++)
			{
			 	if(i==j) continue; 
				map[i][j] = INF;
			} 
		} 
		int x, y, len;
		for(int i = 0; i < m; i++)
		{
			scanf("%d %d %d", &x, &y, &len);
			map[x][y] = map[y][x] = len;
		}
		dijkstra();
		cout << D
 << endl;
	}
	return 0;
}


HDU 2066 一个人的旅行

HDU 2680 Choose the best route(反向建图,注意:这题是单向的,不是双向)

UVA 929(直接bfs会超时)

UVA 10986

【未做的】

UVA 12100

UVA 10588

UVA 1422 (二分+贪心+优先队列)

UVA 11134 (贪心+优先队列)

UVA 501

UVA 658

UVA 11374 (最短路+记录路径)

UVA 10537(最短路+记录路径 看了 没思路)

HDU 1053 Entropy(有关树的 未做)

+

POJ 1324(BFS+状压)

POJ 1338

POJ 1442

POJ 1511(最短路+优先队列)

POJ 1724(邻接表+优先队列+最短路)

POJ 1729(bfs+优先队列)

POJ 1847(bfs+优先队列)

POJ 1862

POJ 2010

POJ 2312(bfs+优先队列)

POJ 2431(贪心+优先队列)

POJ 2442

POJ 2449(第k短路 A*)

POJ 2823

POJ 2970(贪心+优先队列)

POJ 3017

POJ 3159(差分约束 dij 优先队列)

POJ 3190 (区间贪心+优先队列)

POJ 3245

POJ 3253(哈夫曼树,堆,优先队列)

POJ 3306

POJ 3465(贪心+优先队列)

POJ 3614

POJ 3635(BFS+优先队列)

POJ 3687(反拓扑+优先队列)

ZOJ 1204

ZOJ 1319

ZOJ 1649

ZOJ 1671

ZOJ 2212

ZOJ 2724

ZOJ 2849

ZOJ 3632

codeforces 45C

codeforces 128B

codeforces 337B

codeforces 377B

codeforces 446B

codeforces 507E

codeforces 523D

codeforces 555B

HDU 2102

HDU 4544

FZU 1182

FZU 1894
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: