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.
 
题意:
  一个姑娘想要从 1 点尽量快的到达 N 点,输入节点的总个数,以及每个节点之间的距离,输出她最少要走的路程。【单源最短路问题】
代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define MAX 1010
using namespace std;
typedef pair<int ,int> Pair;
struct Node{ int to,cost; };
int Vex,Edge;
vector<Node> graph[MAX];
int dis[MAX]; //存放节点1 到 节点 i 的最短距离
void Dijkstra(int x)
{
    //优先队列默认按从小到大的顺序,结构体的第一个元素作为判断优先值的条件
    priority_queue<Pair,vector<Pair> ,greater<Pair> > que;
    fill(dis,dis+Vex+2,INF);            //将存放距离的数组初始化为INF
    dis[x] = 0;
    que.push(Pair(0,x)); //将第一个节点的权值和节点标号加入优先队列
    while(!que.empty()){
        Pair p = que.top();
        que.pop();
        int v = p.second;
        if(dis[v] < p.first) continue; //一个小小的优化
        for(int i = 0;i < graph[v].size();i++){
            Node node = graph[v][i];
            if(dis[node.to] > dis[v]+node.cost){
                dis[node.to] = dis[v] + node.cost;
                que.push(Pair(dis[node.to],node.to));
            }
        }
    }
}


int main()
{
int from,to,value; Node tmp_node; scanf("%d%d",&Edge,&Vex); for(int i = 0;i < Edge;i++){ scanf("%d%d%d",&from,&to,&value); tmp_node.to = to; tmp_node.cost = value; graph[from].push_back(tmp_node); tmp_node.to = from; graph[to].push_back(tmp_node); } // for(int i = 1;i <= Vex;i++){ // printf("%d ",i); // for(int j = 0;j < graph[i].size();j++){ // printf("%d:%d->",graph[i][j].to,graph[i][j].cost); // } // printf("\n"); // } Dijkstra(1); //起 printf("%d\n",dis[Vex]); return 0; }

 


 
 关于该部分的优化:
   if(dis[v] < p.first) continue; //一个小小的优化

 

 
  
  
 
posted @ 2017-04-18 09:56  渣渣技术狗  阅读(53)  评论(0)    收藏  举报