Dijkstra+heap

#include #include #include using namespace std; #define INF 0x3f3f3f const int maxn = 101; int g[maxn][maxn]; int vis[maxn]; int dis[maxn]; struct node { int t, w; node( int tt, int ww) : t(tt), w(ww){} bool operator < (const node & o) const {return w > o.w;} }; vector edges[maxn]; void init(int E) { for(int i=0; i Q; Q.push(node(src, 0)); while(!Q.empty()) { node curNode = Q.top(); Q.pop(); int u = curNode.t; vis[u] = 1; for(int i=0; i dis[u] + g[u][v]) { dis[v] = dis[u] + g[u][v]; Q.push(node(v, dis[v])); } } while(!Q.empty() && vis[Q.top().t]==1) Q.pop(); if(Q.empty()) break; } } int main() { int V, E; while(scanf("%d%d", &V, &E)!=EOF) { if(V==0) break; init(E); int f, t, w; for(int i=0; i

 

你可能感兴趣的:(algorithm)