POJ 2387-Til the Cows Come Home(Dijkstra+堆優化)

kewlgrl發表於2016-03-30

Til the Cows Come Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40039   Accepted: 13620

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


題目意思:

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


兩種解題思路:

①Dijkstra;

②使用STL的priority_queue實現。


/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:dijkstra.cpp
* 作    者:單昕昕
* 完成日期:2016年3月30日
* 版 本 號:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define INF 0xfffffff//0X代表16進位制,後面是數字,十進位制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],n,t;
bool used[MAXN];//標識是否使用過
void dijkstra(int s)
{
    memset(used,0,sizeof(used));
    fill(dis,dis+n+1,INF);
    fill(used,used+n+1,false);
    dis[s]=0;
    while(true)
    {
        int v=-1;
        //從未使用過的頂點中選擇一個距離最小的頂點
        for(int u=0; u<n; ++u)
            if(!used[u]&&(v==-1||dis[u]<dis[v]))
                v=u;
        if(v==-1) break;
        used[v]=true;
        for(int u=0; u<=n; ++u)
            dis[u]=min(dis[u],dis[v]+cost[v][u]);
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=0; i<=n; ++i)
            for(int j=0; j<=n; ++j)
                cost[i][j]=INF;//手動初始化,不能fill了……
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);
            if(l<cost[a][b])
                cost[a][b]=cost[b][a]=l;//無向圖
        }
        dijkstra(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/


②使用STL的priority_queue實現。

記憶體佔用少:

/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:queue.cpp
* 作    者:單昕昕
* 完成日期:2016年6月1日
* 版 本 號:v2.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0xfffffff//0X代表16進位制,後面是數字,十進位制是4294967295
#define MAXN 1010
using namespace std;
int dis[MAXN],n,t;
struct edge
{
    int to;
    int cost;
};
vector<edge> G[MAXN];//邊表
typedef pair<int,int> P;
//first是最短距離,second是頂點的編號
void dijkstra(int s)
{
    //通過指定greater<P>引數,堆按照first從大到小的順序取初值
    priority_queue<P,vector<P>,greater<P> > que;
    fill(dis,dis+n+1,INF);//初始化範圍要到n
    dis[s]=0;
    que.push(P(0,s));
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(dis[v]<p.first) continue;
        for(int i=0; i<G[v].size(); ++i)
        {
            edge e=G[v][i];
            if(dis[e.to]>dis[v]+e.cost)//Dijkstra
            {
                dis[e.to]=dis[v]+e.cost;
                que.push(P(dis[e.to],e.to));
            }
        }
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);//a->b的距離是l
            G[a].push_back({b,l});//這裡的語法要注意
            G[b].push_back({a,l});
        }
        dijkstra(1);//起點是1
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/


/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:dijkstra.cpp
* 作    者:單昕昕
* 完成日期:2016年3月30日
* 版 本 號:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0xfffffff//0X代表16進位制,後面是數字,十進位制是4294967295
using namespace std;
int p[1003][1003],flag[1003],dis[1003],mmin,n,t;
void dijkstra()
{
    memset(flag,0,sizeof(flag));
    for(int i=2; i<=n; i++)
    {
        dis[i]=p[1][i];
    }
    flag[1]=1;
    int pre;
    for(int i=1; i<=n; i++)
    {
        mmin=inf;
        for(int j=1; j<=n; j++)
        {
            if(flag[j]==0&&dis[j]<mmin)
            {
                mmin=dis[j];
                pre=j;
            }
        }
        if(mmin==inf)
            break;
        flag[pre]=1;
        for(int j=1; j<=n; j++) //找最短路
        {
            if(flag[j]==0&&dis[pre]+p[pre][j]<dis[j])
            {
                dis[j]=dis[pre]+p[pre][j];
            }
        }
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                p[i][j]=inf;
            }
        }
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);
            if(l<p[a][b])
            {
                p[a][b]=p[b][a]=l;//無向圖
            }
        }
        dijkstra();
        printf("%d\n",dis[n]);
    }
    return 0;
}



相關文章