重
#include<cstdio> #include<vector> #include<queue> using namespace std; const int maxn = 100; const int INF = 1<<30; struct edge { int from,to; int value; edge(int a = 0,int b = 0,int c = 0):from(a),to(b),value(c){} }E[maxn]; vector<edge>G[maxn]; int dist[maxn]; int done[maxn]; int p[maxn]; struct cmp { bool operator () (const int a,const int b)const { return dist[a] > dist[b]; } }; priority_queue<int,vector<int>,cmp>Q; int main() { freopen("input.txt","r",stdin); int num_edge,num_V; memset(done,0,sizeof(done)); scanf("%d%d",&num_edge,&num_V); for(int i = 0;i < num_edge;i++) { scanf("%d%d%d",&E[i].from,&E[i].to,&E[i].value); G[E[i].from].push_back(E[i]);//保存的是边的编号 } //要从点s出发 int s; scanf("%d",&s); for(int i = 0;i < num_V;i++) { dist[i] = INF; } dist[s] = 0; Q.push(s); while(!Q.empty()) { int u = Q.top(); Q.pop(); if(done[u])continue; done[u] = 1; for(int i = 0; i < G[u].size();i++) { if(dist[G[u][i].to] > dist[u] + G[u][i].value) { dist[G[u][i].to] = dist[u] + G[u][i].value; Q.push(G[u][i].to); p[G[u][i].to] = u; } } } return 0; }
点在于priority_queue的实现