Returning Home

The farmer wants to return home, Farm No. 1 from Farm No. N. The farmer who is very tired is going to return home as fast as he can.
In the farms he cultivates, there is the total number of T of two-way roads. Create a program that helps the tired farmer find the shortest distance from Farm No. N to Farm No. 1 Home.

Time limit: 1 second (java: 2 seconds)

Input format


Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 20) are given in a row.
The first row of each test is given by being separated as the spaces which are T, the number of two-way roads and N, the number of farm. (1 ≤ T ≤ 10,000, 1 ≤ N ≤ 1,000)
S, E, & D which are the information of roads are given from the second row to T number rows. It means that Farm No. S and Farm No. E has a connected road and its road length is D (1 ≤ D ≤ 100).

Output format

Output the shortest distance to move from Farm No. N to Farm No. 1 Home on the first row per each test case.

Example of Input

2
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
10 2
1 2 68
1 2 66
1 2 64
1 2 62
1 2 60
1 2 58
1 2 56
1 2 54
1 2 52
1 2 50

Example of output

90

50


Hint

Dijkstra


#include <stdio.h>
#define UNCONNECT 1000000

int road_map[1001][1001];
int min_dist[1001];

int get_min_dist(int farm_num)
{
	int i, j, k;
	int walked[1001];
	int min;
	int u;
	
	memset(walked, 0, sizeof(int)*1001);
	
	for(i=1; i<=farm_num; i++)
	{
		if(road_map[1][i]!=UNCONNECT && i!=1)
		{
			min_dist[i] = road_map[1][i];
		}
		else
			min_dist[i] = UNCONNECT;
	}
	
	walked[1] = 1;
	for(i=1; i<=farm_num; i++)
	{
		min = UNCONNECT;
		for(j=1; j<=farm_num; j++)
		{
			if(0==walked[j] && min_dist[j]< min)
			{
				min = min_dist[j];
				u = j;
			}
		}
		
		walked[u] = 1;		
		for(k=1; k<=farm_num; k++)
		{
			if(min_dist[k]==UNCONNECT)
			{
				if(road_map[u][k]!=UNCONNECT && !walked[k])
					min_dist[k] = min_dist[u] + road_map[u][k];
			}
			else
			{
				if(road_map[u][k]!=UNCONNECT && !walked[k] && (road_map[u][k] + min_dist[u] < min_dist[k]))
					min_dist[k] = min_dist[u] + road_map[u][k];
			}
		}
	}
}

int main(void)
{
	int tc, T;
	int road_num, farm_num;
	int start, end, dist;
	int i, j;
	
	//freopen("input.txt", "r", stdin);

	setbuf(stdout, NULL);

	scanf("%d", &T);
	for(tc = 0; tc < T; tc++)
	{
		scanf("%d %d", &road_num, &farm_num);
		//printf("road:%d-farm:%d\n", road_num, farm_num);
		
		for(i=1; i<=farm_num; i++)
			for(j=1; j<=farm_num; j++)
				road_map[i][j] = UNCONNECT;
		
		for(i=0; i<road_num; i++)
		{
			scanf("%d %d %d", &start, &end, &dist);
			if(dist < road_map[start][end])
			{
				road_map[start][end] = dist;
				road_map[end][start] = dist;
			}
		}
		
		/*for(i=1; i<=farm_num; i++)
		{
			for(j=1; j<=farm_num; j++)
				printf("%d ", road_map[i][j]);
			printf("\n");
		}*/
		
		get_min_dist(farm_num);
		printf("%d\n", min_dist[farm_num]);
		/**********************************
		*  Implement your algorithm here. *
		***********************************/
		
		// Print the answer to standard output(screen).
		
	}

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值