dijstra算法

#include<iostream>
using namespace std;
#define maxvalue 100000;

class link   //后结点
{
public:
	int end;
	int len;
	link *next;
};
class head   //头结点
{
public:

	link* first = NULL;
};


class graphl  //图的邻接表类定义
{
public:
	int num;
	head * list = new head[num];
	graphl(int n, int** input)  //构造函数
	{
		for (int i = 0; i < n; i++)
		{
			list[i].first = NULL;
		}
		num = n;
		list = new head[n];
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (input[i][j] != 0 && input[i][j] != 100000)
				{
					setedge(i, j, input[i][j]);
				}
			}
		}
	}
	void setedge(int from, int to, int weight)  //添加边
	{
		if (list[from].first == NULL)
		{
			link *tmp = new link;
			tmp->end = to;
			tmp->len = weight;
			tmp->next = NULL;
			list[from].first = tmp;
		}
		else
		{
			link* tmp1 = new link;
			tmp1 = list[from].first;
			while (tmp1->next != NULL)
			{
				tmp1 = tmp1->next;
			}
			link* new1 = new link;
			new1->len = weight;
			new1->end = to;
			new1->next = NULL;
			tmp1->next = new1;
		}
	}
	int weight(int from, int to,int **input)  //返回某两点之间边的权值
	{
		if (input[from][to] == 0)
		{
			return maxvalue;
		}
		else
		{
			return input[from][to];
		}
	}
};

void dijkstra(graphl& g, int *dist, int *pre, int s, int n,int** input);
int main()
{
	int n = 6;        //结点数
	cout << "input n" << endl;
	 /*cin >> n;*/
	
	int** input = NULL;  //构造邻接矩阵
	input = new int*[n];
	for (int i = 0; i < n; i++)
	{
		input[i] = new int[n];
		
	}
	cout << input;
	input[0][0] = 0;
	input[0][1] = 20;
	input[0][2] = 60;
	input[0][3]=100000;
	input[0][4]=10;
	input[0][5]=65;
	input[1][0]=100000;
	input[1][1]=0;
	input[1][2]=30;
	input[1][3]=70;
	input[1][4]=100000;
	input[1][5]=100000;
	input[2][0]=100000;
	input[2][1]=100000;
	input[2][2]=0;
	input[2][3]=40;
	input[2][4]=100000;
	input[2][5]=100000;
	input[3][0]=100000;
	input[3][1]=100000;
	input[3][2]=100000;
	input[3][3]=0;
	input[3][4]=35;
	input[3][5]=100000;
	input[4][0]=100000;
	input[4][1]=100000;
	input[4][2]=100000;
	input[4][3]=100000;
	input[4][4]=0;
	input[4][5]=20;
	input[5][0]=100000;
	input[5][1]=100000;
	input[5][2]=15;
	input[5][3]=10;
	input[5][4]=100000;
	input[5][5]=0;
	
	//for (int i = 0; i < n; i++)  //输入邻接矩阵
	//{
	//	for (int j = 0; j < n; j++)
	//	{
	//		cin >> input[i][j];
	//	}
	//	cout << endl;
	//}
	for (int i = 0; i < n; i++)  //输chu邻接矩阵
	{
		for (int j = 0; j < n; j++)
		{
			cout << input[i][j] << endl;
		}
		cout << endl;
	}
	graphl g(n, input);  //生成图g

	//test
	for (int i = 0; i < n; i++)
	{
		cout << i << " ";
		if (g.list[i].first == NULL)
		{
			break;
		}
		if (g.list[i].first != NULL)
		{
			cout << g.list[i].first->end;
			cout << " ";
			link * tmp = new link;
			tmp = g.list[i].first;
			while (tmp->next != NULL)
			{
				cout << tmp->next->end;
				tmp = tmp->next;
			}
		}
		cout << endl;
	}

	cout << endl;
	int* dist = new int[n];
	int* pre = new int[n];

	cout << endl;
	dijkstra(g, dist, pre, 0, n,input);			//开始dijkstra算法

	return 0;
}
//dijkstra算法
void dijkstra(graphl& g, int *dist, int *pre, int s, int n,int** input)
{
	int i;
	int *S = new int[n];
	for (i = 0; i < n; i++)
	{
		S[i] = 0;
		if (i != s && g.weight(s, i,input) == 0)
		{
			dist[i] = maxvalue;
		}
		else
		{
			dist[i] = g.weight(s, i,input);
		}
		if (i != s && dist[i] < 100000)
		{
			pre[i] = s;
		}
		else
			pre[i] = -1;
	}
	S[s] = 1;
	int min, v;
	for (i = 0; i < n - 1; i++)
	{
		min = maxvalue;
		v = s;
		for (int j = 0; j < n; j++)
		{
			if (S[j] == 0 && dist[j] < min)
			{
				v = j;
				min = dist[j];
			}
		}
		S[v] = 1;
		for (int j = 0; j < n; j++)
		{
			if (S[j] == 0 && g.weight(v, j,input) != 0 && dist[v] + g.weight(v, j,input) < dist[j])
			{
				dist[j] = dist[v] + g.weight(v, j,input);
				pre[j] = v;
			}
		}
	}
	cout << "结果开始输出了:" << endl;
	for (int i = 1; i < n; i++)
	{
		int j=i;
		cout << i << " ";
		/*cout << pre[i];*/
		while (1)
		{
			cout << pre[j]<<" ";
			if (pre[j] == -1)
			{
				cout << "这根本过不去啊";
				break;
			}
			if (pre[j] == 0)
				break;
			else
				j = pre[j];
		}
		cout << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值