dijskstra算法及其队列优化,spfa,floyd Til the Cows Come Home POJ

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

都是紫书上的写法,留个板子
dijskstra算法

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=9999999;
long long d[1005],v[1005],w[1005][1005];
int main()
{
    memset(w,maxn,sizeof(w));
    memset(v,0,sizeof(v));
    int T,N,a,b,c;
    cin>>T>>N;
    for(int i=1;i<=N;i++)
        d[i]=(i==1?0:maxn);
    for(int i=1;i<=T;i++)
    {
        cin>>a>>b;
        cin>>c;
        if(c<w[a][b])
        w[a][b]=w[b][a]=c;
    }
    for(int i=1;i<=N;i++)
    {
        int x,m=maxn;
        for(int y=1;y<=N;y++)
            if(!v[y]&&d[y]<=m)
            m=d[x=y];
        v[x]=1;
        for(int y=1;y<=N;y++)
            d[y]=min(d[y],d[x]+w[x][y]);
    }
    cout<<d[N];
    return 0;
}

队列优化后

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=2005;
struct Edge
{
    int from,to,dis;
};
struct HeapNode
{
    int d,u;
    bool operator < (const HeapNode& rhs) const
    {
        return d>rhs.d;
    }
};
int m,n;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];//p[maxn];
void init()
{
    for(int i=0;i<n;i++)
        G[i].clear();
    edges.clear();
}
void AddEdge(int from,int to,int dis)
{
    edges.push_back((Edge){from,to,dis});
    int len=edges.size();
    G[from].push_back(len-1);
}
void dijkstra(int s)
{
    priority_queue<HeapNode> Q;
    for(int i=0;i<=n;i++)
        d[i]=INF;
    d[s]=0;
    memset(done,0,sizeof(done));
    Q.push((HeapNode){0,s});
    while(!Q.empty())
    {
        HeapNode x=Q.top();
        Q.pop();
        int u=x.u;
        if(done[u]) continue;
        done[u]=1;
        for(int i=0;i<G[u].size();i++)
        {
            Edge& e=edges[G[u][i]];
            if(d[e.to]>d[u]+e.dis)
            {
                d[e.to]=d[u]+e.dis;
                //p[e.to]=G[u][i];
                Q.push((HeapNode){d[e.to],e.to});
            }
        }
    }
}
int main()
{
    cin>>m>>n;
    int a,b,c;
    init();
    for(int i=0;i<m;i++)
    {
        cin>>a>>b>>c;
        AddEdge(a,b,c);
        AddEdge(b,a,c);
    }
    dijkstra(1);
    cout<<d[n]<<endl;
    return 0;
}

spfa

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
vector<int>G[2005];
int t,n,cnt;
struct Edge
{
    int from,to,dis;
}edges[4005];
int d[2005],inq[2005];
void init()
{
    for(int i=0;i<=n;i++)
        G[i].clear();
    memset(inq,0,sizeof(inq));
}
void Eddedge(int u,int v,int w)
{
    edges[cnt]=(Edge){u,v,w};
    G[u].push_back(cnt++);
}
int spfa()
{
    queue<int>Q;
    Q.push(1);
    inq[1]=0;
    d[1]=0;
    for(int i=2;i<=n;i++)
        d[i]=INF;
    while(!Q.empty())
    {
        int u=Q.front();Q.pop();
        inq[u]=0;
        for(int i=0;i<G[u].size();i++)
        {
            Edge& e=edges[G[u][i]];
            if(d[u]<INF&&d[e.to]>d[u]+e.dis)
            {
                d[e.to]=d[u]+e.dis;
                if(!inq[e.to])
                {
                    Q.push(e.to);
                    inq[e.to]=1;
                }
            }
        }
    }
}
int main()
{
    cin>>t>>n;
    int u,v,w;
    init();
    cnt=0;
    for(int i=0;i<t;i++)
    {
        cin>>u>>v>>w;
        Eddedge(u,v,w);
        Eddedge(v,u,w);
    }
    spfa();
    cout<<d[n]<<endl;
    return 0;
}

floyd(本题会tle。。单纯写一下。。)

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int t,n;
int d[2005][2005];
int main()
{
    cin>>t>>n;
    int u,v,w;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        d[i][j]=i==j?0:INF;
    for(int i=0;i<t;i++)
    {
        cin>>u>>v>>w;
        d[u][v]=w;
        d[v][u]=w;
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        if(d[i][k]<INF&&d[k][j]<INF)
        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    cout<<d[1][n]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/acagain/p/9180723.html