Dijkstra-POJ-2387-Til the Cows Come Home

Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36731 Accepted: 12512
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.

水题,直接用dijikstra求第N个点的单元最短路径,然后再输出第一个点到第N个点的最短路径距离便可。
但是要注意的是数据输入时的判断,可能会有相同两点多条路径,一定要保留最短的那条的数据。

//
//  main.cpp
//  最短路练习-A-Til The Cows Come Home
//
//  Created by 袁子涵 on 15/9/30.
//  Copyright (c) 2015年 袁子涵. All rights reserved.
//

#include 
#include 
#include 
#include 

#define Max 10000001
#define minof(a,b) ((a)>(b)?(b):(a))

using namespace std;

bool book[1010];
int T,N;
long long int dis[1010][1010],length[1010],mins;

int main(int argc, const char * argv[]) {
    long long int a,b,c,d=0;
    cin >> T >> N;
    memset(book, 0, sizeof(book));
    for (int i=1; i<=N; i++) {
        for (int j=1; j<=N; j++)
            dis[i][j]=Max;
        dis[i][i]=Max;
    }
    for (int i=1; i<=T; i++) {
        cin >> a >> b >> c;
        dis[a][b]=dis[b][a]=minof(dis[a][b],c);
    }
    for (int i=1; i<=N; i++)
        length[i]=dis[i][N];
    book[N]=1;
    for (int i=1; i<=N; i++) {
        mins=Max;
        for (int j=1; j<=N; j++)
            if (book[j]==0 && length[j]1;
        for (int j=1; j<=N; j++)
            if (book[j]==0 && length[j]>mins + dis[d][j])
                length[j]=mins + dis[d][j];
    }
    cout << length[1] << endl;
    return 0;
}

近期为了熟悉模板,又做了一发这道题,分别用了邻接矩阵与邻接表优先队列两种方法,后者明显优化了不少。


//
//  main.cpp
//  POJ-2387-Til the Cows Come Home
//
//  Created by 袁子涵 on 15/11/25.
//  Copyright © 2015年 袁子涵. All rights reserved.
//
//普通 94ms   4620KB
//优先队列47ms 756KB

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN 1010
#define typec long long int
#define INF 0x3f3f3f3f

using namespace std;
int T,N;
//bool vis[MAXN];
//int cost[MAXN][MAXN],lowcost[MAXN];
//void Dijkstra(int beg)
//{
//    for (int i=0; i
//        lowcost[i]=INF;
//        vis[i]=0;
//    }
//    lowcost[beg]=0;
//    for (int j=0; j
//        int k=-1;
//        long long int Min=INF;
//        for (int i=0; i
//            if (!vis[i] && lowcost[i]
//                Min=lowcost[i];
//                k=i;
//            }
//        }
//        if (k==-1) {
//            break;
//        }
//        vis[k]=1;
//        for (int i=0; i
//            if (!vis[i] && lowcost[k]+cost[k][i]
//                lowcost[i]=lowcost[k]+cost[k][i];
//            }
//        }
//    }
//}
struct qnode
{
    int v,c;
    qnode(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};
struct Edge
{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vectorE[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int start)
{
    memset(vis, 0, sizeof(vis));
    for (int i=1; i<=N; i++)
        dist[i]=INF;
    priority_queueque;
    while (!que.empty())
        que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while (!que.empty()) {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if (vis[u])
            continue;
        vis[u]=1;
        for (int i=0; iint v=E[tmp.v][i].v;
            int cost=E[u][i].cost;
            if (!vis[v]&&dist[v]>dist[u]+cost) {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}

int main(int argc, const char * argv[]) {
    int a,b,c;
    scanf("%d%d",&T,&N);
    for (int i=0; i<=N+1; i++)
        E[i].clear();
    for (int i=1; i<=T; i++) {
        scanf("%d%d%d",&a,&b,&c);
        addedge(a,b,c);
        addedge(b, a, c);
    }
    Dijkstra(N);
    printf("%d\n",dist[1]);
    return 0;
}

你可能感兴趣的:(C练习,最短路)