POJ2387-Til the Cows Come Home

来源:互联网 发布:f1 2015知乎 编辑:程序博客网 时间:2024/06/01 19:46

POJ2387-Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55360 Accepted: 18793

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 51 2 202 3 303 4 204 5 201 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还能用斐波那契堆优化的,不过我还没学会,日后补齐吧

AC代码

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-9using 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});                }            }        }    }    void dijkstraNoOptimize(int s){        mem(vis,0);        for(int i=0;i<=n;++i){            d[i]=INF;        }        d[s]=0;        int mn,pos;        for(int i=1;i<=n;++i){            mn=INF;            for(int j=0;j<n;++j){                if(!vis[j]&&d[j]<mn){                    pos=j;                    mn=d[j];                }            }            for(int j=0;j<G[pos].size();++j){                Edge &e=edges[G[pos][j]];                if(d[e.to]>d[pos]+e.dist){                    d[e.to]=d[pos]+e.dist;                    p[e.to]=G[pos][j];                }            }            vis[pos]=true;        }    }};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.dijkstraNoOptimize(1);        /*for(int i=0;i<=n;++i){            cout<<Dick.d[i]<<' ';        }*/        cout<<Dick.d[n]<<endl;    }    return 0;}

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-9using 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);        /*for(int i=0;i<=n;++i){            cout<<Dick.d[i]<<' ';        }*/        cout<<Dick.d[n]<<endl;    }    return 0;}

Floyd(这个时间复杂度是不能过的,但是顺手写了就放一下,交的结果是TLE)

#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-9using namespace std;int mp[N][N];void Floyd(int n){    for(int i=1;i<=n;++i){        for(int j=1;j<=n;++j){            for(int k=1;k<=n;++k){                mp[j][k]=min(mp[j][k],mp[j][i]+mp[i][k]);            }        }    }    return;}void init(int n){    for(int i=1;i<=n;++i){        for(int j=1;j<=n;++j){            mp[i][j]=INF;        }    }}int main(){    ios::sync_with_stdio(false);    int n,t,u,v,c;    while(cin>>t>>n){        init(n);        while(t--){            cin>>u>>v>>c;            mp[u][v]=mp[v][u]=min(mp[u][v],c);        }        Floyd(n);        cout<<mp[1][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-9using 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-9using 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;}
原创粉丝点击