最短路问题模板题POJ2387

来源:互联网 发布:牛股宝 平果软件下载 编辑:程序博客网 时间:2024/06/06 02:50
   对于最短路问题,经常用的两种算法必然是dijkstra算法和SPFA算法,SPFA算法虽然可以解决负权值问题和不需要考虑重边,但是其存在时间不稳定性,所以在单纯的无负权值问题解决方面是不如dijkstra算法的。
下面用POJ2378来介绍两种算法的模板,一般题型可以直接套用。
  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.
dijkstraac模板
#include <stdio.h>#include <string.h>#include <string>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;const int inf=1<<29;int map[1010][1010];//map[i][j]表示从i-->j的距离  int dist[1010];//dist[i]从v1到i的距离  int vis[1010];//标记有没有被访问过  void dijkstra(int n){    int k,min;    for(int i=1; i<=n; i++)    {        dist[i]=map[1][i];        vis[i]=0;    }    for(int i=1; i<=n; i++)//遍历顶点      {        k=0;        min=inf;        for(int j=1; j<=n; j++)            if(vis[j]==0&&dist[j]<min)            {                min=dist[j];                k=j;            }        vis[k]=1;        for(int j=1; j<=n; j++)            if(vis[j]==0&&dist[k]+map[k][j]<dist[j])                dist[j]=dist[k]+map[k][j];//如果找到了通路就加上     }    return;}int main(){    int t,n,a,b,w;    while(~scanf("%d%d",&t,&n))    {        mem(map,0);        mem(vis,0);        mem(dist,0);        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)                map[i][j]=inf;//初始化为无穷大          for(int i=1; i<=t; i++)        {            scanf("%d%d%d",&a,&b,&w);            if(w<map[a][b])            {                map[a][b]=w;                map[b][a]=map[a][b];//建立无向图            }//这里是判断是否有重边,应为两点之间的路,未必只有一条。        }        dijkstra(n);        printf("%d\n",dist[n]);    }    return 0;}
SPFA模板
#include <iostream>#include <queue>#include<cstdio> using namespace std;#define inf 0x3f3f3f3f#define MAXM 4005#define MAXV 1005typedef struct{int a,b,w,next;}Edge;Edge edge[MAXM];int n,m,headlist[MAXV];void spfa(){int i,d[MAXV],v,vis[MAXV];queue <int>q;for(i=2;i<=n;i++){d[i]=inf;vis[i]=0;}d[1]=0;vis[1]=1;q.push(1);while(!q.empty()){v=q.front();q.pop();vis[v]=0;for(i=headlist[v];i!=-1;i=edge[i].next)if(d[v]+edge[i].w<d[edge[i].b]){d[edge[i].b]=d[v]+edge[i].w;if(!vis[edge[i].b]){vis[edge[i].b]=1;q.push(edge[i].b);}}}printf("%d\n",d[n]);}int main(){int i,a,b,c;while(~scanf("%d%d",&m,&n)){for(i=1;i<=n;i++) headlist[i]=-1;for(i=1;i<=2*m;i+=2){scanf("%d%d%d",&a,&b,&c);edge[i].a=a;edge[i].b=b;edge[i].w=c;edge[i].next=headlist[a];headlist[a]=i;edge[i+1].a=b;edge[i+1].b=a;edge[i+1].w=c;edge[i+1].next=headlist[b];headlist[b]=i+1;}spfa();}return 0;}