#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
#include <list>
using namespace std;
template <class TypeOfVer, class TypeOfEdge>//节点数值,边数值
class adjmatrix_graph {
private:
int Vers; //顶点(节点)数
int Edges; //边数
//存放邻接矩阵(TypeOfEdge表示顶点关系类型。对于无权图,用1或0,表示相邻否;对于带权图,则为权值类型)
vector<vector<TypeOfEdge> >edge;//邻接矩阵
TypeOfEdge noEdge; //邻接矩阵中的∞的表示值
vector<TypeOfVer> ver; //存放结点值
string GraphKind; //图的种类标志
bool have_dir = false, have_w = false;//图类型参数
public:
adjmatrix_graph()
{
Vers = 0;
Edges = 0;
edge.clear();
ver.clear();
noEdge = 0;
}
~adjmatrix_graph()
{
;
}
//全自动输入 true is need 无边标记
bool Auto_input(bool need_emp)//true is need 无边标记
{
//DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
/*第一行:图的类型 DN UDN
第二行:结点数
第三行:结点集
第四行:无边标记
第五行:边数
第六行:边集
第七行:权集*/
/*第一行:图的类型 DG UDG
第二行:结点数
第三行:结点集
第四行:边数
第五行:边集*/
cin >> GraphKind;//图的类型
cin >> Vers;//结点数
ver.resize(Vers);
for (int i = 0; i < Vers; i++)//结点集
cin >> ver[i];
if (need_emp)//|| GraphKind == "DN" || GraphKind == "DNG")
cin >> noEdge;//无边标记
vector<TypeOfEdge> line;//邻接矩阵初始化
for (int j = 0; j < Vers; j++)
{
for (int i = 0; i < Vers; i++)
line.push_back(noEdge);
edge.push_back(line);
}
cin >> Edges;//边数
vector<int> x_p, y_p, w_p;
for (int i = 0; i < Edges; i++)
{
int c_x, c_y;
cin >> c_x >> c_y;
x_p.push_back(c_x);
y_p.push_back(c_y);
}
//图的类型识别
if (GraphKind == "DG")//DG(有向图)
have_dir = true, have_w = false;
if (GraphKind == "DN")//DN(有向网)
have_dir = true, have_w = true;
if (GraphKind == "UDG")//UDG(无向图)
have_dir = false, have_w = false;
if (GraphKind == "UDN")//UDN(无向网)
have_dir = false, have_w = true;
if (have_w)
for (int i = 0; i < Edges; i++)
{
int c_w;
cin >> c_w;
w_p.push_back(c_w);
}
for (int i = 0; i < Edges; i++)
{
if (have_dir == false)//无向图操作
{
if (have_w == true)//带权值的网的操作
edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = w_p[i];
else//无权值操作
edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = 1;
}
else
{
if (have_w == true)//带权值的网的操作
edge[x_p[i]][y_p[i]] = w_p[i];
else//无权值操作
edge[x_p[i]][y_p[i]] = 1;
}
}
return 1;
}
//输出邻接矩阵
bool PrintMatrix()
{
int i, j;
for (i = 0; i < Vers; i++)
{
for (j = 0; j < Vers - 1; j++)
cout << edge[i][j] << "\t";
cout << edge[i][j] << "\t";
cout << endl;
}
return 1;
}
//判断图空否
bool GraphisEmpty(void)
{
return Vers == 0;
}
//图的类型
string GetGraphKind(void)
{
return GraphKind;
}
//获得顶点集
vector<TypeOfVer> GetGraph_Point(void)
{
return ver;
}
//======================================================
int dp[150][150],path_way[150][150];
void Floyd_DP()
{
int i, j, k;
for (k = 0; k < Vers; k++)
for (i = 0; i < Vers; i++)
for (j = 0; j < Vers; j++)
{
if (i!=j && dp[i][j] > dp[i][k] + dp[k][j])
{
dp[i][j] = dp[i][k] + dp[k][j];
path_way[i][j] = path_way[k][j];
}
}
return;
}
void Floyd()
{
int i, j;
for (i = 0; i < Vers; i++)
{
for (j = 0; j < Vers; j++)
{
dp[i][j] = edge[i][j];
if (i != j)
path_way[i][j] = i;
else
path_way[i][j] = -1;
}
}
Floyd_DP();
for (i = 0; i < Vers; i++)
{
for (j = 0; j < Vers; j++)
if (i != j)
{
cout << dp[i][j] << "\t";
}
else
cout << 0 << "\t";
cout << endl;
}
cout << endl;
for (i = 0; i < Vers; i++)
{
for (j = 0; j < Vers; j++)
if (dp[i][j] != noEdge)
cout << path_way[i][j] << "\t";
else
{
path_way[i][j]=-1;
cout << -1 << '\t';
}
cout << endl;
}
cout << endl;
for (i = 0; i < Vers; i++)
{
for (j = 0; j < Vers; j++)
{
stack<int> out_list;
int next_p=j;
while (1)
{
out_list.push(next_p);
if (path_way[i][next_p] == -1)
break;
next_p = path_way[i][next_p];
}
if (out_list.size() <=1)
continue;
cout << "(";
while (out_list.size() > 1)
{
cout << out_list.top() << "->";
out_list.pop();
}
cout<<out_list.top();
cout << "),";
cout << dp[i][j];
cout << endl;
}
}
return;
}
};
int main()
{
int i;
string s;
adjmatrix_graph<string, int> a;
a.Auto_input(1);
//cout << a.GetGraphKind() << endl;//输出类型
//输出顶点集----------------------
//vector<string> ans;
//ans = a.GetGraph_Point();
//for (i = 0; i < ans.size() - 1; i++)
// cout << ans[i] << " ";
//cout << ans[i] << endl;
//-------------------------------
///cout << endl;
a.PrintMatrix();
cout << endl;
//==============================
///a.Prim(start_p);
a.Floyd();
return 0;
}
11 数据结构课程设计小组任务11:Floyd算法的设计
最新推荐文章于 2024-01-21 15:50:04 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)