题目描述:
给出一个图的邻接矩阵,输入顶点v,用迪杰斯特拉算法求顶点v到其它顶点的最短路径。
输入:
第一行输入t,表示有t个测试实例
第二行输入顶点数n和n个顶点信息
第三行起,每行输入邻接矩阵的一行,以此类推输入n行
第i个结点与其它结点如果相连则为距离,无连接则为0,数据之间用空格
隔开。第四行输入v0,表示求v0到其他顶点的最短路径距离
以此类推输入下一个示例
输出:
对每组测试数据,输出:
每行输出v0到某个顶点的最短距离和最短路径
每行格式:v0编号-其他顶点编号-最短路径值----[最短路径]。没有路径输出:v0编号-其他顶点编号--1。
输入样例:
2
5 0 1 2 3 4
0 5 0 7 15
0 0 5 0 0
0 0 0 0 1
0 0 2 0 0
0 0 0 0 0
0
6 V0 V1 V2 V3 V4 V5
0 0 10 0 30 100
0 0 5 0 0 0
0 0 0 50 0 0
0 0 0 0 0 10
0 0 0 20 0 60
0 0 0 0 0 0
V0
输出样例:
0-1-5----[0 1 ]
0-2-9----[0 3 2 ]
0-3-7----[0 3 ]
0-4-10----[0 3 2 4 ]
V0-V1--1
V0-V2-10----[V0 V2 ]
V0-V3-50----[V0 V4 V3 ]
V0-V4-30----[V0 V4 ]
V0-V5-60----[V0 V4 V3 V5 ]
Floyd实现
#include <iostream>
using namespace std;
int const N = 510;
string name[N];
int g[N][N], rode[N][N];
int n;
int main()
{
int _;
cin >> _;
while (_--)
{
cin >> n;
for (int i = 0;i < n;i++) cin >> name[i];
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++)
{
cin >> g[i][j];
rode[i][j] = j;
if (!g[i][j]) g[i][j] = 0x3f3f3f3f;
if (i == j) g[i][j] = 0;
}
string st;
cin >> st;
int root;
for (int i = 0;i < n;i++)
if (name[i] == st)
{
root = i;
break;
}
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++)
for (int k = 0;k < n;k++)
{
if (g[i][j] > g[i][k] + g[k][j])
{
g[i][j] = g[i][k] + g[k][j];
rode[i][j] = rode[i][k];
}
}
for (int i = 0;i < n;i++)
{
if (i == root) continue;
cout << name[root] << '-' << name[i] << '-';
if (g[root][i] == 0x3f3f3f3f) cout << -1 << endl;
else
{
cout << g[root][i] << "----[";
int t = root;
while (t != i)
{
cout << name[t] << ' ';
t = rode[t][i];
}
cout << name[i] << ' ' << "]" << endl;
}
}
}
return 0;
}
Dijkstra STL-Version
#include <bits/stdc++.h>
using namespace std;
int const N = 510;
int g[N][N], dis[N], n, st[N], path[N];
unordered_map<string, int> mp;//保存点对应的下标
unordered_map<int, string> rmp;//保存下标对应的点
void Dijkstra(int root)
{
memset(st, 0, sizeof st);
memset(path, -1, sizeof path);
memset(dis, 0x3f, sizeof dis);
dis[root] = 0;
for (int cnt = 0;cnt < n;cnt++)
{
int t = -1;
for (int i = 0;i < n;i++)
if (!st[i] && (t == -1 || dis[t] > dis[i]))
t = i;
st[t] = 1;
for (int i = 0;i < n;i++)
if (dis[i] > dis[t] + g[t][i])
{
dis[i] = dis[t] + g[t][i];
path[i] = t;
}
}
for (int i = 0;i < n;i++)
{
if (i == root) continue;
if (dis[i] == 0x3f3f3f3f) dis[i] = -1;
cout << rmp[root] << '-' << rmp[i] << '-' << dis[i];
if (dis[i] == -1) { puts("");continue; }
cout << "----[";
int now = i;
vector<string> p_list;
while (path[now] != -1) { p_list.push_back(rmp[now]); now = path[now]; }
p_list.push_back(rmp[root]);
reverse(p_list.begin(), p_list.end());
for (auto item : p_list) cout << item << ' ';
cout << ']' << endl;
}
}
int main()
{
int _;
cin >> _;
while (_--)
{
mp.clear();
cin >> n;
for (int i = 0;i < n;i++)
{
string name;
cin >> name;
mp[name] = i;
rmp[i] = name;
}
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++)
{
cin >> g[i][j];
if (!g[i][j]) g[i][j] = 0x3f3f3f3f;
}
string root;
cin >> root;
Dijkstra(mp[root]);
}
return 0;
}
Dijkstra no-STL
#include <iostream>
#include <cstring>
using namespace std;
int const N = 510;
int g[N][N], dis[N], n, st[N], path[N];
string w[N];
void Dijkstra(int root)
{
memset(st, 0, sizeof st);
memset(path, -1, sizeof path);
memset(dis, 0x3f, sizeof dis);
dis[root] = 0;
for (int cnt = 0;cnt < n;cnt++)
{
int t = -1;
for (int i = 0;i < n;i++)
if (!st[i] && (t == -1 || dis[t] > dis[i]))
t = i;
st[t] = 1;
for (int i = 0;i < n;i++)
if (dis[i] > dis[t] + g[t][i])
{
dis[i] = dis[t] + g[t][i];
path[i] = t;
}
}
for (int i = 0;i < n;i++)
{
if (i == root) continue;
if (dis[i] == 0x3f3f3f3f) dis[i] = -1;
cout << w[root] << '-' << w[i] << '-' << dis[i];
if (dis[i] == -1) { puts("");continue; }
cout << "----[";
int now = i;
string ans[N];int idx = 0;
while (path[now] != -1) { ans[idx++] = w[now]; now = path[now]; }
ans[idx++] = w[root];
for (int i = idx - 1;i >= 0;i--) cout << ans[i] << ' ';
cout << ']' << endl;
}
}
int main()
{
int _;
cin >> _;
while (_--)
{
cin >> n;
for (int i = 0;i < n;i++)
{
string name;
cin >> name;
w[i] = name;
}
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++)
{
cin >> g[i][j];
if (!g[i][j]) g[i][j] = 0x3f3f3f3f;
}
string root;
cin >> root;
for (int i = 0;i < n;i++)
if (w[i] == root)
{
Dijkstra(i);
break;
}
}
return 0;
}