纯最短路。
1 ///HDU 2544堆优化的最短路 2 #include <cstdio> 3 #include <iostream> 4 #include <sstream> 5 #include <cmath> 6 #include <cstring> 7 #include <cstdlib> 8 #include <string> 9 #include <vector> 10 #include <map> 11 #include <set> 12 #include <queue> 13 #include <stack> 14 #include <algorithm> 15 using namespace std; 16 #define ll long long 17 #define _cle(m, a) memset(m, a, sizeof(m)) 18 #define repu(i, a, b) for(int i = a; i < b; i++) 19 #define repd(i, a, b) for(int i = b; i >= a; i--) 20 #define sfi(n) scanf("%d", &n) 21 #define pfi(n) printf("%d\n", n) 22 const int MAXN = 2200; 23 const int MAXM = 20200; 24 const int INF=0x3f3f3f3f; 25 struct Node 26 { 27 int to,next,w; 28 } edge[MAXM]; 29 struct HeapNode 30 { 31 int d, u; 32 bool operator < (const HeapNode& rhs) const 33 { 34 return d > rhs.d; 35 } 36 }; 37 struct Dijkstra 38 { 39 int head[MAXN],d[MAXN]; 40 bool done[MAXN]; 41 int cnt; 42 void init() 43 { 44 memset(head,-1,sizeof(head)); 45 cnt = 0; 46 } 47 void AddEdge(int u, int v, int w) 48 { 49 edge[cnt].to=v,edge[cnt].next=head[u]; 50 edge[cnt].w=w,head[u]=cnt++; 51 edge[cnt].to=u,edge[cnt].next=head[v]; 52 edge[cnt].w=w,head[v]=cnt++; 53 } 54 void dijkstra(int s,int n) 55 { 56 priority_queue<HeapNode> Q; 57 for(int i = s; i <= n; i++) 58 d[i] = INF; 59 d[s] = 0; 60 memset(done, 0, sizeof(done)); 61 Q.push((HeapNode) 62 { 63 0, s 64 }); 65 while(!Q.empty()) 66 { 67 HeapNode x = Q.top(); 68 Q.pop(); 69 int u = x.u; 70 if(done[u]) continue; 71 done[u] = true; 72 for(int i=head[u]; i!=-1; i=edge[i].next) 73 { 74 int v=edge[i].to; 75 if(d[v] > d[u] + edge[i].w) 76 { 77 d[v] = d[u] + edge[i].w; 78 Q.push((HeapNode) 79 { 80 d[v], v 81 }); 82 } 83 } 84 } 85 } 86 } dij; 87 int main() 88 { 89 int n,m,a,b,c; 90 while(scanf("%d%d",&n,&m),n+m) 91 { 92 dij.init(); 93 repu(i,0,m) 94 { 95 scanf("%d%d%d",&a,&b,&c); 96 dij.AddEdge(a,b,c); 97 dij.AddEdge(b,a,c); 98 } 99 dij.dijkstra(1,n); 100 printf("%d\n",dij.d[n]); 101 } 102 return 0; 103 }
堆优化的Dij
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <cstring> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 #include <set> 11 #include <map> 12 using namespace std; 13 const int maxn=2200; 14 const int INF=0x3f3f3f3f; 15 struct Edge 16 { 17 int u, v, d; 18 Edge(int u, int v, int d):u(u), v(v), d(d) {} 19 }; 20 struct qnode 21 { 22 int u,d; 23 qnode(int u, int d):u(u), d(d) {} 24 bool operator < (const qnode a)const 25 { 26 return d>a.d; 27 } 28 }; 29 struct Dijkstra 30 { 31 int n; 32 vector<int> G[maxn]; 33 vector<Edge> edge; 34 int d[maxn]; 35 bool vis[maxn]; 36 void init(int n) 37 { 38 this->n = n; 39 for(int i=0; i<=n; i++) 40 { 41 G[i].clear(); 42 vis[i]=0; 43 d[i]=INF; 44 } 45 edge.clear(); 46 } 47 void AddEdge(int u, int v, int d) 48 { 49 G[u].push_back(edge.size()); 50 edge.push_back(Edge(u, v, d)); 51 } 52 void dijkstra(int s) 53 { 54 priority_queue<qnode> q; 55 d[s]=0; 56 q.push(qnode(s, 0)); 57 while(!q.empty()) 58 { 59 qnode x=q.top(); 60 q.pop(); 61 if(vis[x.u]) 62 continue ; 63 vis[x.u]=true; 64 for(int i=0; i<G[x.u].size(); i++) 65 { 66 Edge& e=edge[G[x.u][i]]; 67 if(d[e.v]>d[x.u]+e.d) 68 { 69 d[e.v]=d[x.u]+e.d; 70 q.push(qnode(e.v, d[e.v])); 71 } 72 } 73 } 74 } 75 } dij; 76 77 int main() 78 { 79 int n, m; 80 while(scanf("%d%d", &n, &m),n+m) 81 { 82 dij.init(n); 83 while(m--) 84 { 85 int u, v, w; 86 scanf("%d%d%d", &u, &v, &w); 87 dij.AddEdge(u, v, w); 88 dij.AddEdge(v, u, w); 89 } 90 dij.dijkstra(1); 91 printf("%d\n",dij.d[n]); 92 } 93 return 0; 94 }
邻接矩阵Dij
本来以为这两个时间效率相差会很大,看来只在稠密图中奏效。
当图稠密起来后一般需要用堆优化的dijkstra。。。
时间: 2024-11-11 06:48:18