您的位置:首页 > 其它

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

2016-11-29 23:18 267 查看
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 <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <deque>
#include <utility>
#include <functional>
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 <pii,vector<pii> ,greater <pii> > 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
= 0;
}

int main(){
while(~scanf("%d%d",&M,&N)){
init();
dij();
printf("%d\n",dis[1]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: