POJ 3635 BFS+优先队列

Full Tank?

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6326   Accepted: 2086

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 ≤ uv < 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

题目意思:一辆车有固定的油的容量,给定一副图,每个结点有个price即油的单价,两点之间权值为从a走到b(或从b走到a)消耗的油量,给Q次询问,每次为oil,a,b即车的最大容量为oil,从a走到b花费最小的钱为多少,若不能从a到b,输出impossible。

思路:车在每个结点可以加一升、两升...直到油箱装满位置,也可以选择不加油跑到另外的结点,关于状态的转移很明显是bfs,用visited[i][oil]标记车在i处时装有oil升的油的状态,总结点为1000,每个结点100升油,也就100000个状态,很快就能bfs完了,注意每次出队在该城市只加一升油,否则若出队后加一升...直到加满的状态全入队的话,状态会重复导致超时。

代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9
10 #define    N 1005
11
12 int n, m;
13 int pri[N];
14 int visited[N][105];
15 int s, e, oil;
16
17 struct mem{
18     int y, w;
19 };
20 struct node{
21     int e, cost, oil;
22     bool operator<(const node a)const{
23         return a.cost<cost;
24     }
25 };
26
27 vector<mem>ve[N];
28
29 int bfs(){
30     int i, j, k;
31     node p, pp;
32     mem q;
33     priority_queue<node>Q;
34     memset(visited,0,sizeof(visited));
35     p.e=s;p.cost=0;p.oil=0;Q.push(p);
36     while(!Q.empty()){
37         p=Q.top();
38         Q.pop();
39     //    printf("%d %d %d\n",p.e,p.cost,p.oil);
40         if(p.e==e) return p.cost;
41
42         visited[p.e][p.oil]=1;
43         if(p.oil<oil&&!visited[p.e][p.oil+1]){
44             pp.oil=p.oil+1;
45             pp.cost=p.cost+pri[p.e];
46             pp.e=p.e;
47             //visited[pp.e][pp.oil]=1;
48             Q.push(pp);
49         }
50         for(i=0;i<ve[p.e].size();i++){
51             q=ve[p.e][i];
52             if(p.oil>=q.w&&!visited[q.y][p.oil-q.w]){
53                 pp.e=q.y;
54                 pp.cost=p.cost;
55                 pp.oil=p.oil-q.w;
56                 Q.push(pp);
57             //    visited[q.y][p.oil-q.w]=1;
58             }
59         }
60     }
61     return -1;
62 }
63
64 main()
65 {
66     int i, j, k, x, y, z, q;
67     mem p;
68     while(scanf("%d %d",&n,&m)==2){
69         for(i=0;i<n;i++) scanf("%d",&pri[i]),ve[i].clear();
70         for(i=0;i<m;i++){
71             scanf("%d %d %d",&x,&y,&z);
72             p.y=y;p.w=z;
73             ve[x].push_back(p);
74             p.y=x;ve[y].push_back(p);
75         }
76         scanf("%d",&q);
77         int ans;
78         while(q--){
79             scanf("%d %d %d",&oil,&s,&e);
80             ans=bfs();
81             if(ans==-1) printf("impossible\n");
82             else printf("%d\n",ans);
83         }
84     }
85 }
时间: 2024-08-15 10:42:56

POJ 3635 BFS+优先队列的相关文章

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

poj 3635 Full Tank? ( 图上dp )

题意: 已知每个点的加油站的油价单价(即点权),每条路的长度(边权). 有q个询问,每个询问包括起点s.终点e和油箱容量. 问从起点走到终点的最小花费.如果不可达输出impossible,否则输出最小的旅途费用. 算法: 其实要分析状态= =感觉就像是dp. 最直接的想法是  每到一个点都加上要走到下一个点所需要的油量.但是走的路不同,到底怎么处理加多少的问题呢? 因此想到分解状态,即拆点.每到一个点都+1单位的油量,然后把这个状态加入队列.另外如果现在油箱内的油足够达到下一点, 则更新状态,把

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

hdu 2102 A计划 具体题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,只是在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间. 果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,仅仅只是多加了个參数,就是迷宫的层数,我用0代表第一层.1代表第二层,这在数组里面会体现的. struct node { int index;