1. 程式人生 > >Islands Travel——SPFA求最短路

Islands Travel——SPFA求最短路

又是一道微軟的筆試題,看來最短路徑很重要啊!

時間限制:10000ms   單點時限:1000ms   記憶體限制:256MB

描述

There are N islands on a planet whose coordinates are (X1, Y1), (X2, Y2), (X3, Y3) ..., (XN, YN). You starts at the 1st island (X1, Y1) and your destination is the n-th island (XN, YN). Travelling between i-th and j-th islands will cost you min{|Xi

-Xj|, |Yi-Yj|} (|a| denotes the absolute value of a. min{a, b} denotes the smaller value between a and b) gold coins. You want to know what is the minimum cost to travel from the 1st island to the n-th island.

輸入

Line 1: an integer N.

Line 2~N+1: each line contains two integers Xi and Yi.

For 40% data, N<=1000,0<=Xi

,Yi<=100000.

For 100% data, N<=100000,0<=Xi,Yi<=1000000000.

輸出

Output the minimum cost.

樣例輸入
3
2 2
1 7
7 6
樣例輸出
2

掌握SPFA,這道題就很簡單了,學習連結:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 200000;//陣列要開大些,防止越界
int n , dis[maxn] , isQueue[maxn];
vector < pair < int , int > > graph[maxn];//注意後面兩個尖括號之前有空格!

class nodd
{
public:
    int x , y , num;
    //過載"<"符號
    bool operator < (const nodd  A) const {
        return x < A.x;
    }
} A[maxn];

void build()
{
    sort( A , A + n );
    for ( int i = 0; i < n; i ++ ) {
        int nex = i + 1;
        while ( nex < n && A[nex].x == A[i].x ) { //當兩個點的x值相等時
            int a = A[nex].num , b = A[i].num; //排序後點的序號改變,不能直接用i表示,建立新的變數獲取該點原來的序號
            graph[a].push_back( make_pair( b , 0 ) );//建立一條從a到b的邊,距離為0
            graph[b].push_back( make_pair( a , 0 ) );//因為是無向圖,同樣建立一條從b到a的邊,距離為0
            nex ++; //把所有x相等的點連起來
        }
        if ( i > 0 ) {
            int a = A[i].num , b = A[i-1].num;
            graph[a].push_back( make_pair( b , A[i].x - A[i-1].x ) );
            graph[b].push_back( make_pair( a , A[i].x - A[i-1].x ) );
        }
        i = nex - 1; //因為while中進行nex++操作,所以i要從最後一個x與前面一個點x值相同的點開始
    }
}

int main()
{
    scanf("%d",&n);
    for ( int i = 0; i < n; i ++ ) {
        scanf("%d%d",&A[i].x,&A[i].y);
        A[i].num = i;
    }
    build(); //將x之間的差作為距離
    for ( int i = 0; i < n; i ++ )
        swap( A[i].x , A[i].y );
    build(); //將y之間的差作為距離

    queue < int > q;
    for ( int i = 0; i < n; i ++ )
        dis[i] = 2000000000;
    dis[0] = 0;
    q.push( 0 ); //第一個點入隊
    isQueue[0] = 1;
    while ( !q.empty() ) {
        int now = q.front() , nex;
        q.pop(); //當前點出隊
        isQueue[now] = 0;
        for ( int i = graph[now].size()-1; i >= 0; i -- ) { //遍歷跟當前點有邊的所有點
            nex = graph[now][i].first; //獲得與當前點相連的點的編號
            if ( dis[now] + graph[now][i].second < dis[nex] ) { //當前點到起點的距離+當前點到下一個點的距離<下一個點到起點的距離
                dis[nex] = dis[now] + graph[now][i].second;
                if ( isQueue[nex] == 0 ) { //如果下一個點不在佇列中,入隊;否則讓它留在隊中供其他點做判斷
                    isQueue[nex] = 1 ;
                    q.push( nex );
                }
            }
        }
    }
    cout << dis[n-1] << endl;
    return 0;
}


相關推薦

Islands Travel——SPFA短路

