10801 - Lift Hopping//Bellman-Ford

来源:互联网 发布:国外免费域名注册 编辑:程序博客网 时间:2024/06/02 08:34

题目描述:就是裸的最短路。但是在换乘电梯的时候要耗时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;}


原创粉丝点击