UVA 10801 Lift Hopping 最短路

时间:2024-07-14 13:05:20

2种方式直接代码就可以了。注意首次不需要60S的转换

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 105
const int INF = 0x3f3f3f3f;
int T[],N,K;
int w[MAXN][MAXN],d[MAXN];
int src[MAXN];
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > q;
void read()
{
memset(w,0x3f,sizeof(w));
for (int i = ; i < N; i++) scanf("%d",&T[i]);
for (int i = ; i < N; i++)
{
int cas = ;
char ch;
do
{
scanf("%d",&src[cas++]);
ch = getchar();
}while (ch != '\n');
for (int j = ; j < cas; j++)
for (int k = j; k < cas; k++)
{
w[src[j]][src[k]] = min(w[src[j]][src[k]],abs(src[k] - src[j]) * T[i]);
w[src[k]][src[j]] = min(w[src[k]][src[j]],abs(src[k] - src[j]) * T[i]);
}
}
}
void SPFA()
{
bool inq[MAXN];
memset(inq,false,sizeof(inq));
memset(d,0x3f,sizeof(d));
d[] = ;
queue<int>que;
while (!que.empty()) que.pop();
que.push();
while (!que.empty())
{
int u = que.front();que.pop();
inq[u] = false;
for (int i = ; i < MAXN; i++)
{
if (u == )
{
if (d[i] > d[u] + w[u][i])
{
d[i] = d[u] + w[u][i];
if (!inq[i])
{
inq[i] = true;
que.push(i);
}
}
}
else
{
if (d[i] > d[u] + w[u][i] + )
{
d[i] = d[u] + w[u][i] + ;
if (!inq[i])
{
inq[i] = true;
que.push(i);
}
}
}
}
}
}
void dijkstra()
{
memset(d,0x3f,sizeof(d));
d[] = ;
q.push(make_pair(d[],));
while (!q.empty())
{
pii t = q.top();q.pop();
int u = t.second;
if (t.first != d[u]) continue;
for (int v = ; v < MAXN; v++)
{
if (u == )
{
if (d[v] > d[u] + w[u][v])
{
d[v] = d[u] + w[u][v];
q.push(make_pair(d[v],v));
}
}
else
{
if (d[v] > d[u] + w[u][v] + )
{
d[v] = d[u] + w[u][v] + ;
q.push(make_pair(d[v],v));
}
}
}
}
}
int main()
{
//freopen("sample.txt","r",stdin);
while (scanf("%d%d",&N,&K) != EOF)
{
read();
SPFA();
//dijkstra();
if (d[K] == INF) puts("IMPOSSIBLE");
else printf("%d\n",d[K]);
}
return ;
}