# 有权图的单源最短路算法 优先级队列优化dij

#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct node
{
int num,dis;
node(int nn,int dd):num(nn),dis(dd) {}
friend bool operator <(const node &a,const node &b)
{
if(a.dis==b.dis)
return a.num<b.num;
else
return a.dis>b.dis;
}
};
int N,R;
int dist[105];
int path[105];
const int IN = 1<<30;
vector<node> G[105];
priority_queue<node> q;
int MinDis;
int dij()
{
MinDis = IN;
for(int i=1; i<=N; i++)
dist[i] = IN;
dist[1] = 0;
q.push(node(1,0));
while(!q.empty())
{
node now = q.top();
q.pop();
for(int i=0; i<G[now.num].size(); i++)
{
node p = G[now.num][i];
if(now.dis + p.dis <dist[p.num])
{
path[p.num] = now.num;
dist[p.num] = now.dis + p.dis;
q.push(node(p.num,dist[p.num]));
}
}
}
}
int main()
{
cin >> N >> R;
int source,desin,dis;
while(R--)
{
cin >> source >> desin >> dis;
G[source].push_back(node(desin,dis));
G[desin].push_back(node(source,dis));
}
dij();
for(int i=1;i<=N;i++)
cout<<dist[i]<<endl;
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120