CSUOJ 1891 Full Tank? 有约束条件下的最短路 分支限界法?

Description

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

Hint

Source

2007 Nordic Collegiate Programming Contest

没错,这道题是一道有约束的最短路

首先我们可以发现这应该是一张稀疏图,因为n=1000的时候,m顶多也才10000,算了一下,觉得堆优化的Dijkstra应该是可以跑过的

然后就跑喽,用一个数组co[i][j]表示目前走到i号城市,剩余油量为j时的最小代价。

对于每一点,都两种情况,第一种是留在原地,+1油量,第二种是考察所有的邻接点,看能否过去。

把状态全都压到堆里面去跑,一旦发现终点,就停止,假若最后队列为空,而且离终点的距离为0x3f3f3f3f的话,就是impossible

同样的有约束,同样的压到堆里面去,也是同定义状态有关系。。。

状态的重要性越来越明显了

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
const int maxm=10010,maxn=1010,maxc=110;

struct Edge{
    int v,nt,c;
    explicit Edge(int a=0,int b=0,int cc=0):v(a),nt(b),c(cc){};
}edge[maxm<<1];
struct Node{
    int cost,city,fuel;
    explicit Node(int a=0,int b=0,int c=0):cost(a),city(b),fuel(c){};
    bool operator<(const Node& n)const{return cost>n.cost;}
}tmp;

int n,m,fi[maxn],p[maxn],x,y,c,ind,q,s,e,co[maxn][maxc];
bool vis[maxn][maxc];
priority_queue<Node> pq;
inline void addeage(){
    edge[ind]=Edge(x,fi[y],c);
    fi[y]=ind++;
    edge[ind]=Edge(y,fi[x],c);
    fi[x]=ind++;
}

int main(){
    ios_base::sync_with_stdio(0);
    while(cin>>n>>m){
        memset(fi,-1,sizeof(int)*(n+1));
        for(int i=0;i<n;++i)
            cin>>p[i];
        for(int i=0;i<m;++i){
            cin>>x>>y>>c;
            addeage();
        }
        cin>>q;
        while(q--){
            cin>>c>>s>>e;
            memset(co,0x3f3f3f3f,sizeof(co[0])*(n+1));
            memset(vis,0,sizeof(vis[0])*(n+1));
            co[s][0]=0;
            pq.push(Node(0,s,0));
            while(!pq.empty()){
                tmp=pq.top();pq.pop();
                if(vis[tmp.city][tmp.fuel])
                    continue;
                if(tmp.city==e)
                    break;
                if(tmp.fuel<c&&tmp.cost+p[tmp.city]<co[tmp.city][tmp.fuel+1]){
                    co[tmp.city][tmp.fuel+1]=tmp.cost+p[tmp.city];
                    pq.push(Node(tmp.cost+p[tmp.city],tmp.city,tmp.fuel+1));
                }
                for(int i=fi[tmp.city];i!=-1;i=edge[i].nt)
                if(tmp.fuel-edge[i].c>=0&&co[tmp.city][tmp.fuel]<co[edge[i].v][tmp.fuel-edge[i].c]){
                    co[edge[i].v][tmp.fuel-edge[i].c]=co[tmp.city][tmp.fuel];
                    pq.push(Node(co[edge[i].v][tmp.fuel-edge[i].c],edge[i].v,tmp.fuel-edge[i].c));
                }
                vis[tmp.city][tmp.fuel]=true;
            }
            if(co[e][0]<0x3f3f3f3f)
                cout<<co[e][0]<<endl;
            else
                cout<<"impossible\n";
            while(!pq.empty())pq.pop();
        }
        ind=0;
    }
    return 0;
}

    原文作者:分支限界法
    原文地址: https://blog.csdn.net/Good_night_Sion_/article/details/70941683
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