10801 – Lift Hopping//Bellman-Ford

來源:互聯網
上載者:User

題目描述:就是裸的最短路。但是在轉乘電梯的時候要耗時60s,同時從i到j層可以有不同的地奧體達到。

題目分析:就是在建圖的時候注意一點,在Bellman-Ford的過程中改一下。

下面是代碼:

#include <cstdio>#include <cstring>#include <queue>using namespace std;const int INF = 1000000000;const int maxnn = 6;const int maxnk = 105;int G[maxnk][maxnk];int t[maxnn];int temp[maxnk];int d[maxnk];bool inq[maxnk];queue<int> q;int n,k;void Bellman_Ford(){    for(int i = 0; i <= maxnk; i++) d[i] = INF,inq[i] = false;    d[0] = 0;    q.push(0);    inq[0] = true;    while(!q.empty())    {        int u = q.front(); q.pop();        inq[u] = false;        for(int v = 0; v < 100; v++)        {            if(d[v] > d[u] + G[u][v] + 60)            {                d[v] = d[u] + G[u][v] + 60;                if(!inq[v])                {                    inq[v] = true;                    q.push(v);                }            }        }    }    if(d[k] == INF) printf("IMPOSSIBLE\n");    else if(k == 0) printf("0\n");    else printf("%d\n",d[k] - 60);}int main(){    while(scanf("%d%d",&n,&k) != EOF)    {        for(int i = 0; i < maxnk; i++)        {            for(int j = 0; j < maxnk; j++) G[i][j] = G[j][i] = INF;        }        for(int i = 0; i < n; i++) scanf("%d",&t[i]);        int tcount = 0;        for(int i = 0; i < n; i++)        {            int count = 0;            while(true)            {                scanf("%d",&temp[count++]);                char ch = getchar();                if(ch == '\n') break;            }            for(int i = 0; i < count; i++)            {                int u = temp[i];                for(int j = i + 1; j < count; j++)                {                    int v = temp[j];                    int w = (v - u) * t[tcount];                    if(w < G[u][v]) G[u][v] = G[v][u] = w;                }            }            tcount++;        }        Bellman_Ford();    }    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.