HDOJ 1874 畅通工程续 (最短路)

题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~


就是个最短路的问题又wa了ˋ^ˊ... 要判重边不说了,居然测试中有自己到自己的路ㄟ(▔,▔)ㄏ 初始化时要弄成0或s == t时输出0。


code:

#include <stdio.h>
#define Max 0x7fffffff
int map[202][202], dis[202], used[202], n = 0, m = 0, s = 0, t = 0;
void dijkstar()
{
	int i = 0, j = 0, k = 0, min = 0;
	used[s] = 1;
	for(i = 0; i<n; i++)
		dis[i] = map[s][i];
	for(i = 0; i<n-1; i++)
	{
		min = Max;
		for(j = 0; j<n; j++)
			if(!used[j] && min>dis[j])
			{
				k = j;
				min = dis[j];
			}
		used[k] = 1;
		for(j = 0; j<n; j++)
		if(!used[j] && map[k][j] != Max)
		{
			if(dis[j]>dis[k]+map[k][j])
			dis[j] = dis[k]+map[k][j];
		}
	}
}
int main()
{
	int i = 0, j = 0, a = 0, b = 0, time = 0;
	while(scanf("%d %d",&n,&m) != EOF)
	{
		for(i = 0; i<n; i++)
		{
			for(j = 0; j<n; j++)
					map[i][j] = Max;
			dis[i] =	 Max;
			used[i] = 0;
		}
		while(m--)
		{
			scanf("%d %d %d",&a,&b,&time);
			if(map[a][b]>time)
				map[a][b] = map[b][a] = time;
		}
		scanf("%d %d",&s,&t);
		if(s == t)
			printf("0\n");
		else
		{
			dijkstar();
			if(dis[t] != Max)
				printf("%d\n",dis[t]);
			else
				printf("-1\n");
		}
	}
	return 0;
}




你可能感兴趣的:(测试)