STL复习笔记(5)——优先队列(+dijkstra优化)

头文件

#include<queue>

声明:

priority_queue<T> pq;
priority_queue<T, vector<T>, cmp> pq;

第一种是默认的,第二种则自己定义,第一个参数是数据类型,第二个参数是储存结构,第三则是优先顺序,默认是less

我们可以改为greater<T>

priority_queue<T, vector<T>, greater<T>> pq;

当然,也可以在定义类的时候就顺便把顺序给定义了:

class hahaha
{
    int locate;
    char name[20];
    bool operator < (hahaha &a) const
    {
        return locate > a.locate;
    }
};

顺便贴一个dijkstra的优先队列优化

#include<bits/stdc++.h>
using namespace std;
#define INF 1<<27
int d[100];
int n;
struct node{
	int x;
	int d;
	node(int a,int b){x=a;d=b;};
	bool operator < (const node & a) const{
		return d>a.d;
	}
};
vector<node> mp[100];

void Dijkstra(int s){
	for(int i=0;i<=n;i++) d[i]=INF;
	d[s]=0;
	priority_queue<node> q;   
	q.push(node(s,d[s]));
	while(!q.empty()){
		node now = q.top();
		q.pop();
		for(int i=0;i<mp[now.x].size();i++){
			node next = mp[now.x][i];
			if(d[next.x]>now.d+next.d){
				d[next.x]=now.d+next.d;
				q.push(node(next.x,d[next.x]));
			}
		}
	}
}

int main(){
	int a,b,dis,m;    
    while(scanf("%d%d",&n,&m),n+m)    
    {    
        for(int i=0;i<=n;i++) mp[i].clear();    
        while(m--)    
        {    
            scanf("%d%d%d",&a,&b,&dis);    
            mp[a].push_back(node(b,dis));    
            mp[b].push_back(node(a,dis));    
        }    
        Dijkstra(1);    
        cout<<d[n]<<endl;  
    }    
    return 0;    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C语言的优先队列库来实现dijkstra算法,例如STL中的priority_queue或者自己实现一个堆来作为优先队列。以下是使用STL中的priority_queue实现dijkstra算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <limits.h> #include <queue> #define MAXN 1000 using namespace std; typedef struct Edge { int to, w; struct Edge *next; } Edge; Edge *head[MAXN]; int dist[MAXN]; bool vis[MAXN]; void add_edge(int u, int v, int w) { Edge *e = (Edge*)malloc(sizeof(Edge)); e->to = v; e->w = w; e->next = head[u]; head[u] = e; } void dijkstra(int s, int n) { memset(dist, 0x3f, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; pq.push(make_pair(0, s)); while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (vis[u]) continue; vis[u] = true; for (Edge *e = head[u]; e != NULL; e = e->next) { int v = e->to, w = e->w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push(make_pair(dist[v], v)); } } } } int main() { int n, m, s; scanf("%d%d%d", &n, &m, &s); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w); } dijkstra(s, n); for (int i = 1; i <= n; i++) { printf("%d ", dist[i]); } printf("\n"); return 0; } ``` 这个代码实现了使用优先队列实现dijkstra算法,其中priority_queue是STL中的优先队列,用于存储节点到源点的距离和节点编号的pair,按照距离从小到大排序。在每次取出队首元素时,如果该节点已经被访问过,则跳过,否则将其标记为已访问,并遍历其所有出边,更新到其它节点的距离。如果某个节点的距离被更新,则将其加入优先队列中,以便下一次取出时能够得到最小的距离。最终输出源点到所有节点的最短距离。 lua closure factory 完整代码: ```lua function new_counter() local count = 0 return function() count = count + 1 return count end end local counter1 = new_counter() local counter2 = new_counter() print(counter1()) -- 1 print(counter1()) -- 2 print(counter2()) -- 1 print(counter2()) -- 2 ``` 这段代码实现了一个闭包工厂,用于创建计数器函数。每次调用new_counter函数都会返回一个新的计数器函数,该函数内部维护一个计数器变量count,并在每次调用时将其加1并返回。由于每个计数器函数都有自己的count变量,因此可以实现多个计数器同时存在而不会互相干扰。在示例代码中,创建了两个计数器counter1和counter2,并分别调用它们两次,输出结果为1、2、1、2。 中文加密: 中文加密是指将中文文本转换为一串密文,以保护文本的机密性。常见的中文加密算法包括DES、AES、RSA等。其中DES是一种对称加密算法,AES和RSA是一种非对称加密算法。 对称加密算法指加密和解密使用相同的密钥,因此密钥的安全性非常重要。DES算法使用56位的密钥,将明文分成64位的块,经过16轮加密后得到密文。AES算法使用128位、192位或256位的密钥,将明文分成128位的块,经过多轮加密后得到密文。对称加密算法的优点是加密速度快,缺点是密钥的管理比较困难。 非对称加密算法指加密和解密使用不同的密钥,其中一个密钥称为公钥,另一个密钥称为私钥。RSA算法使用两个大素数作为私钥,将其乘积作为公钥,将明文转换为一个整数后进行加密,得到密文后再用私钥解密。非对称加密算法的优点是密钥管理方便,缺点是加密速度慢。 中文加密算法的选择应该根据具体的需求和场景来决定,需要综合考虑加密强度、加密速度、密钥管理等因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值