图-跨市路径

Graph comprehensive experiment

Experiment Date:2019年12月20日

One. Experiment purpose

  1. Familiar with the basic operation of the graph.

  2. Master the storage and travel operation of the graph.

  3. Deepen the understanding of the graph algorithms, and to develop the ability to solve practical problems gradually.

Two. Experimental environment

A computer with Visual Studio.

This experiment has 4 class hours in all.

Three、Experiment content

(Select one problem from the following three contents)

1. Location problem

【Basic requirement

There is a traffic map of n villages. If there is a road between village i and village j, connect them with an edge and Wij stands for the weight of this edge. Now we plan to choose one village and build a hospital. You are required to write following algorithms:

(1) Find out the village in which we should build the hospital, making the distance of the farthest village from the hospital is shortest.

(2) Find out the village in which we should build the hospital, making the sum distance of all villages to the hospital is shortest.

【Tips

ü For question(1),you can find out the shortest path of every village to all other villages, then store the max value(stands for the distance of the farthest village from hospital if hospital is built in this village); Then find a max value in those min value.

ü For question(2),you can find out the shortest distance of every village to all other villages, then store the sum of all distance(stands for the sum distance of all villages from the hospital); Then find a min value from these sums.

ü You can also draw a traffic map of n villages. Here is an example below:

2. Minimum cost problem

The government’s Village-to-Village project is a systematic and national project. It includes roads, electricity, and drinking water, telephone network, cable TV network, Internet network and so on. City XX is a poor city in the southwest of China. The city government, according to its own financial situation, decided to realize the road first, the rest will be realized in the next step. Now, there are roads between one and another town already, but there is nearly no road between villages at the lower level of the town. In order to simplify the project, the cost of the road is decided by the length of the road. Now, you should provide a “connectable village” plan for the town with a minimum cost according to the map of the village of a town and the possible roads built between villages.

hint:

Input format of village distribution:Village name 1 Village name 2 The distance between the two villages in kilometers

xx village yy village 120

​ xx village zz village 84

output: Village name1 Village name 2 The distance between the two villages in kilometers

​ (totally N-1 roads)

​ The total length in kilometers:XXXkm

3. traffic consultation

Simulate the national traffic consultation, providing travelers with three kinds of optimal traffic advice.

(1)When you enter a city name in the program, you need to enter a string of letters within 10 letters;Input an integer data when entering a train or aircraft number;Input a real type of data for the cost of entering a train or aircraft;Two integer data are required to enter the start time and the arrival time of the train or aircraft(In the form of hh:mm);When selecting a function, an integer data should be entered corresponding to the selected function.

(2)The mainly output information of the program is:How long does it takes to arrive in the least time or least travel expense? How much does it takes to arrive in the least travel expense? How many times you need to transfer at least? And which train or flight to take and at what time you take it.

(3)The functions of the program include:

a. Provide editing of city information.

b. Provide editing of train schedules and flight schedules.

c. provide three optimal decisions: arrival in the fastest way, arrival in the cheapest way, and arrival in the shortest way.

Four.Important data structures

