鹤岗市网站建设_网站建设公司_MongoDB_seo优化
2026/1/18 21:59:51 网站建设 项目流程
点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N=3000;
const int M=7000*2;
int n,m,s,t;
struct Edge{int to;int w;int ne;
}edge[M];
int h[N];
int idx;
int dist[N];
bool st[N];int dijkstra()
{memset(dist,0x3f,sizeof dist);dist[s]=0;priority_queue<PII,vector<PII>,greater<PII>> heap;heap.push({dist[s],s});while(!heap.empty()){auto t=heap.top();heap.pop();auto ver=t.second;if(st[ver]) continue;st[ver]=true;for(int i=h[ver];i!=-1;i=edge[i].ne){int j=edge[i].to;if(dist[j]>dist[ver]+edge[i].w){dist[j]=dist[ver]+edge[i].w;heap.push({dist[j],j});}}}return dist[t];
}void add(int u,int v,int w)
{edge[idx].w=w;edge[idx].to=v;edge[idx].ne=h[u];h[u]=idx++;
}int main()
{ios::sync_with_stdio(0),cin.tie(0);cin>>n>>m>>s>>t;memset(h,-1,sizeof h);while(m--){int u,v,w;cin>>u>>v>>w;add(u,v,w);add(v,u,w);}cout<<dijkstra()<<endl;
}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询