任意两个城市之间最短路(Floyd)
来源:互联网 发布:黑龙江省快乐十分软件 编辑:程序博客网 时间:2024/05/23 00:50
#include <iostream>#include <cstdlib>#include <string>#include <cstdio>#include <fstream>using namespace std;const int maxn = 100;int Hash(string s, string h[], int &num, int Match[]){ int now = 0; for (int i = 0; ; i++) { now = (s[0] - 'A' + i) % maxn; if (h[now] == "") {//如果没有被Hash过,就新建 h[now] = s; Match[num] = now; num++; return now; } else if (h[now] == s) {//如果被Hash过,就直接返回原来Hash出来的地址 return now; } }}void Floyd(){}int main(){ ifstream in("in.txt"); for (int i = 0; i < 4; i++) { string s; in >> s; } string h[maxn] = ""; string s1, s2; int path[maxn][maxn]; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) { path[i][j] = -1; } } int cost, dis; int cnt = 0; int num = 0; int Match[maxn]; while (in >> s1 >> s2 >> cost >> dis) { cnt++; int u = Hash (s1, h, num, Match); int v = Hash (s2, h, num, Match); path[u][v] = dis; path[v][u] = dis; } in.close(); for (int k = 0; k <= num; k++) { for (int i = 0; i <= num; i++) { for (int j = 0; j <= num; j++) { int a1 = Match[k], a2 = Match[i], a3 = Match[j]; if ((path[a2][a1] != -1 && path[a1][a3] != -1) && (path[a2][a1] + path[a1][a3] < path[a2][a3] || path[a2][a3] == -1)) { path[a2][a3] = path[a2][a1] + path[a1][a3]; } } } } int op; while (1) { cout << "如果想继续得到城市间的最短距离,请输入1,如果想要退出程序,请输入0" << endl; cin >> op; if (op == 0) break; cout << "请输入想要得到最短路的两个城市的名称,用空格隔开(请保证这两个城市的名称和文件中文件的名称一样)" << endl; cin >> s1 >> s2; int i = Hash (s1, h, num, Match), j = Hash (s2, h, num, Match); if (path[i][j] == -1) { cout << "抱歉,这两个城市之间没有路径到达" << endl; } else { cout << "两个城市之间最短距离为:" << path[i][j] << endl; } } return 0;}
0 0
- 任意两个城市之间最短路(Floyd)
- Floyd --任意两点之间的最短路问题
- PAT程序设计练习——甲级1003(任意两个城市最短距离、Floyd最短路径算法)
- 任意两点间的最短路问题(floyd算法)
- 基础Floyd--任意两点间最短路
- 最短路(floyd)
- 最短路算法(Floyd)
- UVA104Arbitrage(floyd最短路)
- hdu1690(最短路floyd)
- 最短路poj2253(floyd)
- hdu2544 最短路 (floyd)
- 最短路(Floyd算法)
- Cow Contest(POJ 3660)(Floyd)(任意两点间最短路)
- 【算法】Floyd-Warshall算法(任意两点间的最短路问题)(判断负圈)
- POJ - 2139 Six Degrees of Cowvin Bacon(任意两点最短路,Floyd)
- POJ 2139 Six Degrees of Cowvin Bacon(任意两点最短路,Floyd)
- Floyd-Warshall算法 (任意两点间的最短路问题)
- 每日一题 No.50 任意二点间的最短路问题(Floyd-Warshall算法)
- html具体例子
- C语言笔记(3)
- 红黑树(未完待续)
- 向vs2010添加GMapControl控件
- Debian与Centor区别
- 任意两个城市之间最短路(Floyd)
- 如何让eclipse自动提示xml文本的插件:alt+/
- Linux学习笔记4——文件访问权限
- Linux下的C语言编程——信号队列
- Html----基本介绍
- ubuntu--minicom使用中遇到的问题
- OGNL |对象栈和Map值栈
- 简易菜单的实现
- linux下mongodb的安装及启动