您的位置:首页 > 运维架构

10841 - Lift Hopping in the Real World

2013-04-03 13:19 483 查看
/*

乘坐电梯问题的升级版 要求的是 最差情况下的最短路

题意:从0层到k层 求最短路 换一次电梯+5s 但每次乘坐电梯的时候无法确定电梯在哪一层 所以要等一段时间才可以乘坐

求出最坏情况下所用的时间 也就是每次乘坐电梯 的时候电梯离你的位置最远。

*/

#include<cstdio>

#include<cstring>

#include<queue>

#include<vector>

#include<cstdlib>

#define INF 1<<30

using namespace std;

int n,k,T[60],vis[110],pa[110][110],d[111],ma[110],mi[110];

struct Edge

{

int from,to,dist,t;

};

struct Node

{

int u,w;

bool operator < (const Node &a) const

{

return w>a.w;

}

};

vector<Edge> edges;

vector<int> G[110];

int add(int x1,int y1,int w1,int i1)

{

edges.push_back((Edge)

{

x1,y1,w1,i1

});

int l = edges.size();

G[x1].push_back(l-1);

}

int dij()

{

memset(vis,0,sizeof(vis));

for(int i = 0; i < 110; i++)

d[i] = INF;

d[0] = 0;

priority_queue<Node> q;

while(!q.empty())

q.pop();

q.push((Node)

{

0,0

});

while(!q.empty())

{

Node p = q.top();

q.pop();

int u = p.u;

if(u==k)

break;

if(vis[u]) continue;

vis[u] = 1;

for(int i = 0; i < G[u].size(); i++)

{

Edge &e = edges[G[u][i]];

int v = e.to;

int t1 = T[e.t]*(ma[e.t]-u);

int t2 = T[e.t]*(u-mi[e.t]);

int temp = max(t1,t2);

if(u == 0)

{

if(d[v]>d[u]+e.dist+temp)

{

pa[u][v] = e.t;

d[v]=d[u]+e.dist+temp;

q.push((Node)

{

v,d[v]

});

}

}

else

{

if(d[v]>d[u]+e.dist+5+temp)

{

pa[0][v] = pa[0][u];

d[v]=d[u]+e.dist+5+temp;

q.push((Node)

{

v,d[v]

});

}

}

}

}

if(d[k]>=INF)

{

puts("IMPOSSIBLE");

}

else

{

int i = pa[0][k];

printf("%d\n",d[k]);//+T[i]*ma[i]);

}

}

int main()

{

while(scanf("%d%d",&n,&k)==2)

{

memset(ma,0,sizeof(ma));

memset(pa,0,sizeof(pa));

edges.clear();

for(int i = 0; i < 110; i++)

{

G[i].clear();

mi[i] = INF;

}

for(int i = 1; i <= n; i++)

scanf("%d",&T[i]);

for(int i = 1; i <= n; i++)

{

int tp[110],pos=0;

memset(tp,0,sizeof(tp));

do

{

scanf("%d",&tp[pos++]);

if(tp[pos-1]>ma[i])

ma[i] = tp[pos-1];

if(tp[pos-1]<mi[i])

mi[i] = tp[pos-1];

}

while(getchar()!='\n');

for(int j = 0; j < pos; j++)

for(int k = j+1; k < pos; k++)

{

add(tp[j],tp[k],abs(tp[j]-tp[k])*T[i],i);

add(tp[k],tp[j],abs(tp[j]-tp[k])*T[i],i);

}

}

dij();

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: