1254 - Prison Break
Time Limit: 2 second(s) Memory Limit: 32 MB

Michael Scofield has just broken out of the prison. Now he wants to go to a certain city for his next unfinished job. As you are the only programmer on his gang, he asked your help. As you know that the fuel prices vary in the cities, you have to write a code to help Scofield that instructs him where to take the fuel and which path to choose. Assume that his car uses one unit of fuel in one unit of distance. Now he gives you the starting city s where he starts his journey with his car, the destination city t and the capacity of the fuel tank of his car c, the code should find the route that uses the cheapest fuel cost. You can assume that Scofield's car starts with an empty fuel tank.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (2 ≤ n ≤ 100) and m (0 ≤ m ≤ 1000) where n denotes the number of cities and m denotes the number of roads. The next line contains n space separated integers, each lies between 1 and 100. The ith integer in this line denotes the fuel price (per unit) in the ith city. Each of the next m lines contains three integers u v w (0 ≤ u, v < n, 1 ≤ w ≤ 100, u ≠ v) denoting that there is a road between city u and v whose length is w.

The next line contains an integer q (1 ≤ q ≤ 100) denoting the number of queries by Scofield. Each of the next q lines contains the request. Each request contains three integers: c s t (1 ≤ c ≤ 100, 0 ≤ s, t < n) where c denotes the capacity of the tank, s denotes the starting city and t denotes the destination city.

Output

For each case, print the case number first. Then for each query print the cheapest trip from s to t using the car with the given capacity c or 'impossible' if there is no way of getting from s to t with the given car.

Sample Input

Output for Sample Input

1

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

Case 1:

170

impossible

思路:最短路+贪心+dp;

dp[i][j]表示到第i个城市,油箱中还剩j油的最小费用;

Dijkstra来维护,
开始超时了,后来网上看了人家的思路;我开始每到一个点就将所有的状态枚举了出来,这样会导致枚举对答案没有贡献的。

所以我们按贪心策略来枚举,因为,Dijkstra(Elog(n))的算法取出来的点是当前花费最小的情况,

所以如果这个点可以到达下一个点不加油就到更新下一个点,然后如果当前的油量没超过油箱的容量就+1,加入队列,就和广搜差不多的思想,然后,如果到达了目标点,那么这个一定是最小的就跳出,因这个是当前最小的。

  1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <iostream>
6 #include <algorithm>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 using namespace std;
11 typedef long long LL;
12 typedef struct pp
13 {
14 int to;
15 int id;
16 int cost;
17 bool operator<(const pp&cx)const
18 {
19 return cost>cx.cost;
20 }
21 } ss;
22 vector<ss>vec[200];
23 int dp[200][200];
24 bool flag[200][200];
25 priority_queue<ss>que;
26 int dj(int n,int m,int c);
27 int co[200];
28 int main(void)
29 {
30 int i,j,k;
31 scanf("%d",&k);
32 int __ca=0;
33 while(k--)
34 {
35 int n,m;
36 scanf("%d %d",&n,&m);
37 for(i=0; i<200; i++)
38 vec[i].clear();
39 for(i=0; i<n; i++)
40 {
41 scanf("%d",&co[i]);
42 }
43 while(m--)
44 {
45 int x,y,co;
46 scanf("%d %d %d",&x,&y,&co);
47 ss ans;
48 ans.to=y;
49 ans.cost=co;
50 vec[x].push_back(ans);
51 ans.to=x;
52 vec[y].push_back(ans);
53 }
54 int t;
55 printf("Case %d:\n",++__ca);
56 scanf("%d",&t);
57 while(t--)
58 {
59 int c,u,v;
60 scanf("%d %d %d",&c,&u,&v);
61 int ask=dj(u,v,c);
62 if(ask==1e9)
63 printf("impossible\n");
64 else
65 {
66 printf("%d\n",ask);
67 }
68 }
69 }
70 return 0;
71 }
72 int dj(int n,int m,int c)
73 {
74 int i,j,k;
75 memset(flag,0,sizeof(flag));
76 while(!que.empty())
77 {
78 que.pop();
79 }
80 for(i=0; i<200; i++)
81 {
82 for(j=0; j<200; j++)
83 {
84 dp[i][j]=1e9;
85 }
86 }
87 ss ans;
88 ans.to=n;
89 ans.cost=0;
90 dp[n][0]=0;
91 ans.id=0;
92 que.push(ans);
93 while(!que.empty())
94 {
95 ss ak;
96 ak=que.top();
97 if(ak.to==m)
98 return ak.cost;
99 que.pop();
100 if(dp[ak.to][ak.id]<ak.cost||flag[ak.to][ak.id])
101 {
102 continue;
103 }
104 else
105 {
106 flag[ak.to][ak.id]=true;
107 for(i=0; i<vec[ak.to].size(); i++)
108 {
109 ss aa=vec[ak.to][i];
110 {
111 if(ak.id>=aa.cost)
112 { ss dd;
113 if(dp[aa.to][ak.id-aa.cost]>ak.cost)
114 {
115 dp[aa.to][ak.id-aa.cost]=ak.cost;
116
117 dd.to=aa.to;
118 dd.id=ak.id-aa.cost;
119 dd.cost=dp[aa.to][dd.id];
120 que.push(dd);
121 }
122 }
123 if(ak.id<c)
124 { ss ad;
125 ad.id=1+ak.id;
126 ad.to=ak.to;
127 ad.cost=co[ak.to]+ak.cost;
128 if(dp[ad.to][ad.id]>ad.cost)
129 {dp[ad.to][ad.id]=ad.cost;que.push(ad);}
130 }
131 }
132 }
133 }
134 }
135 return 1e9;
136 }

