HDU–2066[一個人的旅行] Dijkstra

來源:互聯網
上載者:User

最短路徑題,套用Dijkstra演算法,但增加一個頂點A連通所有起點,一個頂點B連通所有終點..問題就轉化為A到B的最短路徑D,D再減去A跟B多出來的距離就得到結果..

 

 

CODE:

#include <iostream>using namespace std;const int INF=99999999;const int MAX=1010;int max(int a,int b){    return a>b?a:b;}int T,S,D,L;int map[MAX][MAX];int Dijkstra(int start,int end){    bool hash[MAX];    int path[MAX];//記錄從起始點到i點的最短路徑    for(int i=0;i<=L;i++)    {        hash[i]=true;//表示該點沒有處理過        path[i]=INF;//表示i點沒被處理過,此時最短路為無窮大    }    hash[start]=false;    path[start]=0;//注意點    while(start!=end)    {        for(i=0;i<=L;i++)        {            if(map[start][i]!=0)            {                if(path[i]>path[start]+map[start][i])                    path[i]=path[start]+map[start][i];            }        }        int min=INF;        for(i=0;i<=L;i++)        {            if(path[i]<min&&hash[i])//找新的起始點            {                min=path[i];                start=i;            }        }        hash[start]=false;    }return path[end];}int main (){    int i,a,b,time,s,d;    while(scanf("%d%d%d",&T,&S,&D)!=EOF)    {        memset(map,0,sizeof(map));//初始化        L=0;        for(i=1;i<=T;i++)        {            scanf("%d%d%d",&a,&b,&time);            if(!map[a][b])                map[a][b]=map[b][a]=time;            else            {                if(map[a][b]>time)                    map[a][b]=map[b][a]=time;            }            if(L<max(a,b))                L=max(a,b);//更新最大編號的點        }        for(i=1;i<=S;i++)        {            scanf("%d",&s);            map[0][s]=map[s][0]=1;//添加總的起始點        }        L++;//添加結束點        for(i=1;i<=D;i++)        {            scanf("%d",&d);            map[L][d]=map[d][L]=1;        }        printf("%d/n",Dijkstra(0,L)-2);    }return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.