3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
今天刚刚开始学习最短路,这道题也就是一道裸的最短路题,狠狠的将这道题敲个各个最短路的模板- -
首先是Dijkstra,这个是n^2的复杂度版本
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int inf = 1<<30; int n,m; int map[300][300]; int vis[300],cast[300]; void Dijkstra(int s,int e) { int i,j,min,pos; memset(vis,0,sizeof(vis)); cast[s] = 0; vis[s] = 1; for(i = 0;i<n;i++) cast[i] = map[s][i]; for(i = 1;i<n;i++) { min = inf; for(j = 0;j<n;j++) { if(cast[j]<min && !vis[j]) { pos = j; min = cast[j]; } } vis[pos] = 1; for(j = 0;j<n;j++) { if(cast[pos]+map[pos][j]<cast[j] && !vis[j]) cast[j] = cast[pos]+map[pos][j]; } } } int main() { int i,j,x,y,z,start,end; while(~scanf("%d%d",&n,&m)) { for(i = 0; i<200; i++) { for(j = 0; j<200; j++) map[i][j] = inf; map[i][i] = 0; } for(i = 0; i<m; i++) { scanf("%d%d%d",&x,&y,&z); if(z<map[x][y]) map[x][y] = map[y][x] = z; } scanf("%d%d",&start,&end); Dijkstra(start,end); printf("%d\n",cast[end]==inf?-1:cast[end]); } return 0; }
还是Dijkstra,但是这个复杂度为nlogn,当然其优势在这道题并不明显,数据量太小
#include <stdio.h> #include <queue> #include <string.h> #include <algorithm> using namespace std; const int inf = 1<<30; const int L = 1000+10; struct Edges { int x,y,w,next; }; struct node { int d; int u; node (int dd = 0,int uu = 0):d(dd),u(uu) {} bool operator < (const node &x) const { return u>x.u; } }; priority_queue<node> Q; Edges e[L<<2]; int head[L]; int dis[L]; int vis[L]; void AddEdge(int x,int y,int w,int k) { e[k].x = x,e[k].y = y,e[k].w = w,e[k].next = head[x],head[x] = k++; e[k].x = y,e[k].y = x,e[k].w = w,e[k].next = head[y],head[y] = k++; } void init(int n,int m) { int i; memset(e,-1,sizeof(e)); for(i = 0; i<n; i++) { dis[i] = inf; vis[i] = 0; head[i] = -1; } for(i = 0; i<2*m; i+=2) { int x,y,w; scanf("%d%d%d",&x,&y,&w); AddEdge(x,y,w,i); } } int Dijkstra(int n,int src) { node mv; int i,j,k,pre; vis[src] = 1; dis[src] = 0; Q.push(node(src,0)); for(pre = src,i = 1; i<n; i++) { for(j = head[pre]; j!=-1; j=e[j].next) { k = e[j].y; if(!vis[k] && dis[pre]+e[j].w<dis[k]) { dis[k] = dis[pre]+e[j].w; Q.push(node(e[j].y,dis[k])); } } while(!Q.empty()&&vis[Q.top().d]==1) Q.pop(); if(Q.empty()) break; mv = Q.top(); Q.pop(); vis[pre=mv.d] = 1; } } int main() { int n,m,i,j,x,y; while(~scanf("%d%d",&n,&m)) { init(n,m); scanf("%d%d",&x,&y); Dijkstra(n,x); printf("%d\n",dis[y]==inf?-1:dis[y]); } return 0; }
然后是Floyd,这个算法写起来比较简单,但是复杂度为n^3,,花费时间太多了
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int inf = 100000000; int map[205][205]; void Floyd(int n) { int i,j,k; for(i = 0;i<n;i++) for(j = 0;j<n;j++) for(k = 0;k<n;k++) if(map[j][i]+map[i][k]<map[j][k]) map[j][k] = map[j][i]+map[i][k]; } int main() { int n,m,i,j,x,y,z,start,end; while(~scanf("%d%d",&n,&m)) { for(i = 0; i<200; i++) { for(j = 0; j<200; j++) map[i][j] = inf; map[i][i] = 0; } while(m--) { scanf("%d%d%d",&x,&y,&z); if(z<map[x][y]) map[x][y] = map[y][x] = z; } Floyd(n); scanf("%d%d",&start,&end); printf("%d\n",map[start][end]!=inf?map[start][end]:-1); } return 0; }
SPFA有两个版本,首先是堆栈的实现
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int inf = 1<<30; const int L = 2000+10; struct Edges { int x,y,w,next; }e[L<<2]; int head[L]; int dis[L]; int vis[L]; int relax(int u,int v,int c) { if(dis[v]>dis[u]+c) { dis[v] = dis[u]+c; return 1; } return 0; } void AddEdge(int x,int y,int w,int k) { e[k].x = x,e[k].y = y,e[k].w = w,e[k].next = head[x],head[x] = k++; e[k].x = y,e[k].y = x,e[k].w = w,e[k].next = head[y],head[y] = k++; } void init(int n,int m) { int i; memset(e,-1,sizeof(e)); for(i = 0; i<n; i++) { dis[i] = inf; vis[i] = 0; head[i] = -1; } for(i = 0; i<2*m; i+=2) { int x,y,w; scanf("%d%d%d",&x,&y,&w); AddEdge(x,y,w,i); } } int SPFA(int src) { int i; dis[src] = 0; vis[src] = 1; int Q[2005],top = 1; Q[0] = src; while(top) { int u,v; u = Q[--top]; vis[u] = 0; for(i = head[u];i!=-1;i=e[i].next) { v = e[i].y; if(relax(u,v,e[i].w)==1 && !vis[v]) { Q[top++] = v; vis[v] = 1; } } } } int main() { int n,m,i,j,x,y; while(~scanf("%d%d",&n,&m)) { init(n,m); scanf("%d%d",&x,&y); SPFA(x); printf("%d\n",dis[y]==inf?-1:dis[y]); } return 0; }
还是SPFA,这次是队列的实现
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std; const int inf = 1<<30; const int L = 2000+10; struct Edges { int x,y,w,next; }e[L<<2]; int head[L]; int dis[L]; int vis[L]; int cnt[L]; int relax(int u,int v,int c) { if(dis[v]>dis[u]+c) { dis[v] = dis[u]+c; return 1; } return 0; } void AddEdge(int x,int y,int w,int k) { e[k].x = x,e[k].y = y,e[k].w = w,e[k].next = head[x],head[x] = k++; e[k].x = y,e[k].y = x,e[k].w = w,e[k].next = head[y],head[y] = k++; } void init(int n,int m) { int i; memset(e,-1,sizeof(e)); for(i = 0; i<n; i++) { dis[i] = inf; vis[i] = 0; head[i] = -1; } for(i = 0; i<2*m; i+=2) { int x,y,w; scanf("%d%d%d",&x,&y,&w); AddEdge(x,y,w,i); } } int SPFA(int src) { int i; memset(cnt,0,sizeof(cnt)); dis[src] = 0; queue<int> Q; Q.push(src); vis[src] = 1; cnt[src]++; while(!Q.empty()) { int u,v; u = Q.front(); Q.pop(); vis[u] = 0; for(i = head[u];i!=-1;i=e[i].next) { v = e[i].y; if(relax(u,v,e[i].w)==1 && !vis[v]) { Q.push(v); vis[v] = 1; } } } } int main() { int n,m,i,j,x,y; while(~scanf("%d%d",&n,&m)) { init(n,m); scanf("%d%d",&x,&y); SPFA(x); printf("%d\n",dis[y]==inf?-1:dis[y]); } return 0; }