Hdu-2544 最短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

解题思路:

最裸的最短路,不会写的可以参考。。。。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define N 110
#define MAX 0x3f3f3f3f
int num, road, dis[N], map[N][N];
bool visit[N];

void Dijkstra(int start, int end)
{
	int k, temp;
	memset(visit, 0, sizeof(visit));
	for(int i = 1; i <= num; ++i)
		dis[i] = (i == start ? 0 : MAX);
	for(int i = 1; i <= num; ++i)
	{
		temp = MAX;
		for(int j = 1; j <= num; ++j)
		{
			if(!visit[j] && temp > dis[j])
				temp = dis[k = j];
		}
		visit[k] = 1;
		for(int j = 1; j <= num; ++j)
			if(!visit[j] && dis[j] > dis[k] + map[k][j])
				dis[j] = dis[k] + map[k][j];
	}
}

int main()
{
	int a, b, cost;
	while(scanf("%d%d", &num, &road) != EOF)
	{
		if(num == 0 && road == 0) break;
		memset(map, MAX, sizeof(map));
		for(int i = 0; i < road; ++i)
		{
			scanf("%d%d%d", &a, &b, &cost);
			if(cost < map[a][b])
				map[a][b] = map[b][a] = cost;
		}
		Dijkstra(1, num);
		printf("%d\n", dis[num]);
	}
	return 0;
}


你可能感兴趣的:(Hdu-2544 最短路)