1254 - Prison Break的更多相关文章

  1. light oj 1254 - Prison Break 最短路

    题目大意:n个点m条边的有向图,q次询问c,s,t,表示汽车邮箱容量为c,求从起点s到终点t的最小费用.汽车在每个点可以加任意的油,每个点的单位油价为a[i]. 题目思路:利用最小费优先队列优化最短路 ...

  2. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  3. hdu 3681 Prison Break (TSP问题)

    Prison Break Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. hdu 3681 Prison Break(状态压缩+bfs)

    Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...

  5. Prison Break

    Prison Break 时间限制: 1 Sec  内存限制: 128 MB提交: 105  解决: 16[提交][状态][讨论版] 题目描述 Scofild又要策划一次越狱行动,和上次一样,他已经掌 ...

  6. hdu3511 Prison Break 圆的扫描线

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511 题目: Prison Break Time Limit: 10000/5000 MS ( ...

  7. HDU3681 Prison Break

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

  9. hdu 3681 Prison Break

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个n*m的矩阵,'F'是起点.机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所 ...

随机推荐

  1. Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  2. OC-私有方法,构造方法,类的本质及启动过程

    总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...

  3. hive 启动不成功,报错:hive 启动报 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/mapred/MRVersi

    1. 现象:在任意位置输入 hive,准备启动 hive 时,报错: Exception in thread "main" java.lang.NoClassDefFoundErr ...

  4. Centos 7 安装redis,修改配置文件不生效、外网不能访问。

    前提: 在用Centos 7 安装 redis 时,遇上一下几个问题 ,记录下 . 1.修改配置文件,按官网步骤启动,不生效. 2.外网无法访问redis. 步骤: 1.打开centos 虚拟机 ,按 ...

  5. <转>C++继承中虚函数的使用

      转自:http://blog.csdn.net/itolfn/article/details/7412364 一:继承中的指针问题. 1. 指向基类的指针可以指向派生类对象,当基类指针指向派生类对 ...

  6. 统计图—柱状图可视化(python)

    # 柱状图 import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.sans-serif']=['Fan ...

  7. ciscn_2019_en_3

    例行检查我就不放了,64位的程序放入ida中 可以看到s到buf的距离是0x10,因为puts是遇到\x00截止.而且题目没有限制我们s输入的数量,所以可以通过这个puts泄露出libc的基值 很明显 ...

  8. 工作簿合并(Excel代码集团)

    同一文件夹内N个工作簿 ,每个工作簿里N个工作表,最终合并到一个工作表里的代码. 假设每个表格结构相同,第一行为标题,第二行为表头,表头内容固定,行数不固定,列固定14,工作表数量不固定,工作簿数量不 ...

  9. .NET6中一些常用组件的配置及使用记录,持续更新中。。。

    NET6App 介绍 .NET 6的CoreApp框架,用来学习.NET6的一些变动和新特性,使用EFCore,等一系列组件的运用,每个用单独的文档篇章记录,持续更新文档哦. 如果对您有帮助,点击右上 ...

  10. 关于后端 Entity Model Domain 的分界线

    前言:在我们开发中经常用一种类型的值来接收来自Dao层的数据并将它传送给前端,或者作为逻辑处理,一般这种类型有三种 Entity  Model  Domain  我们该如何准确的应用这三种类型呢?这三 ...