别人给的模板,所以不知道链接
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
#define MAXN 1020
#define INF 0x3f
typedef long long ll;
// O(nlogn)
typedef pair<ll, ll> P; //first是到源点最短距离,second是顶点的编号
struct edge { ll to, cost;
edge (ll too, ll costt) { to = too; cost = costt; }
};//指向顶点to的权值为cost的边
vector <edge> G[MAXN];//存图 可以理解为二维的,每个G包含若干项,这些项是以当前编号点为起点, 若干项边结构体(edge)的v为终点,cost为权值的边
void addedge (ll u, ll v, ll w) {
G[u].push_back (edge (v, w)); //添加从u到v权值为w的边
}
ll ma[MAXN][MAXN];
ll kl[MAXN];
ll pre[MAXN];//记录前驱节点
ll d[MAXN];//最短距离
ll n, m, s ;//顶点数 边数 起点
vector<ll> path;//路径
void dijkstra (ll s) {
priority_queue<P, vector<P>, greater<P> > que;
fill (d, d + n + 1, INF); //fill (pre, pre + n + 1, -1);
d[s] = 0; que.push (P (0, s));
while (!que.empty()) {//核心
P p = que.top(); que.pop();
ll v = p.second;
if (d[v] < p.first) continue;
for (ll i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push (P (d[e.to], e.to));
pre[e.to]=v;
}
}
}
while(!que.empty())que.pop();
}