UVA 10801 Lift Hopping

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <queue>
#include <functional>
using namespace std;
int tim[10];
int n,k;
int lift[101][101];
int temp[10][101];
const int INF=0xffffff;
bool vis[101];
int d[101];
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > q;
void SPFA(int start)
{
	memset(vis,0,sizeof vis);
	for(int i=0;i<101;++i) d[i]=INF;
	d[start]=0;
	queue<int> q;
	q.push(start);
	while(!q.empty()){
		int u=q.front();q.pop();
		vis[u]=false;
		for(int i=0;i<101;i++){
			if(u==0){
				if(d[i]>d[u]+lift[u][i]){
					d[i]=d[u]+lift[u][i];
					if(!vis[i]){
						vis[i]=true;
						q.push(i);
					}
				}
			}
			else if(d[i]>d[u]+lift[u][i]+60){
				d[i]=d[u]+lift[u][i]+60;
				if(!vis[i]){
					vis[i]=true;
					q.push(i);
				}
			}
		}
	}
}
void Dijkstra(int start)
{
	for(int i=0;i<101;i++) d[i]=INF;
	d[start]=0;
	q.push(make_pair(0,0));
	while(!q.empty()){
		pii t=q.top();q.pop();
		int u=t.second;
		if(t.first!=d[u]) continue;
		for(int i=0;i<101;i++){
			if(u==0){
				if(d[i]>d[u]+lift[u][i]){
					d[i]=d[u]+lift[u][i];
					q.push(make_pair(d[i],i));
				}
			}
			else if(d[i]>d[u]+lift[u][i]+60){
				d[i]=d[u]+lift[u][i]+60;
				q.push(make_pair(d[i],i));
			}
		}
	}
}
int main()
{
	while(scanf("%d%*c%d%*c",&n,&k)!=EOF){
		for(int i=0;i<n;i++)
			scanf("%d%*c",&tim[i]);
		for(int i=0;i<101;i++)
			for(int j=i;j<101;j++)
				lift[i][j]=lift[j][i]=INF;
		int Index=0;
		for(int i=0;i<n;i++){
			int x;
			char code;
			scanf("%d%c",&x,&code);
			temp[i][Index++]=x;
			if(code==' ') --i;
			else{
				for(int x=0;x<Index;x++){
					for(int j=0;j<Index;j++){
						lift[temp[i][x]][temp[i][j]]=min(abs(tim[i]*(temp[i][x]-temp[i][j])),lift[temp[i][x]][temp[i][j]]);
					}
				}
				Index=0;
			}
		}
		//SPFA(0);
		Dijkstra(0);
		if(d[k]!=INF) printf("%d\n",d[k]);
		else printf("IMPOSSIBLE\n");
	}
	return 0;
}

你可能感兴趣的:(UVA 10801 Lift Hopping)