UVA 11367 - Full Tank

来源:互联网 发布:ubuntu 文本编辑命令 编辑:程序博客网 时间:2024/06/16 18:37

/*

最短路问题  可以分解为2种走法  1:原地加一单位油,2:到下一个地点。

*/

#include<cstdio>

#include<cstring>
#include<queue>
#include<vector>
#define M 10010
using namespace std;


struct Edge
{
    int from,to,dist;
    Edge(int x,int y,int z)
    {
        from = x;
        to = y;
        dist = z;
    }
};
struct P
{
    int cost;
    int u,f;
    bool operator < (const P &a) const
    {
        return a.cost < cost;
    }
};
int n,m,s,t,c;
const int INF = 1<<30;
vector<Edge> edges;
vector<int> G[M];
int dp[1010][1010],pr[1010],vis[1010][1010];


int init()
{
    for(int i = 0; i < n; i++)
        G[i].clear();
    edges.clear();
}
int add(int from,int to,int dist)
{
    edges.push_back(Edge(from,to,dist));
    int tm = edges.size();
    G[from].push_back(tm-1);
}
int bfs()
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= 100; j++)
            dp[i][j] = INF;
    memset(vis,0,sizeof(vis));
    priority_queue<P> q;
    while(!q.empty())
        q.pop();
    P tmp,tp;
    tmp.u = s;
    tmp.f = 0;
    tmp.cost = 0;
    dp[s][0] = 0;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.top();
        q.pop();
        vis[tmp.u][tmp.f] = 1;
        if(tmp.u==t)
        {
            printf("%d\n",tmp.cost);
            return 0;
        }
        if(tmp.f+1<=c&&!vis[tmp.u][tmp.f+1]&&(dp[tmp.u][tmp.f+1]>dp[tmp.u][tmp.f]+pr[tmp.u]))
        {
            dp[tmp.u][tmp.f+1]=dp[tmp.u][tmp.f]+pr[tmp.u];
            tp.f = tmp.f+1;
            tp.u = tmp.u;
            tp.cost = dp[tmp.u][tmp.f+1];
            q.push(tp);
        }
        for(int i = 0; i < G[tmp.u].size(); i++)
        {
            Edge &e = edges[G[tmp.u][i]];
            int u = e.from,v = e.to,w = e.dist;
            if(tmp.f>=w&&!vis[v][tmp.f-w]&&dp[v][tmp.f-w]>tmp.cost)
            {
                dp[v][tmp.f-w] = tmp.cost;
                tp.u = v;
                tp.f = tmp.f-w;
                tp.cost = dp[v][tmp.f-w];
                q.push(tp);
            }
        }
    }
    puts("impossible");
}
int main()
{
    int x,y,z;
    scanf("%d %d",&n,&m);
    init();
    for(int i = 0; i < n; i++)
        scanf("%d",&pr[i]);
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    int kc;
    scanf("%d",&kc);
    while(kc--)
    {
        scanf("%d %d %d",&c,&s,&t);
        bfs();
    }
    return 0;
}