1. 程式人生 > >POJ 2387 圖論之最短路【三種寫法】

POJ 2387 圖論之最短路【三種寫法】

最短路問題;

坑點1:是先輸入邊,再輸入點;

坑點2:資料很大,不適合用別的模板;

坑點3:有重邊 需要判定;

題意:題目大意:有N個點,給出從a點到b點的距離,當然a和b是互相可以抵達的,問從1到n的最短距離

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.

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程式碼:

解法一:(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\n",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\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 ;
}

方法三(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 ;
}