POJ2387-Til the Cows Come Home-最短路

POJ2387-Til the Cows Come Home-最短路

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.

Source

USACO 2004 November

也是裸的最短路,就放一下自己的模板

Dijkstra

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#define LL long long
#define ULL unsigned long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
#define N 1010
#define INF 0x3f3f3f3f
#define eps 1e-9
using namespace std;
struct Edge{
    int from,to,dist;
    Edge(int u,int v,int d):from(u),to(v),dist(d){};
};
struct HeapNode{
    int d,u;
    bool operator<(const HeapNode &a) const {
        return d>a.d;
    }
};
struct Dijkstra{
    int n,m;
    vector<Edge> edges;
    vector<int> G[N];
    bool vis[N];//是否永久标号 
    int d[N];//松弛操作 
    int p[N];//最短路中的上一条弧

    void init(int n) {
        this->n=n;
        for(int i=0;i<=n;++i){
            G[i].clear();
        }
        edges.clear();
    }

    void AddEdge(int from,int to,int dist){
        edges.push_back(Edge(from,to,dist));
        m=edges.size();
        G[from].push_back(m-1);
    }

    void dijkstra(int s){
        priority_queue<HeapNode> q;
        for(int i=0;i<=n;++i){
            d[i]=INF;
        }
        d[s]=0;
        mem(vis,0);
        q.push((HeapNode){0,s});
        while(!q.empty()){
            HeapNode x=q.top();
            q.pop();
            int u=x.u;
            if(vis[u]){
                continue;
            }
            vis[u]=true;
            for(int i=0;i<G[u].size();++i){
                Edge &e=edges[G[u][i]];
                if(d[e.to]>d[u]+e.dist){
                    d[e.to]=d[u]+e.dist;
                    p[e.to]=G[u][i];
                    q.push((HeapNode){d[e.to],e.to});
                }
            }
        }
    }
};
Dijkstra Dick;
int main()
{
    ios::sync_with_stdio(false);
    int n,t,u,v,c;
    while(cin>>t>>n){
        Dick.init(n);
        while(t--){
            cin>>u>>v>>c;
            Dick.AddEdge(u,v,c);
            Dick.AddEdge(v,u,c);
        }
        Dick.dijkstra(1);
        cout<<Dick.d[n]<<endl;
    }
    return 0;
}

Bellman-Ford

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#define LL long long
#define ULL unsigned long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
#define N 1010
#define INF 0x3f3f3f3f
#define eps 1e-9
using namespace std;
struct Edge{
    int from,to,dist;
    Edge(int u,int v,int d):from(u),to(v),dist(d){};
};
struct BellmanFord{
    int n,m;
    vector<Edge> edges;
    vector<int> G[N];
    int d[N];//松弛数组
    int pre[N];

    void init(int n) {
        this->n=n;
        for(int i=0;i<=n;++i){
            G[i].clear();
        }
        edges.clear();
    }

    void AddEdge(int from,int to,int dist){
        edges.push_back(Edge(from,to,dist));
        m=edges.size();
        G[from].push_back(m-1);
    }

    bool bellmanford(int s){
        for(int i=0;i<=n;++i){
            d[i]=INF;
        }
        d[s]=0;
        for(int i=1;i<n;++i){
            for(int j=0;j<m;++j){
                d[edges[j].to]=min(d[edges[j].to],d[edges[j].from]+edges[j].dist);
            }
        }
        bool hascycle=false;
        for(int j=0;j<m;++j){
            if(d[edges[j].to]>d[edges[j].from]+edges[j].dist){
                hascycle=true;
                break;
            }
        }
        return hascycle;
    }
};
BellmanFord bilibili; 
int main()
{
    ios::sync_with_stdio(false);
    int t,n,u,v,c;
    while(cin>>t>>n){
        bilibili.init(n);
        while(t--){
            cin>>u>>v>>c;
            bilibili.AddEdge(u,v,c);
            bilibili.AddEdge(v,u,c);
        }
        bilibili.bellmanford(1);
        cout<<bilibili.d[n]<<endl;
    }
    return 0;
}

SPFA

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector> 
#define LL long long
#define ULL unsigned long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
#define N 1010
#define INF 0x3f3f3f3f
#define eps 1e-9
using namespace std;
struct Edge{
    int from,to,dist;
    Edge(int u,int v,int d):from(u),to(v),dist(d){};
};
struct SPFA{
    vector<Edge> edges;
    vector<int> G[N];
    bool inque[N];
    int d[N];
    int n,m;

    void init(int n){
        this->n=n;
        for(int i=0;i<=n;++i){
            G[i].clear();
        }
        edges.clear();
    }

    void AddEdge(int from,int to,int dist){
        edges.push_back(Edge(from,to,dist));
        m=edges.size();
        G[from].push_back(m-1);
    }

    bool spfa(int s){
        queue<pair<int,int> > que;
        memset(inque,0,sizeof(inque));
        memset(d,INF,sizeof(d));
        que.push(make_pair(s,1));
        inque[s]=true;
        d[s]=0;
        while(!que.empty()){
            int temp=que.front().first,cnt=que.front().second;
            if(cnt==n){
                return true;
            }
            que.pop();
            for(int i=0;i<G[temp].size();++i){
                if(d[edges[G[temp][i]].to]>d[edges[G[temp][i]].from]+edges[G[temp][i]].dist){
                    que.push(make_pair(edges[G[temp][i]].to,cnt+1));
                    d[edges[G[temp][i]].to]=d[edges[G[temp][i]].from]+edges[G[temp][i]].dist;
                }
            }
        }
        return false;
    }
};
SPFA SMF;
int main()
{
    ios::sync_with_stdio(false);
    int n,t,u,v,c;
    while(cin>>t>>n){
        SMF.init(n);
        while(t--){
            cin>>u>>v>>c;
            SMF.AddEdge(u,v,c);
            SMF.AddEdge(v,u,c);
        }
        SMF.spfa(1);
        cout<<SMF.d[n]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值