POJ-2387 Til the Cows Come Home【最短路】

題面:

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

題目大意:

現在有一個n個點的圖,有t個通路,每一行給出一個路徑和它的權值。
求從第1個到第n個點的最短路徑並輸出

大致思路:

Dijkstra裸題,把圖建好跑一遍答案就出來了。但是!!我WA了10次!!
主要願意有:
1.之前寫的模板有一個細節寫錯了,而那個模板沒有經過題目驗證。導致我一直沒有注意到這個問題。
2.可能有重邊的情況,比如 :當輸入爲1 2 9和1 2 10的時候,由於沒有考慮重邊的情況,導致後面的一條邊將前面的一條邊覆蓋,導致結果錯誤。

代碼:

關於優先隊列,我在WA的生無可戀的時候,看別人的博客。看到另外一種優先隊列的寫法,在這裏mark一下

    typedef pair<int,int> pii;
    priority_queue<pii,vector<pii>,greater<pii> > q;
    q.push(make_pair(shortlen[start],start));

然後是我自己的代碼:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
const int maxn=2000+10;
const int INF=1<<30;
int shortlen[maxn];
int graph[maxn][maxn];
bool visit[maxn];
typedef struct Node{
    int dis,v;
    bool operator < (const Node &cmp)const{
        return dis > cmp.dis;
    }
}Node;
void Dijkstra(int start,int numv)
{
    priority_queue<Node> q;
    int value;
    shortlen[start]=0;
    q.push((Node){0,start});
    while(!q.empty())
    {
        Node a=q.top();
        q.pop();
        start=a.v;
        value=shortlen[start];
        if(visit[start])
            continue;
        visit[start]=true;
        for(int i=1;i<=numv;++i){
            if(i==start)
                continue;
            if(!visit[i]&&shortlen[i]>value+graph[start][i]){
                shortlen[i]=graph[start][i]+value;
                q.push((Node){shortlen[i],i});

            }
        }
    }
}
void Init(int num)
{
    for(int i=1;i<=num;++i){
        shortlen[i]=INF;
        visit[i]=false;
        for(int j=1;j<=num;++j)
            graph[i][j]=i==j?0:INF;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t,num,x,y,w;
    while(cin>>t>>num)
    {
        Init(num);
        while(t--)
        {
            cin>>x>>y>>w;
            if(graph[x][y]>w){
                graph[x][y]=w;
                graph[y][x]=w;
            }
        }
        Dijkstra(1,num);
        cout<<shortlen[num]<<endl;
    }
    return 0;
}
發佈了113 篇原創文章 · 獲贊 27 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章