最短路径问题,djkstra算法

Problem C: Shortest Path

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1007   Solved: 348
[ Submit][ Status][ Web Board]

Description

Abrao is a beautiful country. It has N cities (1 ≤ N≤ 2,500) and C (1 ≤ C ≤ 10,000) highways to connect adjacent cities. Each city is assigned with a unique number from 1 to N. You are required to calculate the length of the shortest path from one city to another.

Input

The first line contains four integers N, C, S and T. N and C has been defined above. S and T (1 ≤ S,T≤ 2,500) are the numbers of two cities. The information about the highways is defined in the following C lines, each one of which contains three integers vi,vj and w  indicating a high way between vand vwith the distance equal to w (there may be multiple highways between two cities). You are required to calculate the length of the shortest path from the city S to T. 

Output

The output is the length of the shortest path from the city S to T.

Sample Input

7 11 5 42 4 21 4 37 2 23 4 35 7 57 3 36 1 16 3 42 4 35 6 37 2 1

Sample Output

7
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
#define UNVISITED false
#define VISITED true

class Edge
{
private:
	int destination;
	int weight;
public:
	Edge(int a, int b)
	{
		destination = a;
		weight = b;
	}
	Edge()
	{
		destination = (int)INT_MAX;
		weight = (int)INT_MAX;
	}
	int Getdestination()
	{
		return destination;
	}
	int Getweight()
	{
		return weight;
	}
};

class graph
{
private:
	int numofvertices;
	int numofhighways;
	vector<Edge>* graphvertices;
	bool* mask;
public:
	graph(int numver, int numhigh)
	{
		numofvertices = numver;
		numofhighways = numhigh;
		graphvertices = new vector<Edge>[numver];
		mask = new bool[numver];
		for (int i = 0; i < numver; i++)
		{
			mask[i] = UNVISITED;
		}
	}
	void Insert(int start, int destination, int weight)
	{
		Edge tmp(destination, weight);
		graphvertices[start - 1].push_back(tmp);
	}
	vector<Edge> Getverticelist(int i)
	{
		return graphvertices[i];
	}
	int Getnumofvertices()
	{
		return numofvertices;
	}
	int Getnumofhighways()
	{
		return numofhighways;
	}
	bool Getmask(int i)
	{
		return mask[i];
	}
	void Setmask(int i, bool change)
	{
		mask[i] = change;
	}

};
int Minvertice(graph* G, int* D)
{
	int min = INT_MAX;
	int v = 0;
	for (int i = 0; i < G->Getnumofvertices(); i++)
	{
		if (G->Getmask(i) == UNVISITED&&D[i] < min)
		{
			v = i;
			min = D[i];
		}
	}
	return v;
}
void djkstra(graph* G, int* D, int s)
{
	for (int i = 0; i < G->Getnumofvertices(); i++)
	{
		D[i] = INT_MAX;
	}
	D[s - 1] = 0;
	for (int i = 0; i < G->Getnumofvertices(); i++)
	{
		int v = Minvertice(G, D);
		G->Setmask(v, VISITED);

		for (int i = 0; i < G->Getverticelist(v).size(); i++)
		{

			int w = G->Getverticelist(v).at(i).Getdestination() - 1;
			if (D[w] >(D[v] + G->Getverticelist(v).at(i).Getweight()))
			{
				D[w] = (D[v] + G->Getverticelist(v).at(i).Getweight());

			}
		}

	}
}

int main()
{
	int numcities, numhighway, Start, Terminal;
	cin >> numcities >> numhighway >> Start >> Terminal;

	graph* path = new graph(numcities, numhighway);
	int* D = new int[numcities];
	while (numhighway--)
	{
		int start, destination, weight;
		cin >> start >> destination >> weight;

		path->Insert(start, destination, weight);
		path->Insert(destination, start, weight);
	}
	djkstra(path, D, Start);
	cout << D[Terminal - 1] << endl;
	return 0;
}

经验:尽量使用c++库内包含的数据结构,自己写链表还有队列之类的会很崩溃的,而且浪费时间又做不好。
INT_MAX要引入<limits.h>头文件
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值