跳到正文

算法·理论

迪杰斯特拉(Dijkstra)

xiesean约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月28日
收录
2026年8月28日
更新
2026年8月28日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

双倍经验!!!

题目传送门(简单版)

题目传送门(普通版)

简单版

C++
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
struct node{
    vector<pii> e;
    int b=0;
    int d=INT_MAX;
};
node v[10005];
int n,m,s;
int u,v1,w;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m>>s;
    for (int i=1;i<=m;i++){
        cin>>u>>v1>>w;
        v[u].e.push_back({v1,w});
    }
    v[s].d=0;
    for (int i=1;i<=n;i++){
        int p=0;
        for (int j=1;j<=n;j++){
            (!v[j].b&&(!p||v[j].d<v[p].d))&&(p=j);
        }
        if(v[p].d==INT_MAX){
            break;
        }
        v[p].b=1;
        for (auto x:v[p].e){
            v[x.first].d=min(v[x.first].d,v[p].d+x.second);
        }
    }
    for(int i=1;i<=n;i++){
        cout<<v[i].d<<" ";
    }
    return 0;
}

普通版

C++
#include<bits/stdc++.h>
using namespace std;
typedef pair<long long,long long> pll;
struct node{
    vector<pll> e;
    int b=0;
    int d=-1;
};
priority_queue<pll,vector<pll>,greater<pll>> h;
node v[100005];
long long n,m,s;
long long u,v1,w;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m>>s;
    for (int i=1;i<=m;i++){
        cin>>u>>v1>>v[0].d;
        v[u].e.push_back({v[0].d,v1});
    }
    h.push({0,s});
    while (!h.empty()){
        pll p=h.top();
        h.pop();
        if(v[p.second].d==-1){
            v[p.second].d=p.first;
            for (pll x:v[p.second].e){
                h.push({p.first+x.first,x.second});
            }
        }
    }
    for(int i=1;i<=n;i++){
        cout<<v[i].d<<" ";
    }
    return 0;
}

codeforces

题目传送门codeforces

题目传送门洛谷

C++
#include<bits/stdc++.h>
using namespace std;
const long long MAX=LONG_LONG_MAX;
struct node{
	long long x,w;
	bool operator<(const node& a)const{
		return x>a.x;
	}
};
long long n,m;
long long dp[100005],vis[100005];
long long p[100005];
vector<long long> g[100005];
vector<long long> e[100005];
void bfs(){
	priority_queue<node> q;
	for(int i=1;i<=n;i++){
	    dp[i]=MAX;
	}
	dp[1]=0;
	q.push({0,1});
	while(q.size()) {
		node f=q.top();
		q.pop();
		long long w=f.w;
		if(vis[w]){
		    continue;
		}
		vis[w]=1;
		for(int i=0;i<e[w].size();i++) {
			long long x=e[w][i];
			if(dp[x]>dp[w]+g[w][i]) {
				dp[x]=dp[w]+g[w][i];
				p[x]=w;
				q.push({dp[x],x});
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=0;i<m;i++){
	    long long a,b,c;
	    cin>>a>>b>>c;
	    e[a].push_back(b);
		g[a].push_back(c);
		e[b].push_back(a);
		g[b].push_back(c);
	}
	bfs();
	if(dp[n]==MAX){
	    cout<<"-1"<<endl;
	    return 0;
	}
	e[1].clear();
	e[1].push_back(n);
	while(p[n]!=1){
	    n=p[n];
	    e[1].push_back(n);
	}
	e[1].push_back(1);
    for(int i=e[1].size()-1;i>0;i--){
        cout<<e[1][i]<<" ";
    }
    cout<<e[1][0];
	return 0;
} 

题目传送门

C++
#include<bits/stdc++.h>
using namespace std;
typedef pair<long long,long long> pll;
priority_queue<pll,vector<pll>,greater<pll>> h;
long long a[3],d[100005],n,ans;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>a[0]>>a[1]>>a[2];
    n--;
    fill(d,d+a[0],-1);
    h.push({0,0});
    while(h.empty()!=1){
        pll p=h.top();
        h.pop();
        if(d[p.second]==-1){
            d[p.second]=p.first;
            h.push({p.first+a[1],(p.second+a[1])%a[0]});
            h.push({p.first+a[2],(p.second+a[2])%a[0]});
        }
    }
    for (int i=0;i<a[0];i++){
        (d[i]!=-1&&n>=d[i])&&(ans+=(n-d[i])/a[0]+1);
    }
    cout<<ans;
    return 0;
}
原文