#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int , int > P;
struct dege{int to,cost;};
vector <dege> G[MAX];
const int MAX = 101;
const int INF = 9999999;
int cost[MAX][MAX];//边e=(u,v)的权值
int d[MAX];
bool used[MAX];
int v;//顶点数
void Dijkstra1(int s){
fill(d,d+v,INF);
fill(used,used+v,false);
d[s] = 0;
while(true){
int vs = -1;
for(int u = 0;u<v;u++)
if(!used[u] && (vs==-1||d[u]<d[vs])) vs=u;
if(vs == -1) break;
used[vs] = true;
for(int u = 0;u<v;u++)
d[u] = min(d[u],d[vs]+cost[vs][u]);
}
}
void Dijkstra2(){
priority_queue<P,vector<p>,greater<p>> que;
fill(d,d+v,INF);
d[s] = 0;
que.push(P(0,s));
while(!que.empty()){
P p = que.top();que.pop();
int vs = p.second;
if(d[vs]<p.first) continue;
for(int i = 0;i<G[vs].size();i++){
dege e = G[vs][i];
if(d[e.to]>d[vs] + e.cost){
d[e.to] = d[vs] + e.cost;
que.push(P(d[e.to]),e.to);
}
}
}
}
int main()
{
int n,m;
cin>>v;
while(v--){
cin>>n>>m;
cin>>cost[n][m];
}
Dijkstra1(0);
for(int i=0;i<v;i++)
cout<<d[i]<<' ';
return 0;
}
单源最短路经
最新推荐文章于 2022-12-28 22:18:21 发布