題目描述:就是裸的最短路。但是在轉乘電梯的時候要耗時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;}