UVA 10801(p381)----Lift Hopping

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
int v[200],dist[200],n,k,lift[200];
int now,g[200][200],t[200];
queue<int> q;
void solve()
{
    memset(v,0,sizeof(v));
    memset(dist,127,sizeof(dist));
    dist[0]=0;q.push(0);
    while(!q.empty())
    {
        now=q.front();
       // cout<<now<<endl;
        q.pop();
        for(int i=0;i<100;i++)
        {
            if(now==0)
            {
               if(dist[i]>dist[now]+g[now][i])
                {
                   dist[i]=dist[now]+g[now][i];
                   if(!v[i])
                    {
                      q.push(i);
                      v[i]=true;
                    }
                }
            }
            else
              if(dist[i]>dist[now]+g[now][i]+60)
                {
                   dist[i]=dist[now]+g[now][i]+60;
                   if(!v[i])
                    {
                      q.push(i);
                      v[i]=true;
                    }
                }
        }
        v[now]=false;
    }
}
int main()
{
    //freopen("in.in","r",stdin);
    while(scanf("%d%d",&n,&k)!=EOF&&n)
    {
        memset(g,0x3f,sizeof(g));
        for(int i=0;i<n;i++) scanf("%d",&t[i]);
        for(int i=0;i<n;i++)
        {
            int num=0,x,c;
            do{
                scanf("%d",&lift[num++]);
            }while(getchar()!='\n');
            for(int j=0;j<num;j++)
                for(int k=j+1;k<num;k++)
                 {
                   int temp=abs(lift[j]-lift[k])*t[i];
                   g[lift[j]][lift[k]]=min(g[lift[j]][lift[k]],temp);
                   g[lift[k]][lift[j]]=g[lift[j]][lift[k]];
                 }
        }
      solve();
      if(dist[k]<1e9) printf("%d\n",dist[k]);
      else printf("IMPOSSIBLE\n");
    }
    return 0;
}

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