一:时间复杂度为O(V*V)的Dijkstra
const int Max_v = 100 + 10; const int INF = 1<<30; int cost[Max_v][Max_v];//权值 int d[Max_v];//顶点s出发最短距离 bool used[Max_v];//以使用过的图 int V;//顶点数 int Edge;//边数 void dijkstra(int s) { fill(d,d+V,INF); fill(used,used+V,false); d[s] = 0; while(true) { int v = -1; for(int u = 0; u<V; u++) { if(!used[u] && (v == -1 || d[u] < d[v])) v = u; } if(v == -1) break; used[v] = true; for(int u = 0; u <V; u++) { d[u] = min(d[u],d[v] + cost[v][u]); } } printf("%d\n",d[V-1]); }
const int maxn = 1000 + 10; const int INF = 1<<30; typedef pair<int, int> P; int N, R; struct edge { int to, cost; }; vector<edge> G[maxn]; int d[maxn]; //最短路径 void solve() { priority_queue<P, vector<P>, greater<P> > que; fill(d, d+N, INF); //最短距离 d[0] = 0; que.push(P(0,0)); while(!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int 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)); } } } printf("%d\n",d[N-1]); }
Description
Input
Output
Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
Sample Output
3 2
第一种AC代码
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int Max_v = 100 + 10; const int INF = 1<<30; int cost[Max_v][Max_v];//权值 int d[Max_v];//顶点s出发最短距离 bool used[Max_v];//以使用过的图 int V;//顶点数 int Edge;//边数 void dijkstra(int s) { fill(d,d+V,INF); fill(used,used+V,false); d[s] = 0; while(true) { int v = -1; for(int u = 0; u<V; u++) { if(!used[u] && (v == -1 || d[u] < d[v])) v = u; } if(v == -1) break; used[v] = true; for(int u = 0; u <V; u++) { d[u] = min(d[u],d[v] + cost[v][u]); } } printf("%d\n",d[V-1]); } int main() { #ifdef xxz freopen("in.txt","r",stdin); #endif // xxz int Case; while(scanf("%d%d",&V,&Edge) != EOF) { if(V == 0 && Edge == 0) break; for(int i = 0; i < Max_v; i++) fill(cost[i],cost[i]+Max_v,INF);//注意每次都要初始化权值,否则WA for(int i = 0 ; i < Edge; i++) { int u,v,value; scanf("%d%d%d",&u,&v,&value); u--,v--; cost[u][v] = cost[v][u] = value; } dijkstra(0); } return 0; }
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1000 + 10; const int INF = 1<<30; typedef pair<int, int> P; int N, R; struct edge { int to, cost; }; vector<edge> G[maxn]; int d[maxn]; //最短路径 void solve() { priority_queue<P, vector<P>, greater<P> > que; fill(d, d+N, INF); //最短距离 d[0] = 0; que.push(P(0,0)); while(!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int 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)); } } } printf("%d\n",d[N-1]); } int main() { int i, a, b, c; edge e; while(~scanf("%d%d",&N,&R)) { if(N == 0 && R == 0) break; for(i = 0; i < maxn; i++) G[i].clear(); for(i=0; i<R; ++i) { scanf("%d%d%d", &a, &b, &c); a--,b--; e.to = b, e.cost = c; G[a].push_back(e); e.to = a; G[b].push_back(e); } solve(); } return 0; }
原文:http://blog.csdn.net/u013445530/article/details/43711873