Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
题意:给出了一个无向图的边,求N从1的最短路径。
Dijkstra算法
一、算法过程:
(1)初始时,S只包含源点,即S={v},v的距离dist[v]为0。U包含除v外的其他顶点,U中顶点u距离dis[u]为边上的权值(若v与u有边) )或∞(若u不是v的出边邻接点即没有边<v,u>)。
(2)从U中选取一个距离v(dist[k])最小的顶点k,把k,加入S中(该选定的距离就是v到k的最短路径长度)。
(3)以k为新考虑的中间点,修改U中各顶点的距离;若从源点v到顶点u(u∈ U)的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u的距离值,修改后的距离值的顶点k的距离加上边上的权(即如果dist[k]+w[k,u]<dist[u],那么把dist[u]更新成更短的距离dist[k]+w[k,u])。
(4)重复步骤b和c直到所有顶点都包含在S中。
#include <stdio.h> #include <stdlib.h> #define INF 100000 int dist[2005][2005]; int main() { int T,N,a,b,D,i,j,min,v; int d[1005]; int vis[1005]; scanf("%d%d",&T,&N); for(i=1;i<=N;i++) //初始化边的权值 for(j=1;j<=N;j++) { dist[i][j]=INF; dist[i][i]=0; } while(T--) { scanf("%d%d%d",&a,&b,&D); if(dist[a][b]>D||dist[b][a]>D)//两个顶点之间可能有多条路,选取最短的 { dist[a][b]=D; dist[b][a]=D; } } for(i=1;i<=N;i++) { vis[i]=0; //第i个顶点没有访问过。 d[i]=dist[1][i]; //将第i个结点与1的距离存入dist[]中。 } for(i=1;i<=N;i++)//N次循环,使所有的顶点都更新一遍。 { min=INF; for(j=1;j<=N;j++) if(vis[j]==0 && d[j]<min)//从dist[]选取最小值 { v=j; min=d[j]; } vis[v]=1; for(j=1;j<=N;j++) if(vis[j]==0 && d[j]>dist[v][j]+d[v]) d[j]=dist[v][j]+d[v];//把V当做中间点更新dist[] } printf("%d\n",d[N]); return 0; }