Description
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Line 1: Two integers: T and N
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
大意就是要你找出从1到第n个节点的最短距离,很容易就想到用djst.
*
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
大意就是要你找出从1到第n个节点的最短距离,很容易就想到用djst.
* =====================================================================================
*
* Filename: acm.c
*
* Description:
*
* Version: 1.0
* Created: 2015年03月23日 17时41分32秒
* Revision: none
* Compiler: gcc
*
* Author: Dr. Fritz Mehner (fgm), mehner.fritz@fh-swf.de
* Organization: FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/
//path for recording what is used and record
#include<stdio.h>
#define MAX 0x3fffffff
#define MAX_SIZE 2005
int path[MAX_SIZE] = {0};
int dist[MAX_SIZE];
struct fun
{
int v;//顶点
int e;//边
int p[MAX_SIZE][MAX_SIZE];
}page;
void init ()
{
int i = 0, j;
for(i = 1; i <= page.v;i++)
{
page.p[i][i] = 0;
for(j = 1; j<i; j++)
{
page.p[j][i] = page.p[i][j] = MAX;
}
dist[i] = MAX;
}
}
void djst()
{
int min;
int i, j, k, t =1;
for(i = 1; i <=page.v; i++ )
{
dist[i] = page.p[1][i];//记录各条最小路长度
}
path[1] = 1;//表示i = 0 这个节点已经选过
for(i = 2; i<=page.v; i++)//寻找各条最短路径
{
min = MAX;
for(j = 1; j<= page.v; j++)//选择最小权值路径
if(!path[j]&&dist[j] < min)//选择没选的点中最小点,更新最小值
{
k = j;
min = dist[j];
}
if(min == MAX)
{
printf("%d\n", dist[page.v]);
return;
}
path[k]= 1;//record k to be choosed
for(j = 1; j <= page.v; j++)//修改路径
{
if(!path[j]&&page.p[k][j] < MAX && dist[k]+page.p[k][j] < dist[j])//do not record the number that has thought;
dist[j] = dist[k] + page.p[k][j];//alter the distance
}
}
printf("%d\n", dist[page.v]);
}
int main()
{
int x, y, value;
int i, j;
while(scanf("%d%d", &page.e, &page.v) != EOF)
{
// printf("sdfasf\n");
init();
for(i = 1; i <= page.e; i++)
{
scanf("%d%d%d",&x, &y, &value);
if(page.p[y][x] > value)
page.p[y][x] = page.p[x][y] = value;
}
djst();
}
return 0;
}
顺便证明一下djst的贪心可行性:djst时,总是选当前已选过所有节点可以到的所有没选过的节点,选出最小的那一个节点,当选择的当前节点为权值最小的时候,那么后面不管选了其他的节点,图再怎么变化都不会改变当前路径为源点到该点最短距离的事实,因为在选择该路径时其它路径都不如这个路径,那么当你下次选择其他点时不是要已当前点位为前提条件,那么就是以选择比当前更长路径更长的其它路径作为前提条件,那么如果以当前为前提肯定要先选此路,如果选其他路径,那么路径在没到达该点时就已经超过了该点的当前选择了.
此博客证明纯属个人兴趣,如果有问题还请大神指正.