又是一道微軟的筆試題,看來最短路徑很重要啊! 時間限制:10000ms   單點時限:1000ms   記憶體限制:256MB 描述 There are N islands on a planet whose coordinates are (X1, Y1), (

利用SPFA算法短路

while nbsp cout poi 操作 沒有 實際應用 a算法 () 該算法由Bellman-Ford算法演變過來,首先介紹一下Bellman-Ford算法 最短路最多經過n-1個點,可以用n-1輪松弛操作來得到 for(int i=0;i<n;i++)

POJ 3592--Instantaneous Transference【SCC縮點新建圖 &amp;&amp; SPFA長路 &amp;&amp; 經典】

col describe sca 搜索 hat style ecif test csdn Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K

bfs短路的幾道例題

情況 map 加權 while [] insert dir end void 題目來自於記蒜客數據結構課,類型差不多,都是用bfs求最短路(註意是不加權的最短路,加權的情況後面的文章會講)。 代碼如下: 1 //記蒜客習題 2 //bfs求某點到其他各點的最短距離

暴力短路

images search 錯誤 txt spa pla cin aps tar 暴力求最短路 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 思路: 求1-5的最短距離找所有1可以直接到達的點,從這些點再去找5,並且記錄我現在已經走了的

【模板】前向星 SPFA短(長)路

代碼 poj ostream name 兩個 col spfa ron esp 之前一個改自別人的模板竟然在一道題上TLE了,而代碼也實在醜陋,網上找得到的模板也大多跑得慢(vector存圖)或代碼醜陋、殘疾(無初始化函數的模板能叫模板嗎?),索性自己重新寫了一個。 題是P

短路的多種方法比較及應用

eight nbsp 我們 以及 原來 ron man 如果 ans 《挑戰程序設計競賽》裏面介紹了三種方法: Bellman-Ford、Dijkstra and Floyd 三者區別也都很明顯: Bellman-Ford: 求單源最短路,可以判斷有無負權回路(

UESTC - 1147 短路方案數

typedef gre pty uestc max prior rst pop type 這道題很是說明了記憶化搜索的重要性 瞎bfs遞推半天發現沒卵用(也許是姿勢不對,但我認為樹形或圖形dfs明顯好寫並且很好正確地遞推) 參考了別人的寫法,總感覺自己的實現能力太弱了 還有

UVA 816 -- Abbott's Revenge(BFS短路)

sid 結果 bool 迷宮問題 inf ios walk cstring solution UVA 816 -- Abbott‘s Revenge(BFS求最短路)   有一個 9 * 9 的交叉點的迷宮。 輸入起點, 離開起點時的朝向和終點, 求最短路(多解時任意一個

spfa短路

read define struct digi 路徑 測試 can 不存在 記得 思路:先算出每個點到1的最短路d1[i],記錄下路徑,然後枚舉最短路上的邊 刪掉之後再求一遍最短路,那麽這時的最短路就可能是答案。 既然這樣為甚麽不用A*求次短路呢?因為A*求次短路處理不了無

短路

() std 空間 格式 ret header const pri ace 求最短路 題目描述: 給定一張無向圖,求一條經過邊數最少的從點1到點N的最短路。 輸入格式: 第一行兩個整數N,M,表示點數和邊數。 接下來M行每行三個整數,表示一條無向邊的兩端和它的邊權。保證點的

E - Easy Dijkstra Problem(短路

cos rmi rom () lag col tro ring ecif Description Determine the shortest path between the specified vertices in the graph given in the inp

UVA - 816 Abbott's Revenge (BFS短路並列印路徑)

The 1999 World Finals Contest included a problem based on a dice maze. At the time the problem was written, the judges were unable to discover t

狼抓兔子 HYSBZ - 1001(網路流/平面圖轉對偶圖短路)

傳送門 題意:有個方格,求從(1,1)到(n,m)的最小割 題解:有兩種方法,第一種:使用dinic演算法,但是需要有個優化就是當每次增廣時,如果一旦哪條路增廣失敗,那麼就把這條路直接賦值為-1,把它堵死,這樣優化可以達到很高。 附上第一種程式碼:(使用白書上的dinic演算法呢,可以過

Single Source Shortest Path I (dijkstra()演算法鄰接錶轉化為鄰接矩陣短路)

Single Source Shortest Path For a given weighted graph $G = (V, E)$, find the shortest path from a source to each vertex. For each vertex $u$, print

Frogger (短路變形-短路上的大權)

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

How Many Maos Does the Guanxi Worth(去掉某一點及其所有的邊)(n次Dijkstra短路大值)

How Many Maos Does the Guanxi Worth “Guanxi” is a very important word in Chinese. It kind of means “relationship” or “contact”. Guanxi can be ba

單源短路-Dijsktra

最短路徑問題---Dijkstra演算法詳解 轉自:https://blog.csdn.net/qq_35644234/article/details/60870719 1、最短路徑問題介紹 問題解釋:  從圖中的某個頂點出發到達另外一個頂點的所經過的邊的權重和最小的一條路徑,

【演算法模板】Floyd短路

#include<iostream> using namespace std; const int MAXN=100+10; const int INF=99999999; int n,m,s,t,g[MAXN][MAXN]; int main() {

CH6202 黑暗城堡(短路徑生成樹+二叉堆優化的dijkstra短路模板)

題目連結: http://contest-hunter.org:83/contest/0x60%E3%80%8C%E5%9B%BE%E8%AE%BA%E3%80%8D%E4%BE%8B%E9%A2%98/6202%20%E9%BB%91%E6%9A%97%E5%9F%8E%E5%A0%A1