# 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.

```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```

```170
impossible```

## Source

2007 Nordic Collegiate Programming Contest

``````#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;
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;
}
cin>>q;
while(q--){
cin>>c>>s>>e;
memset(co,0x3f3f3f3f,sizeof(co)*(n+1));
memset(vis,0,sizeof(vis)*(n+1));
co[s]=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]<0x3f3f3f3f)
cout<<co[e]<<endl;
else
cout<<"impossible\n";
while(!pq.empty())pq.pop();
}
ind=0;
}
return 0;
}
``````

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