10801 - Lift Hopping

//这题其实就是 658 - It's not a Bug, it's a Feature!

//注意下面标红的判重!

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <cstdlib>
#include <sstream>
#include <memory.h>

using namespace std;
const int MAX=5;
//typedef pair<int,int> pii;
struct Elevator{
    int cost;
    vector <int> to;
};
struct State{
    int time,num,flo;//num代表在第flo层的时候,使用的是第几个电梯
};
struct cmp{
    bool operator () (const State& i,const State& j){
        return i.time>j.time;
    }
};
Elevator ele[MAX];
bool findFlag,done[105],sta[105];
int n,k,ans;
string line;
vector<int> hash[105];//用于存放每一层对应的电梯
priority_queue<State,vector<State>,cmp> q;
void spfa(){
    memset(done,0,sizeof(done));
    //memset(sta,0,sizeof(sta));
    //for(int i=0;i<100;i++)
      //  sta[i]=1;
    while(!q.empty()) q.pop();

    int floor,eleNum,t;
    State u,v;
    //q.push(make_pair(0,0));
    //初始化 从 0层的开始状态
    for(int i=0;i<(int)hash[0].size();i++){
        eleNum=hash[0][i];
        for(int j=0;j<(int)ele[eleNum].to.size();j++){
            t=ele[eleNum].to[j];
            v.num=eleNum;
            v.flo=t;
            v.time=(abs(0-t)*ele[eleNum].cost);
            //if(v.num!=u.num) //根据要求从0层开始时,不必花费switch时间
              //  v.time+=60;
            //cout<<t<<" "<<v.time<<endl;
            q.push(v);
        }
    }
    while(!q.empty()){
    //while(memcmp(done,sta,sizeof(done))){
        u=q.top();q.pop();
        //cout<<u.num<<endl;
        //cout<<u.time<<endl;
        //if(u.time==275)
          //  cout<<u.flo<<endl;
        floor=u.flo;//pii 的second 存放floor,first 存放到达floor的时间
        //cout<<k<<endl;
        if(floor==k){
            findFlag=1;
            ans=u.time;
            break;
        }
        if(done[floor])  //在这里判重,而不在下面判重的原因是,这个弹出的第flo层状态一定是所有状态中最小的(所有可能到达这个flo的状 
            continue;     //态一定已经在队列中了)
        done[floor]=1;
        for(int i=0;i<(int)hash[floor].size();i++){
            eleNum=hash[floor][i];
            //cout<<eleNum<<endl;
            if(u.num==eleNum)
                continue;
            for(int j=0;j<(int)ele[eleNum].to.size();j++){
                t=ele[eleNum].to[j];
                //cout<<t<<endl;
                v.num=eleNum;
                v.flo=t;
                v.time=u.time+(abs(u.flo-t)*ele[eleNum].cost);
                if(v.num!=u.num)
                    v.time+=60;

                //不在这里判重,如果不重则加入的原因是 可能优先级队列中的已经有了在第flo层的状态,而它不是最小值,在这里判重

                //会导致比队列中更小的优先值无法加入队列中
                q.push(v);
            }
        }
    }
}
int main()
{
    freopen("i.txt","r",stdin);
    int t;
    while(cin>>n>>k){
        for(int i=0;i<n;i++)
            cin>>ele[i].cost;
        cin.ignore();
        for(int i=0;i<=100;i++) hash[i].clear();
        for(int i=0;i<n;i++){
            ele[i].to.clear();
            getline(cin,line);
            //cout<<line<<endl;
            istringstream stream(line);
            while(stream>>t){
                //cout<<t<<" ";
                hash[t].push_back(i);
                ele[i].to.push_back(t);
            }
            //cout<<endl;
        }
        findFlag=0;
        spfa();
        if(findFlag)
            cout<<ans<<endl;
        else
            cout<<"IMPOSSIBLE"<<endl;
    }
    return 0;
}

你可能感兴趣的:(struct,vector,Stream,ini,pair,lift)