Importantdata structures:
typedef struct Graph
{
	char city[Max];
	int graph[Max][Max];
}Graph;
The Global function
extern int D[Max][Max];
extern int p[Max][Max];
extern map<int, string>Map;
The main function
int main()
{
	Graph *G;
	G = (Graph*)malloc(sizeof(Graph));
	int n;
	int choice;
	cout << "提示:输入两个城市,第一个城市为起始城市,后一个为目的地城市" << endl;
	cout << "/-------0:退出选择------/"<<endl<<"/-------1:最短路径------/" << endl << "/-------2:最少金额------/" << endl << "/-------3:最少中转------/" << endl << "/-------4:最短时间------/" << endl;
	while (cin >> choice && choice != 0)
	{
		switch (choice)
		{
		case 1:
			n = Creat_Graph_D(G,choice);
			Floyd(G, n);
			In_put(choice,n);
			cout << "输入出发地输出出发地到其他地区的最短路径" << endl;
			Dijkstra(G,n);
			break;
		case 2:
			n = Creat_Graph_D(G, choice);
			Floyd(G, n);
			In_put(choice,n);
			break;
		case 3:
			n = Creat_Graph_D(G, choice);
			Floyd(G, n);
			In_put(choice,n);
			break;
		case 4:
			n = Creat_Graph_D(G, choice);
			Floyd(G, n);
			In_put(choice, n);
			break;
		default :
			cout << "还未添加改功能" << endl;
			break;
		}
		cout << "/-------0:退出选择------/" << endl << "/-------1:最短路径------/" << endl << "/-------2:最少金额------/" << endl << "/-------3:最少中转------/" << endl << "/-------4:最短时间------/" << endl;
		cout << "/*--------------------*/"<<endl;
	}

Five.Implementation analysis

Program Execution Structure Diagram
最短路径
花费最少
转中最少
花时间最少
输入选择
输入出发城市和目的地城市
选择
输出最短路径和公里数
输出最少花费和路径
输出中转最少和路径
输出花费的时间和路径
操作函数流程
Structure Diagram
最短路径
最少花费
最少时间
从文件中读取数据
根据不同的数据创建不同的图
选择
读取路径文件数据并创建图
读取花费文件数据并创建图
读取时间文件数据并创建图
弗洛伊德
输出
The main operation:
		case 1:
			n = Creat_Graph_D(G,choice);
			Floyd(G, n);
			In_put(choice,n);
			cout << "输入出发地输出出发地到其他地区的最短路径" << endl;
			Dijkstra(G,n);
			break;
		case 2:
			n = Creat_Graph_D(G, choice);
			Floyd(G, n);
			In_put(choice,n);
			break;
		case 3:
			n = Creat_Graph_D(G, choice);
			Floyd(G, n);
			In_put(choice,n);
			break;
		case 4:
			n = Creat_Graph_D(G, choice);
			Floyd(G, n);
			In_put(choice, n);
			break;
		default :
			cout << "还未添加改功能" << endl;
Create graph function
int Creat_Graph_D(Graph*G,int n)
{
	fstream Map_num("城市.txt", ios::in | ios::out | ios::app);
	fstream City_Dis("距离.txt", ios::in | ios::out | ios::app);
	fstream City_Cost("花费.txt", ios::in | ios::out | ios::app);
	fstream City_Time("时间.txt",ios::in | ios::out | ios::app);
Floyd
void Floyd(Graph*G, int n)
{
	int  i, j, k;
	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++)
		{
			if (G->graph[i][j] != Max_number)	//有联通路的记录
				p[i][j] = j;    //j是i的后继 
			else
				p[i][j] = 0;
			D[i][j] = G->graph[i][j];
		}
	for (k = 1; k <= n; k++) {
		{
			for (i = 1; i <= n; i++)
				for (j = 1; j <= n; j++)
				{
					if (D[i][k] + D[k][j] < D[i][j])
					{
						D[i][j] = D[i][k] + D[k][j];     //修改长度 
						p[i][j] = p[i][k];
					}
				}
		}
	}
}
Dijkstra
void Dijkstra(Graph*G,int n)
{
	string S1;
	cin >> S1;
	int v1=Find_City_num(S1);
	int D2[Max], P2[Max];//储存路径和最终最短距离
	int i, j, k, min,u;
	bool S[Max];		//用来标定是否已经找到最短路径
	for (k = 1; k <=n; k++)
	{
		S[k] = false;//还没找到
		D2[k] = G->graph[v1][k];
		if (G->graph[v1][k] < Max)
			P2[k] = v1;
		else
			P2[k] = 0;
	}					//先把图置为0
	D2[v1] = 0;			//最初只有一个点起点
	S[v1] = true;		//距离为0
	for (int i = 1; i <= n; i++)//其余的n-1个点
	{
		min = Max_number;
		for (int j = 1; j <=n; j++)
		{
			if (S[j]==0 && D2[j] < min)
			{
				u = j;
				min = D2[j];
			}
		}
		S[u] = true;//找到一条
		for(int i=1;i<=n;i++)
			if (S[i]==0 && (D2[u] + G->graph[u][i] < D2[i]))
			{
				D2[i] = D2[u] + G->graph[u][i];
				P2[i] = u;
			}
	}
	cout << "最短路径:"<<endl;
	for (int i = 1; i <= n; i++)
	{
		if (D2[i] == Max_number)
			cout << "无法到达" << endl;
		else
		{
			cout << "行驶的路程:" << D2[i] << " ";
			k = P2[i];
			cout << City_name(i);
			while (k != 0)
			{
				cout << "<-" << City_name(k);
				k = P2[k];
			}
			cout << endl;
		}
	}
	cout << endl;
}
Output function
void In_put(int choice,int n)
{
	map<int,string>::iterator it;
	int add1, add2, add;		//出发地、目的地,是否有路径
	string Add1, Add2;			//地址,目的地
	cout << "输入乘车地点和目的地" << endl;
	cin >> Add1 >> Add2;
	add1 = Find_City_num(Add1);	//对应寻找下标
	add2 = Find_City_num(Add2);
	add = p[add1][add2];//路径、钱、时间
	if (add == 0)
	{
		cout << "线路尚未开通" << endl;
		return;
	}
	if (choice == 1)
	{
		cout << "最短路径为:" << City_name(add1);//出发地
		while (add != add2)
		{
			cout << "->" << City_name(add);		//存在路线就回溯回去
			add = p[add][add2];
		}
		cout << "->" << City_name(add2) << endl;
		cout << "路径长度为" << D[add1][add2] <<"公里"<< endl;
	}

Six.Debugging problem analysis

(1)Problem:

When using Dijkstra, the output function exceeds the subscript, but no problem is found

Solvetion:

Good recovery by checking and changing subscript program

(2)Problem:

Using the map corresponding function to replace the two query functions failed. The compiler said that the pointer type did not match, but I did use the map of string and int type. The compiler said that there was a problem in parsing after the problem was solved.

Solvetion:

Did not solve, decided to use the search function written before the first use first, then have time to add map to solve the storage problem, all using file storage.

Seven.Summary

This time, we did a good job, that is, the map function could not be solved. If it was solved, it would be relatively perfect. At the beginning, only one Freud was used, although the Freud algorithm has been able to solve the problem. But later, a Dijkstra algorithm was added specifically, which made the individual more satisfied. However, there were many difficulties in thinking about this problem. At first, I wanted to use a struct to solve it directly, but I couldn’t contact the graph I just learned, so I only used three graphs to solve it. The only problem was that I couldn’t use the map to correspond and then eliminate it Cities are not very friendly to new cities, and there is a bit of trouble when adding city routes. You need to write a special function to add in the file, which is relatively complex.

Eight.Crew Division

MembernameMembernameCompletionsituation
Programming, Implementation, Document Writing and Summary100%
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值