HDU 2544 最短路重刷,四种算法

来源:互联网 发布:php导出excel表格下载 编辑:程序博客网 时间:2024/05/16 06:03

题意,赤果果的最短路

想当年傻傻地用floyd和dijkstra乱刷过,最近好好理解了一下最短路,今天用四种算法实现了一遍。

以后可以当模板用。

代码:

floyd

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        hdu2544.cpp*  Create Date: 2013-11-28 15:37:26*  Descripton:  min path, floyd*/#include <cstdio>#include <cstring>const int MAXN = 110;const int INF = 0x3c3c3c3c - 1;int nn, en;// num of node and edgeint map[MAXN][MAXN];int min(int a, int b) {return a < b ? a : b;}int floyd() {for (int k = 1; k <= nn; k++)for (int i = 1; i <= nn; i++)for (int j = 1; j <= nn; j++)map[i][j] = min(map[i][j], map[i][k] + map[k][j]);}int main() {int s, e, t;while (~scanf("%d%d", &nn, &en) && (nn || en)) {// initfor (int i = 1; i <= nn; i++)for (int j = 1; j <= nn; j++)map[i][j] = INF;// inputfor (int i = 0; i < en; i++) {scanf("%d%d%d", &s, &e, &t);if (t < map[s][e])map[s][e] = map[e][s] = t;}floyd();printf("%d\n", map[1][nn]);}return 0;}


dijkstra

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        hdu2544.cpp*  Create Date: 2013-11-28 15:37:26*  Descripton:  min path, dijkstra*/#include <cstdio>#include <cstring>const int MAXN = 110;const int INF = 0x3c3c3c3c - 1;int nn, en;// num of node and edgeint map[MAXN][MAXN], dis[MAXN];bool vis[MAXN];int dijkstra(int s, int e) {memset(vis, 0, sizeof(vis));// init the disfor (int i = 1; i <= nn; i++)if (i == s) dis[i] = 0;else dis[i] = map[s][i];vis[s] = true;for (int i = 1; i <= nn; i++) {int t = INF, k;for (int j = 1; j <= nn; j++)if (!vis[j] && t > dis[j])t = dis[j], k = j;if (t == INF) break;vis[k] = true;for (int j = 1; j <= nn; j++)if (!vis[j] && dis[j] > dis[k] + map[k][j])dis[j] = dis[k] + map[k][j];}return dis[e];}int main() {int s, e, t;while (~scanf("%d%d", &nn, &en) && (nn || en)) {// initfor (int i = 1; i <= nn; i++)for (int j = 1; j <= nn; j++)map[i][j] = INF;// inputfor (int i = 0; i < en; i++) {scanf("%d%d%d", &s, &e, &t);if (t < map[s][e])map[s][e] = map[e][s] = t;}printf("%d\n", dijkstra(1, nn));}return 0;}


bellman ford

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        hdu2544.cpp*  Create Date: 2013-11-28 15:37:26*  Descripton:  min path, bellman ford */#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 110;const int INF = 0x3c3c3c3c - 1;struct Edge {int u, v;int cost;} e[MAXN * MAXN / 2];int nn, en;// num of node and edgeint dis[MAXN];int bellman_ford(int op, int ed) {for (int i = 1; i <= nn; i++)dis[i] = INF;dis[op] = 0;for (int i = 0; i < nn - 1; i++)for (int j = 0; j < en * 2; j++)dis[e[j].v] = min(dis[e[j].v], dis[e[j].u] + e[j].cost);return dis[ed];}int main() {int op, ed, t;while (~scanf("%d%d", &nn, &en) && (nn || en)) {// inputfor (int i = 0; i < en; i++) {scanf("%d%d%d", &op, &ed, &t);e[i].u = op; e[i].v = ed; e[i].cost = t;e[i + en].u = ed; e[i + en].v = op; e[i + en].cost = t;}printf("%d\n", bellman_ford(1, nn));}return 0;}


spfa+queue

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        hdu2544.cpp*  Create Date: 2013-11-28 15:37:26*  Descripton:  min path, spfa*/#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <iostream>using namespace std;const int MAXN = 110;const int INF = 0x3c3c3c3c - 1;int nn, en;// num of node and edgeint map[MAXN][MAXN], dis[MAXN];bool vis[MAXN]; // if in the queueint spfa(int op, int ed) {queue<int> q;memset(vis, false, sizeof(vis));for (int i = 1; i <= nn; i++)dis[i] = INF;dis[op] = 0;q.push(op);vis[op] = true;while (!q.empty()) {int cur = q.front();q.pop();vis[cur] = false;for (int i = 1; i <= nn; i++)if (dis[i] > dis[cur] + map[cur][i]) {dis[i] = dis[cur] + map[cur][i];if (!vis[i]) {q.push(i);vis[i] = true;}}}return dis[ed];}int main() {int op, ed, t;while (~scanf("%d%d", &nn, &en) && (nn || en)) {// initfor (int i = 1; i <= nn; i++)for (int j = 1; j <= nn; j++)map[i][j] = INF;// inputfor (int i = 0; i < en; i++) {scanf("%d%d%d", &op, &ed, &t);if (t < map[op][ed])map[op][ed] = map[ed][op] = t;}printf("%d\n", spfa(1, nn));}return 0;}


原创粉丝点击