Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Last active August 29, 2015 13:58
Show Gist options
  • Save KT-Yeh/9952994 to your computer and use it in GitHub Desktop.
Save KT-Yeh/9952994 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <queue>
using namespace std;
#define Inf 99999999
int main()
{
ios::sync_with_stdio(false);
int n, k;
int T[6];
int Floor[100];
int Weight[100][100];
int Dis[100];
while (cin >> n >> k)
{
for (int i = 1; i <= n; ++i)
cin >> T[i];
for (int i = 0; i < 100; ++i) {
Dis[i] = Inf;
for (int j = 0; j < 100; ++j)
Weight[i][j] = Inf;
}
cin.ignore();
for (int i = 1; i <= n; ++i) {
string str;
getline(cin, str);
stringstream ss(str);
int Count = 0;
while (ss >> Floor[Count]) ++Count;
for (int x = 0; x < Count; ++x)
for (int y = x + 1; y < Count; ++y) {
int f1 = Floor[x], f2 = Floor[y];
if ((f2-f1)*T[i] < Weight[f1][f2])
Weight[f1][f2] = Weight[f2][f1] = (f2-f1)*T[i];
}
}
queue<int> Q;
int inQueue[101] = {0};
Dis[0] = 0;
inQueue[0] = true;
Q.push(0);
while (!Q.empty()) {
int now = Q.front();
inQueue[now] = false;
Q.pop();
for (int nxt = 0; nxt < 100; ++nxt) {
if (Dis[now] + Weight[now][nxt] + 60 < Dis[nxt]) {
Dis[nxt] = Dis[now] + Weight[now][nxt] + 60;
if (!inQueue[nxt]) {
Q.push(nxt);
inQueue[nxt] = true;
}
}
}
}
if (k == 0) cout << 0 << endl; // 不加這行答案會是-60
else if (Dis[k] == Inf) cout << "IMPOSSIBLE" << endl;
else cout << Dis[k] - 60 << endl;
// SPFA把起點也當成有轉換電梯了 所以答案要減掉60
}
}
@SwadhInz
Copy link

why you add a 60 in the 53 number line if (Dis[now] + Weight[now][nxt] + 60 < Dis[nxt]) ..?? I dont get it cuz every time it adds additional 60 as Dis was set to Inf .. :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment