usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
来源:互联网 发布:希腊经济知乎 编辑:程序博客网 时间:2024/06/10 07:37
题目链接
水题,求到某一个点的最短路
(第一发还写跪了...)
后来觉得需要练练手(?并不能起到什么作用)于是分别写了Floyd,Dijkstra以及Dijkstra堆优化(即优先队列)的
Floyd
/*PROB:comehomeLANG:C++ID:fan_0111*/#include <iostream>#include <cstdio>#include <cctype>typedef long long LL;LL a[100][100];using namespace std;int tran(char ch) { if (isupper(ch)) return ch - 'A'; else return ch - 'a' + 26;}int main() { freopen("comehome.in", "r", stdin); freopen("comehome.out", "w", stdout); int n; scanf("%d\n", &n); for (int i = 0; i < n; ++i) { char ch1, ch2; int d; scanf("%c %c%d\n", &ch1, &ch2, &d); if (ch1 == ch2) continue; int x1 = tran(ch1), x2 = tran(ch2);// printf("%c %c\n", ch1, ch2); if (a[x1][x2] == 0 || a[x1][x2] > d) a[x1][x2] = a[x2][x1] = d;// printf("a[%d][%d] = %d\n", x1, x2, a[x1][x2]); } const int tot = 52; for (int k = 0; k < tot; ++k) { for (int i = 0; i < tot; ++i) { for (int j = i + 1; j < tot; ++j) { if (a[i][k] != 0 && a[k][j] != 0 && (a[i][j] == 0 || a[i][j] > a[i][k] + a[k][j])) { a[j][i] = a[i][j] = a[i][k] + a[k][j]; } } } } int p; int maxD = 1e9; for (int i = 0; i < 25; ++i) { if (a[i][25] != 0 && a[i][25] < maxD) { p = i; maxD = a[i][25]; } } printf("%c %lld\n", 'A' + p, a[p][25]); return 0;}
Dijkstra
/*PROB:comehomeLANG:C++ID:fan_0111*/#include <iostream>#include <cstdio>#include <cctype>#define MAX 60000typedef long long LL;const int tot = 52;int a[100][100], dist[100];bool vis[60];using namespace std;int tran(char ch) { if (isupper(ch)) return ch - 'A'; else return ch - 'a' + 26;}int main() { freopen("comehome.in", "r", stdin); freopen("comehome.out", "w", stdout); int n; scanf("%d\n", &n); for (int i = 0; i < n; ++i) { char ch1, ch2; int d; scanf("%c %c%d\n", &ch1, &ch2, &d); if (ch1 == ch2) continue; int x1 = tran(ch1), x2 = tran(ch2); if (a[x1][x2] == 0 || a[x1][x2] > d) a[x1][x2] = a[x2][x1] = d; } int src = 25; for (int cnt = 0; cnt < tot - 1; ++cnt) { int minD = MAX, p; for (int i = 0; i < tot; ++i) { dist[i] = a[src][i]; if (!vis[i] && dist[i] != 0 && dist[i] < minD) { minD = dist[i]; p = i; } } vis[p] = true; for (int i = 0; i < tot; ++i) { if (i != src && a[p][i] != 0 && (a[src][i] == 0 || a[src][i] > a[src][p] + a[p][i])) a[src][i] = a[i][src] = a[src][p] + a[p][i]; } } int p, minD = MAX; for (int i = 0; i < 25; ++i) {// printf("%d\n", a[src][i]); if (a[src][i] != 0 && a[src][i] < minD) { p = i; minD = a[src][i]; } } printf("%c %d\n", 'A' + p, a[src][p]); return 0;}
优先队列
/*PROB:comehomeLANG:C++ID:fan_0111*/#include <iostream>#include <cstdio>#include <cctype>#include <queue>#define MAX 60000typedef long long LL;const int tot = 52;int a[100][100], dist[100];bool vis[60];using namespace std;int tran(char ch) { if (isupper(ch)) return ch - 'A'; else return ch - 'a' + 26;}struct qnode { int v, c; qnode(int vv = 0, int cc = 0) : v(vv), c(cc) {} bool operator < (const qnode& node) const { return c > node.c; }};priority_queue<qnode> que;int main() { freopen("comehome.in", "r", stdin); freopen("comehome.out", "w", stdout); int n; scanf("%d\n", &n); for (int i = 0; i < n; ++i) { char ch1, ch2; int d; scanf("%c %c%d\n", &ch1, &ch2, &d); if (ch1 == ch2) continue; int x1 = tran(ch1), x2 = tran(ch2); if (a[x1][x2] == 0 || a[x1][x2] > d) a[x1][x2] = a[x2][x1] = d; } int src = 25; vis[src] = true; for (int i = 1; i < tot; ++i) { for (int j = 0; j < tot; ++j) { if (a[src][j] == 0) continue; if (!vis[j] && (dist[j] == 0 || dist[j] > dist[src] + a[src][j])) { dist[j] = dist[src] + a[src][j]; que.push(qnode(j, dist[j])); } } while (!que.empty() && vis[que.top().v]) que.pop(); if (que.empty()) break; vis[src = que.top().v] = true; } int minD = MAX, p; for (int i = 0; i < 26; ++i) { if (dist[i] != 0 && dist[i] < minD) { minD = dist[i]; p = i; } } printf("%c %d\n", 'A' + p, dist[p]); return 0;}
阅读全文
0 0
- usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- 最短路(floyd+bellman+dijkstra+dijkstra优先队列优化+spfa)
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- hdu 2066最短路 dijkstra 及其优先队列优化
- [最短路]使用优先队列优化的Dijkstra算法
- 最短路dijkstra,floyd
- 最短路Floyd、Dijkstra
- 优先队列Dijkstra实现最短路算法
- 最短路(Dijkstra+优先队列)
- hdu 2112 最短路 dijkstra优先队列
- hdu2544 最短路(dijkstra/优先队列)
- dijkstra + 优先队列 最短路 [sdut 2143]
- ACM:最短路,dijkstra,邻接表的建立,使用邻接表跟优先队列的dijkstra,Bellman-Ford,Floyd。。
- 优先队列优化 dijkstra
- Dijkstra(优先队列优化)
- Dijkstra优先队列优化
- hdu2544最短路(floyd)(dijkstra)
- Android 自定义 View 的重绘和更新使用
- 【DOS命令】之 move 文件移动
- 矩阵覆盖
- 电话拨号器错误打包运行错误
- 安装win10
- usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
- 如何通过表单上传图片数据,PHP后端获取并且保存文件数据
- sdut-离散题目18
- 【PHP】修改 配置php.ini上传文件大小的限制
- Linux操作系统基础
- 图的最短路径问题-07-图6 旅游规划
- 通过源码的方式编译hadoop的安装文件
- 线索化二叉树
- 【附源码工程】Unity3D 支持代价的A星寻路和优化(二叉堆,寻路节点合并,相邻节点预先存储)