模板 最短路(簡單)

2021-07-09 22:52:22 字數 3759 閱讀 5760

以poj 1847為例。

/*

有n個路口,計算從a路口到b路口的最短轉彎數。

對於每乙個路口,有t個分叉口,第乙個分叉口是直行,也就是不用轉彎

剩下的t-1個路口都需要轉彎一次。

*//************dijkstra***************/

#include #include #include #include #include #include #include #include using namespace std;

const int maxn = 110;

const int maxm = 10010;

const int inf = 0x3f3f3f3f;

int map[maxn][maxn], n, a, b, t, p;

int dijkstra(int s, int e)//212k 47ms

if (k == -1) break;

vis[k] = 1;

for (int j = 1; j <= n; j++)

if (!vis[j] && map[k][j] < inf)

dis[j] = min(dis[j], dis[k] + map[k][j]);

} return dis[e];

}int main()

} int ans = dijkstra(a, b);

if (ans == inf) ans = -1;

printf("%d\n", ans);

} return 0;

}

/********dijkstra+heap*******/

#include #include #include #include #include #include #include #include using namespace std;

const int maxn = 110;

const int maxm = 10010;

const int inf = 0x3f3f3f3f;

struct edge edge[maxm];

int vis[maxn], head[maxn];

int n, a, b, t, p, tot;

void addedge(int from, int to, int dist, int k)

struct heapnode

}dis[maxn];

int dijkstra_heap(int s,int e)//184k 0ms

dis[s].dist = 0;

memset(vis, 0, sizeof(vis));

q.push(dis[s]);

while (!q.empty())

} }return dis[e].dist;

}int main()

} int ans = dijkstra_heap(a, b);

if (ans == inf) ans = -1;

printf("%d\n", ans);

} return 0;

}

/**********floyd***********/

#include #include #include #include #include #include #include #include using namespace std;

const int maxn = 110;

const int maxm = 10010;

const int inf = 0x3f3f3f3f;

int map[maxn][maxn], n, a, b, t, p;

int floyd(int s,int e)//212k 16ms

int main()

} int ans=floyd(a,b);

if (ans == inf) ans = -1;

printf("%d\n", ans);

} return 0;

}

/**************bellman_ford*********************/

#include #include #include #include #include #include #include #include using namespace std;

const int maxn = 110;

const int maxm = 10010;

const int inf = 0x3f3f3f3f;

int n, a, b, uu, vv, ww, t, tot, p;

int dis[maxn];

struct edge edge[maxm];

int bellman_ford(int s, int e)//172k 0ms

for (int i = 0; i < n - 1; i++)

for (int j = 0; j <= tot; j++)

return dis[e];

}int main()

} int ans = bellman_ford(a, b);

if (ans == inf) ans = -1;

printf("%d\n", ans);

} return 0;

}

/***********spfa+queue***********/

/***********spfa+stack***********/

#include #include #include #include #include #include #include #include using namespace std;

const int maxn = 110;

const int maxm = 10010;

const int inf = 0x3f3f3f3f;

int dis[maxn], head[maxn], vis[maxn];

int n, a, b, t, p, tot;

struct edge edge[maxm];

void addedge(int u, int v, int w, int k)

int spfa_queue(int s, int e)//佇列實現 184k 16ms

memset(vis, 0, sizeof(vis));

vis[s] = 1;

q.push(s);

while (!q.empty())

}} }

return dis[e];

}int spfa_stack(int s, int e)//棧實現 184k 0ms

memset(vis, 0, sizeof(vis));

vis[s] = 1;

q.push(s);

while (!q.empty())

}} }

return dis[e];

}int main()

} //int ans = spfa_queue(a, b);

int ans = spfa_stack(a, b);

if (ans == inf) ans = -1;

printf("%d\n", ans);

} return 0;

}

最短路模板

1.dijkstra演算法 演算法思想 從某乙個點開始,找與它距離最近的乙個點,然後更新所有點到這條路徑的距離,然後再次選取乙個距離上乙個點路徑最短的點,將其加入路徑,以此進行,直到求出起點到所有點的最短距離.注意 權值不能為負,如圖 1到3的距離應該是零,但這裡結果為一 1 鄰接矩陣 複雜度 o ...

最短路模板

週六週日tyvj上有模擬賽 順便逃了周練 霧 複習了一下圖論裡的基本演算法 彷彿第一次寫最短路的手生 寫的太少了 orz dijkstra include define maxn 10001 define maxm 300001 using namespace std template void r...

最短路模板

include include include include include include define inf 0x3f3f3f3f using namespace std const int maxn 105 int dis maxn pre maxn struct edge 邊 edge ...