标签:
题目:click here
题意:给定一个无向图,有T条边,N个顶点,问从1到n的最短路。
下面给出3种dijkstra算法分别用邻接矩阵,邻接表,和堆优化后的代码。
邻接表堆优化:O(ElogV)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 #define F first 7 #define S second 8 #define pb push_back 9 using namespace std; 10 typedef pair<int,int> P; // F是最短距离,S是顶点编号 11 const int INF = 1e9+7; 12 const int M = 5e3+3; 13 14 struct edge { 15 int to, cost; 16 }; 17 int t, n; 18 vector<edge> G[M]; // 邻接表记录无向图 19 int dist[M]; // dist[i]表示从1到i的最短距离 20 21 void dijkstra( int s ) { // digkstra算法 复杂度O(|E|log|v|) 22 priority_queue<P, vector<P>, greater<P> > que; // 按照F从小到大的顺序取出值 23 fill( dist, dist+n+1, INF ); 24 dist[s] = 0; 25 que.push( P(0,s) ); 26 while( !que.empty() ) { 27 P p = que.top(); que.pop(); 28 int v = p.S; 29 if( dist[v] < p.first ) continue; 30 for( int i=0; i<G[v].size(); i++ ) { 31 edge e = G[v][i]; 32 if( dist[e.to] > dist[v]+e.cost ) { 33 dist[e.to] = dist[v] + e.cost; 34 que.push( P(dist[e.to], e.to) ); 35 } 36 } 37 } 38 } 39 40 void solve() { 41 for( int i=0; i<t; i++ ) { 42 int f, t, c; 43 scanf("%d%d%d", &f, &t, &c ); 44 G[f].pb( (edge){t,c} ); 45 G[t].pb( (edge){f,c} ); 46 } 47 dijkstra( n ); 48 printf("%d\n", dist[1] ); 49 } 50 51 int main() { 52 while( ~scanf("%d%d", &t, &n ) ) { 53 solve(); 54 } 55 return 0; 56 }
邻接矩阵1:O(V2)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 const int M = 5e3+3; 8 9 int cost[M][M]; 10 int t, n; 11 int d[M]; 12 bool vis[M]; 13 14 void bijkstra( int s ) { 15 fill( d, d+n+1, INF ); 16 fill( vis, vis+n+1, false ); 17 d[s] = 0; 18 while( true ) { 19 int v = -1; 20 for( int u=1; u<=n; u++ ) 21 if( !vis[u] && (v==-1 || d[u]<d[v] ) ) v = u; 22 if( v == -1 ) break; 23 vis[v] = true; 24 for( int u=1; u<=n; u++ ) 25 if( cost[v][u] != INF ) 26 d[u] = min( d[u], d[v]+cost[v][u] ); 27 } 28 } 29 30 void solve() { 31 for( int i=1; i<=n; i++ ) { 32 for( int j=1;j<=n; j++ ) 33 cost[i][j] = INF; 34 cost[i][i] = 0; 35 } 36 for( int i=0; i<t; i++ ) { 37 int f, t, c; 38 scanf("%d%d%d", &f, &t, &c ); 39 if( cost[f][t] > c ) { 40 cost[f][t] = cost[t][f] = c; 41 } 42 } 43 bijkstra( 1 ); 44 printf("%d\n", d[n] ); 45 } 46 47 int main() { 48 while( ~scanf("%d%d", &t, &n ) ) { 49 solve(); 50 } 51 return 0; 52 }
邻接矩阵2:O(V2)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 const int M = 1e3+3; 8 9 int cost[M][M]; 10 int t, n; 11 bool vis[M]; 12 13 void bijkstra( int s ) { 14 memset( vis, false, sizeof(vis) ); 15 vis[1] = true; 16 int tm = s-1; 17 while( tm-- ) { 18 int k = 1, minn = INF; 19 for( int j=1; j<=n; j++ ) { 20 if( !vis[j] && minn > cost[1][j] ) { 21 minn = cost[1][j]; 22 k = j; 23 } 24 } 25 vis[k] = true; 26 for( int j=1; j<=n; j++ ) { 27 if( !vis[j] && cost[1][j] > cost[1][k]+cost[k][j] ) 28 cost[1][j] = cost[1][k] + cost[k][j]; 29 } 30 } 31 } 32 void solve() { 33 for( int i=1; i<=n; i++ ) { 34 for( int j=1;j<=n; j++ ) 35 cost[i][j] = INF; 36 cost[i][i] = 0; 37 } 38 for( int i=0; i<t; i++ ) { 39 int f, t, c; 40 scanf("%d%d%d", &f, &t, &c ); 41 if( cost[f][t] > c ) { 42 cost[f][t] = cost[t][f] = c; 43 } 44 } 45 bijkstra( n ); 46 printf("%d\n", cost[1][n] ); 47 } 48 49 int main() { 50 while( ~scanf("%d%d", &t, &n ) ) { 51 solve(); 52 } 53 return 0; 54 }
POJ 2387 Til the Cows Come Home
标签:
原文地址:http://www.cnblogs.com/TaoTaoCome/p/4740421.html