[kuangbin带你飞]专题四 最短路练习 A

Description
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.

http://poj.org/problem?id=2387

题意:

这个人要从n到1,求最短路。。。

tip:

dijstra。。。当最小堆是1的时候。。。直接return。。。不用求出所以
注意: 两个点可能给多个边。。。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1010;
int M,N,tot;
struct node{
    int v,w,next;
}edges[4*maxn];
typedef pair<int,int>pii;
priority_queue vector ,greater  > pq;
int first[maxn*2],dist[maxn][maxn],dis[maxn];
const int INF = 1<<30;

void add(int u,int v,int w){
    edges[tot].v = v;edges[tot].w = w;edges[tot].next = first[u];first[u] = tot++;
    edges[tot].v = u;edges[tot].w = w;edges[tot].next = first[v];first[v] = tot++;
}

void dij(){
    while(!pq.empty()){
        int ss = pq.top().first,no = pq.top().second;
        if(no == 1) return;
        pq.pop();
        if(dis[no] < ss)
            continue;
        for(int i = first[no]; i != -1;i = edges[i].next){
            if(edges[i].w + dis[no] < dis[edges[i].v]){
                dis[edges[i].v] = edges[i].w + dis[no];
                pq.push(make_pair(dis[edges[i].v],edges[i].v));
            }
        }
    }
}

void init(){
    tot = 0;
    memset(first,-1,sizeof(first));
    //memset(vis,false,sizeof(vis));
    memset(dist,-1,sizeof(dist));
    for(int i = 0 ; i < M ; i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        if(dist[u][v] != -1 &&w >= dist[u][v])
            continue;
        add(u,v,w);
        dist[u][v] = dist[v][u] = w;
    }
    while(!pq.empty())  pq.pop();
    for(int i = 1; i <= N ;i++) dis[i] = INF;
    pq.push(make_pair(0,N));
    dis[N] = 0;
}

int main(){
    while(~scanf("%d%d",&M,&N)){
        init();
        dij();
        printf("%d\n",dis[1]);
    }
}

你可能感兴趣的:(acm,带你飞系列,基本算法)