• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
井_中窥月
万物美好,我在中央
博客园    首页    新随笔    联系   管理    订阅  订阅
uva 10801(最短路)

题目大意:

有一层不超过100层的大楼, 有n个电梯,它们的速度都不同。 而且每个电梯只能到达指定的那些楼层,而且它们都有各自的速度(即上升一层或下降一层所用的时间)。 如果一个人在某层走出电梯,要换一个电梯乘,那么他要等60秒(不管要等的是那个电梯,即使是刚刚出来的那个电梯也要等60秒)。在0层搭电梯出发时不需要等待。一个人从0层开始,目的地是k层, 现在要搭这些电梯,问最少需多少时间。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll int
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define pfi(n) printf("%d\n", n)
#define sfi2(n, m) scanf("%d%d", &n, &m)
#define sfd2(n, m) scanf("%lf%lf", &n, &m)
#define pfi2(n, m) printf("%d %d\n", n, m)
#define pfi3(a, b, c) printf("%d %d %d\n", a, b, c)
#define maxn 100
const int INF = 0x3f3f3f3f;
int w[maxn][maxn];
int c[maxn];
bool vis[maxn];
int d[maxn];
int t[6];
inline void SPFA(int src){
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < maxn; ++i) d[i] = INF;
    d[src] = 0;
    queue<int> q;
    q.push(src);
    while(!q.empty()){
        int u = q.front(); q.pop();
        vis[u] = false;
        for(int i = 0; i < maxn; ++i){
            if(u == 0){
                if(d[i] > d[u] + w[u][i]){
                    d[i] = d[u] + w[u][i];
                    if(!vis[i]){
                        vis[i] = true;
                        q.push(i);
                    }
                }
            }
            else if(d[i] > d[u] + w[u][i] + 60){
                d[i] = d[u] + w[u][i] + 60;
                if(!vis[i]){
                    vis[i] = true;
                    q.push(i);
                }
            }
        }
    }
    return ;
}

int main()
{
    int n, k;
    while(~sfi2(n, k))
    {
        _cle(w, 0x3f);
        repu(i, 0, n) sfi(t[i]);
        int la = -1, cur;
        char cc;
        repu(i, 0, n)
        {
            int m = 0;
            while(1)
            {
                sfi(c[m++]);
                cc = getchar();
                if(cc == '\n')
                {
                    repu(j, 0, m) repu(k, j + 1, m)
                    w[c[k]][c[j]] = w[c[j]][c[k]] = min(w[c[j]][c[k]], (c[k] - c[j]) * t[i]);
                    break;
                }
            }
            SPFA(0);
        }
        if(d[k] != INF) printf("%d\n", d[k]);
        else printf("IMPOSSIBLE\n");
    }
    return 0;
}
View Code

 

posted on 2015-09-27 09:44  井_中窥月  阅读(280)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3