题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离
poj2387
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
解法一:(dijkstra算法)(PS:2016.3.22修改自己写的版本)
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #define MAX 9999999
- using namespace std ;
- int u , v ,n, dis[1111],vis[1111],ma[1111][1111];
- void dijk()
- {
- int k , mini;
- for(int i = 1 ; i <=v;i++)
- {
- dis[i]=ma[1][i];
- }
- for(int i = 1 ;i<=v;i++)
- {
- mini=MAX;
- for(int j = 1 ; j<=v;j++)
- {
- if(!vis[j]&&dis[j]<mini)
- {
- mini=dis[j];
- k=j;
- }
- }
- vis[k]=1;
- for(int j=1 ;j<=v;j++)
- {
- if(dis[j]>dis[k]+ma[k][j])
- {
- dis[j]=dis[k]+ma[k][j];
- }
- }
- }
- }
- int main()
- {
- while(cin>>u>>v)
- {
- n=0;
- for(int i = 0 ; i <=v;i++)
- {
- for(int j = 0 ; j <=v;j++)
- {
- ma[i][j]=MAX;
- }
- ma[i][i]=0;
- vis[i]=0;
- dis[i]=MAX;
- }
- for(int i = 1 ;i<=u;i++)
- {
- int a , b , len;
- cin>>a>>b>>len;
- n=max(max(n,a),b);
- if(ma[a][b]>len)
- {
- ma[a][b]=ma[b][a]=len;
- }
- }
- dijk();
- printf("%d ",dis[v]);
- }
- return 0 ;
- }
解法二(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 ",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 ;
- }
方法三(Floyd-Warshall):虽然过不去数据,因为太大;但是值得一试;
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <algorithm>
- #include <cstring>
- #define N 2000
- #define MAX 99999999
- using namespace std ;
- int u , v ;
- int dis[N][N];
- void warsh() {
- int i , j , k ;
- for(k=1; k<=v; k++) {
- for(i=1; i<=v; i++) {
- for(j=1; j<=v; j++) {
- dis[i][j]=min(dis[i][j],dis[k][j]+dis[i][k]);
- }
- }
- }
- }
- int main() {
- cin>>u>>v ;
- int a, b , c ;
- for(int i = 1 ; i <= v ; i++) {
- for(int j = 1 ; j <=v; j++) {
- dis[i][j]=MAX;
- }
- }
- for(int i = 0 ; i < v ; i++) {
- dis[i][i]=0;
- }
- for(int i = 1 ; i <=u ; i++) {
- cin>>a>>b>>c;
- dis[a][b]=dis[b][a]=c;
- }
- warsh();
- cout<<dis[1][v]<<endl;
- return 0 ;
- }