知识要点:优先队列、dijkstra优化版
优先队列
优先队列也被称为堆(堆分为大根堆和小根堆)。
优先队列默认为大根堆,我们每插入一个元素,他就会将这个元素进行比大小,把他插入到相应的位置里。优先队列比较时也会使用cmp函数,函数返回代表排在前面,返回代表排在前面(和数组不一样,优先队列是反过来的),所以小根堆我们应该使用大于号,大根堆使用小于号。
优先队列的格式如下:
priority_queue<类型>q; //默认为大根堆,那么堆顶为最大值
priority_queue<类型,vector<类型>,greater<类型>>q; //现在为小根堆,那么堆顶为最小值因为优先队列会对存储进去的所有元素进行排序,而优先队列是不知道结构体应该怎么比较的,因此我们需要告诉优先队列怎么比较两个结构体。
在这里因为优先队列不能接受函数,所以我们要重写定义一个小于号,告诉计算机怎么使用小于号来比较两个结构体。
struct node{
int v, w;
bool operator<(const node& x)const{
return w < x.w;
}//大根堆使用小于号
};因为每次插入都需要比较,所以优先队列的时间复杂度:
以下是具体操作:
- 获取堆顶:
q.top();(不像其他队列一样是使用front) - 删除堆顶:
q.pop(); - 获取堆中元素个数:
q.size(); - 插入元素:
q.push(x);
dijkstra优化
每一次将所有起点能够到达的节点放入优先队列,然后找出其中到达起点最近的节点,让他来充当中间节点,但是我们可以发现,每一个节点的最短距离可能被更新多次。
那么一个节点可能多次重新入队,而每个节点只应该充当一次中间节点。我们可以使用一个标记数组。
题目解析+总结
P1144 最短路计数
题目解析
从起点出发到达终点的所有路径,一定会包含在所有的中间节点中。当我们枚举从起点出发到达一个中间节点最终到达终点,只要我们枚举了所有的中间节点,那么这里面一定包含了所有的最短路径,假设我们从起点到达中间节点,再从中间节点到达终点,应该等于从起点到达的最短路径数量 + 到的最短路径数量。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,s,e1,e2,dist[1000005],cnt[1000005];
struct node{
int to,w;
};vector<node>G[1000005];
bool vis[1000005];
void dijk(){
for(int i=1;i<=n;++i) dist[i] = 1e9,vis[i] = 0,cnt[i] = 0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>Q;
Q.push({0,s}),dist[s] = 0,cnt[s] = 1;
while(Q.size()){
int top = Q.top().second; Q.pop();
if(vis[top]) continue;
vis[top] = 1;
for(auto x:G[top]){
int v = x.to,w = x.w;
if(dist[v] > dist[top] + w){
dist[v] = dist[top] + w;
Q.push({dist[v],v});
cnt[v] = cnt[top] % 100003;
}
else if(dist[v] == dist[top] + w){
cnt[v] = (cnt[v] + cnt[top]) % 100003;
Q.push({dist[v],v});
}
}
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m; s = 1;
while(m--){
int u,v;
cin>>u>>v;
G[u].push_back({v,1});
G[v].push_back({u,1});
}
dijk();
for(int i=1;i<=n;++i) cout<<cnt[i]<<endl;
return 0;
} homework~
P2951 [USACO09OPEN] Hide and Seek S
AC代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e4 + 7;
int n,m,s;
struct sstt{
int x,id;
}dist[N];
struct node{
int to,w;
};vector<node>G[N];
bool vis[N];
void dijk(){
memset(dist,0x3f,sizeof dist);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>Q;
Q.push({0,s}),dist[s].x = 0;
while(Q.size()){
int top = Q.top().second; Q.pop();
if(vis[top]) continue;
vis[top] = 1;
for(auto x:G[top]){
int v = x.to,w = x.w;
if(dist[v].x > dist[top].x + w){
dist[v].x = dist[top].x + w;
Q.push({dist[v].x,v});
}
}
}
}
bool cmp(sstt a,sstt b){
if(a.x == b.x) return a.id < b.id;
return a.x > b.x;
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m; s = 1;
while(m--){
int u,v;
cin>>u>>v;
G[u].push_back({v,1});
G[v].push_back({u,1});
}
dijk();
for(int i=1;i<=n;++i) dist[i].id = i;
sort(dist+1,dist+1+n,cmp);
int cnt = 0;
for(int i=1;i<=n;++i){
cnt++;
if(dist[i].x != dist[i+1].x) break;
}
cout<<dist[1].id<<' '<<dist[1].x<<' '<<cnt<<endl;
return 0;
} P2984 [USACO10FEB] Chocolate Giving S
AC代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e4 + 7;
int n,m,s;
int dist[N];
struct node{
int to,w;
};vector<node>G[N];
bool vis[N];
void dijk(){
memset(dist,0x3f,sizeof dist);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>Q;
Q.push({0,s}),dist[s] = 0;
while(Q.size()){
int top = Q.top().second; Q.pop();
if(vis[top]) continue;
vis[top] = 1;
for(auto x:G[top]){
int v = x.to,w = x.w;
if(dist[v] > dist[top] + w){
dist[v] = dist[top] + w;
Q.push({dist[v],v});
}
}
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int k;cin>>n>>m>>k; s = 1;
while(m--){
int u,v,w;
cin>>u>>v>>w;
G[u].push_back({v,w});
G[v].push_back({u,w});
}
dijk();
while(k--){
int x,y; cin>>x>>y;
cout<<dist[x] + dist[y]<<endl;
}
return 0;
} P6770 [USACO05MAR] Checking an Alibi 不在场的证明
AC代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e4 + 7;
int f,p,c,m,s;
int dist[N];
struct node{
int to,w;
};vector<node>G[N];
bool vis[N];
void dijk(){
memset(dist,0x3f,sizeof dist);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>Q;
Q.push({0,s}),dist[s] = 0;
while(Q.size()){
int top = Q.top().second; Q.pop();
if(vis[top]) continue;
vis[top] = 1;
for(auto x:G[top]){
int v = x.to,w = x.w;
if(dist[v] > dist[top] + w){
dist[v] = dist[top] + w;
Q.push({dist[v],v});
}
}
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>f>>p>>c>>m; s = 1;
while(p--){
int u,v,w;
cin>>u>>v>>w;
G[u].push_back({v,w});
G[v].push_back({u,w});
}
dijk();
vector<int>op;
for(int i=1;i<=c;++i){
int x; cin>>x;
if(dist[x] <= m) op.push_back(i);
}
sort(op.begin(),op.end());
cout<<op.size()<<endl;
for(auto x:op) cout<<x<<endl;
return 0;
}