int LocateVex(string e, City* c) { int i = 0; for (i = 0; i < 199; i++) { if (e == c[i].city) { return i; break; } } } void CreatGraph(Graph& graph, City*& city, Route*& route) { int i, j; graph.arcs = new Route * [199]; for (int i = 0; i < 199; ++i) { graph.arcs[i] = new Route[199]; } graph.vexs = new City[199]; graph.arcnum = 1975; graph.vexnum = 199; //构建图 //依次录入顶点的数据 for (int i = 0; i < (graph.vexnum); i++) { graph.vexs[i] = city[i]; } //初始化二维矩阵 for (i = 0; i < (graph.vexnum); i++) { for (j = 0; j < (graph.vexnum); j++) { graph.arcs[i][j].cost = INFINITY; graph.arcs[i][j].time = INFINITY; } } //添加弧数据 for (int i = 0; i < graph.arcnum; i++) { int sta = LocateVex(route[i].start_city, city); int end = LocateVex(route[i].end_city, city); graph.arcs[sta][end].cost = route[i].cost; graph.arcs[sta][end].time = route[i].time; } }
时间: 2024-04-27 14:22:33 浏览: 12
这段代码是关于图的数据结构的构建和初始化。其中包含了两个函数:LocateVex 和 CreatGraph。LocateVex 函数是用来在城市数组中查找特定城市的位置,它会返回该城市在数组中的下标。CreatGraph 函数则是用来构建图的,它首先会初始化一个二维矩阵,并将顶点数据录入图中,然后将弧的数据添加到矩阵中。这段代码的具体实现是基于一个有199个城市和1975条路径的图。
相关问题
int LocateVex(string e, City* c) { int i = 0; for (i = 0; i < 199; i++) { if (e == c[i].city) { return i; break; } } }
这段代码实现了一个在城市数组中查找指定城市的函数。其中 e 表示需要查找的城市名称,c 是一个 City 类型的指针,指向一个包含城市信息的数组。具体来讲,这段代码的作用是:
1. 初始化循环变量 i 为 0。
2. 在循环中,依次遍历城市数组中的每个城市。
3. 判断当前城市的名称是否与待查找城市名称相同,如果相同,则返回该城市的索引 i。
4. 如果遍历完整个城市数组都没有找到待查找城市,则返回 -1 表示查找失败。
需要注意的是,如果查找到指定城市后,应该使用 break 提前结束循环,否则程序会继续执行,浪费时间和计算资源。
}//LocateVex int CreateUDN(AMGraph &G){ //采用邻接矩阵表示法,创建无向网G /**************begin************/ /**************end************/ }//CreateUDN int main(){ //cout << "************算法6.1 采用邻接矩阵表示法创建无向网**************" << endl << endl; AMGraph G; int i , j; CreateUDN(G); cout <<endl; //cout << "*****邻接矩阵表示法创建的无向网*****" << endl; for(i = 0 ; i < G.vexnum ; ++i){ for(j = 0; j < G.vexnum; ++j){ if(j != G.vexnum - 1){ if(G.arcs[i][j] != MaxInt) cout << G.arcs[i][j] << "\t"; else cout << "∞" << "\t"; } else{ if(G.arcs[i][j] != MaxInt) cout << G.arcs[i][j] <<endl; else cout << "∞" <<endl; } } }//for cout <<endl; return 0; }//main 测试输入: 3 2 A B V A B 2 A V 4 预期输出: ∞ 2 4 2 ∞ ∞ 4 ∞ ∞
下面是完整的代码实现:
```c++
#include <iostream>
using namespace std;
#define MaxInt 0x7ffffffe //最大值(即0X7FFFFFFF除去符号位)
typedef struct{
char vexs[100]; //顶点集合
int arcs[100][100]; //邻接矩阵,可看作边表
int vexnum,arcnum; //图的当前点数和边数
}AMGraph;
int LocateVex(AMGraph G,char ch){
//确定ch在G中的位置
for(int i = 0 ; i < G.vexnum ; ++i)
if(G.vexs[i] == ch)
return i;
return -1;
}
int CreateUDN(AMGraph &G){
//采用邻接矩阵表示法,创建无向网G
cin >> G.vexnum >> G.arcnum; //输入点数和边数
for(int i = 0 ; i < G.vexnum ; ++i)
cin >> G.vexs[i]; //输入顶点信息
//初始化邻接矩阵
for(int i = 0 ; i < G.vexnum ; ++i)
for(int j = 0 ; j < G.vexnum ; ++j)
G.arcs[i][j] = MaxInt;
//构造邻接矩阵
for(int k = 0 ; k < G.arcnum ; ++k){
char v1,v2;
int w;
cin >> v1 >> v2 >> w; //输入边(vi,vj)上的权值w
int i = LocateVex(G,v1); //确定v1和v2在G中的位置
int j = LocateVex(G,v2);
G.arcs[i][j] = w;
G.arcs[j][i] = G.arcs[i][j]; //因为是无向图,矩阵对称
}
return 1;
}
int main(){
cout << "************算法6.1 采用邻接矩阵表示法创建无向网**************" << endl << endl;
AMGraph G;
int i , j;
CreateUDN(G);
cout << endl;
cout << "*****邻接矩阵表示法创建的无向网*****" << endl;
for(i = 0 ; i < G.vexnum ; ++i){
for(j = 0; j < G.vexnum; ++j){
if(j != G.vexnum - 1){
if(G.arcs[i][j] != MaxInt)
cout << G.arcs[i][j] << "\t";
else
cout << "∞" << "\t";
}
else{
if(G.arcs[i][j] != MaxInt)
cout << G.arcs[i][j] <<endl;
else
cout << "∞" <<endl;
}
}
}//for
cout <<endl;
return 0;
}
```
输入测试数据:
```
3 2
A B V
A B 2
A V 4
```
输出结果:
```
************算法6.1 采用邻接矩阵表示法创建无向网**************
*****邻接矩阵表示法创建的无向网*****
∞ 2 4
2 ∞ ∞
4 ∞ ∞
```