题干:

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.

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.

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.

解题报告:

    单源最短路裸题。

AC代码: 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
int vis[MAX];
int maze[1005][1005];
int n,m;
int dis[MAX];//表示从出发点开始到该点的最短距离。 

void Dijkstra(int u,int v) {
	dis[u] = 0;
//	vis[u] = 1;		//这步还真不能加上。。。。。 
	int minw = INF,minv;
	for(int k = 1;k<= n;k++) {
		minw = INF;
		for(int i = 1; i<=n; i++) {
			if(vis[i] ) continue;
			if(dis[i] < minw) {
				minv = i; minw = dis[i];
			}
		}
		vis[minv] = 1;
		if(minv == v) break;
		for(int i = 1; i<=n; i++) {
			if(vis[i]) continue;
			if(ma***v][i] == 0) continue;
			dis[i] = min(dis[i], dis[minv] + ma***v][i]);
		}
	}
	printf("%d\n",dis[v]);
	
} 

int main()
{
	int a,b,w;
	while(~scanf("%d %d",&m,&n) ) {
		memset(dis,0x3f3f3f3f,sizeof(dis) ) ;
		memset(maze,0x3f3f3f3f,sizeof(maze) );
		memset(vis,0,sizeof(vis) ) ;
		for(int i = 1; i<=m; i++) {
			scanf("%d%d%d",&a,&b,&w);
			if(w<maze[a][b])
			maze[a][b] = maze[b][a] = w;
		}
		Dijkstra(1,n); 
	}
	
	return 0 ;
 } 

AC代码2:(Bellman-Ford算法)

//*bellman算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 2010
#define MAX 99999999
using namespace std ;
struct node {
	int a , b , w ;
} edge[N];
int n , m ;
void bell() {
	int i , j ;
	int  d[N];
	for(int i =1 ; i<=n; i++) { //*距离初始化为无穷;
		d[i]=MAX;
	}
	d[1]=0;//*初始地点为0;
	for(i=1; i<=n; i++) {
		for(j=1; j<=m; j++) { //*按点-边搜,顺便解决了重边问题;
			if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;
			if(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;
		}
	}
	printf("%d\n",d[n]);
}
int main() {
	int i , a   , b ,c;
	while(cin>>m>>n) {
		for(int i =1 ; i<=m; i++) { //*结构体存边和权
			cin>>a>>b>>c;
			edge[i].a=a;
			edge[i].b=b;
			edge[i].w=c;
		}
		bell();
	}
	return 0 ;
}

 

 总结:

    别忘处理一下重边!