码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3635 BFS+优先队列

时间:2014-10-04 21:34:17      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   strong   

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 }

 

POJ 3635 BFS+优先队列

标签:des   style   blog   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/qq1012662902/p/4006254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!