Full Tank? POJ - 3635 (bfs | 最短路)

After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?

To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.

Input

The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ u, v < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.

Output

For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or "impossible" if there is no way of getting from s to e with the given car.

Sample Input

5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4

Sample Output

170
impossible



题意:n个城市,m条路,每个城市有各自的油价,每条路有各自需要的油量,q个问题,每个问题给出c(汽车油箱容积)、s(起始起点)、t(出发地点),问你最小的邮费
思路:对于某的城市, 当 当前油量 小于 c时,可以选择加油,也就是油费+单位油费,油量+1,城市不变;同时如果 当前油量 >= 某条路花费,就可以通过这路去其他城市,这样油费不变,油量-路上油耗,城市变成另一个城市。
因为我们需要一个油费最少的答案,所有需要用优先队列来保证bfs的单调性,也就是最短路,这样当一个城市第一次出队列的时候就是它的最小油费


    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<string.h>
    using namespace std;
    int n,m,q,c,s,t;
    typedef pair<int,int> p;
    typedef pair<int,pair<int,int> >mp;
    int city[1005];
    int next[1005];
    int cnt;
    int vis[1005][105];
    struct Node
    {
        int x,y,val;
        int next;
        Node(int x=0,int y=0,int val=0,int next=-1):x(x),y(y),val(val),next(next) {}
    } node[20005];

    void add(int x,int y,int val)
    {
        node[++cnt].x = x;
        node[cnt].y = y;
        node[cnt].val = val;
        node[cnt].next = next[x];
        next[x] = cnt;
    }

    int bfs(int s,int t)
    {
        memset(vis,0x3f,sizeof(vis));
        priority_queue<mp,vector<mp>,greater<mp> >que;
        while(!que.empty())
            que.pop();
        que.push(mp(0,p(0,s)));
        while(!que.empty())
        {
            mp tmp = que.top();
            que.pop();
            int now = tmp.second.second;
            int s = tmp.second.first;
            if(vis[now][s] != 0x3f3f3f3f)
                continue;
            vis[now][s] = tmp.first;
            if(now == t)
                return s;
            if(s < c)
                que.push(mp(vis[now][s]+city[now],p(s+1,now)));
            for(int i=next[now]; i!=-1; i=node[i].next)
            {
                int w = node[i].val;
                int To = node[i].y;
                if(w <= s && vis[now][s] < vis[To][s-w])
                {
                    que.push(mp(vis[now][s],p(s-w,To)));
                }
            }
        }
        return -1;
    }

    int main()
    {
        scanf("%d%d",&n,&m);
        memset(next,-1,sizeof(next));
        cnt = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&city[i]);
        }
        for(int i=1; i<=m; i++)
        {
            int u,v,val;
            scanf("%d%d%d",&u,&v,&val);
            add(u,v,val);
            add(v,u,val);
        }
        scanf("%d",&q);
        for(int i=1; i<=q; i++)
        {
            scanf("%d%d%d",&c,&s,&t);
            int flag = bfs(s,t);
            if(flag != -1)printf("%d\n",vis[t][flag]);
            else
                printf("impossible\n");
        }
    }
View Code



猜你喜欢

转载自www.cnblogs.com/iwannabe/p/10603217.html