poj3635 Full Tank? (bfs+heap)
来源:互联网 发布:青少年编程培训 编辑:程序博客网 时间:2024/05/29 00:33
题意:
n个城市m条路,给出每个城市的油价,每组询问给出油箱容量,起点和终点,针对每组询问输出从起点到终点的最小花费。
做法:
最短路所以肯定是bfs。cost[i][j]表示到第i个城市,剩余油量为j的最小花费。每次从优先队列里取花费最大的,然后可以选择不加油,也可以加1个单位的油。
这样第一次扩展到终点的花费就是最小花费。
完整代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#include <vector>#define INF 0x3fusing namespace std;struct Node{int v, gas, cost;Node(int a, int b, int c) : v(a), gas(b), cost(c) {}bool operator < (const Node &n) const {return cost > n.cost;}};struct Edge{int to, value;Edge(int a, int b) : to(a), value(b) {}};int V, E, Q;int s, t, cap;int price[1111];vector<Edge> edges[1111];void addEdge(int from, int to, int value) {edges[from].push_back(Edge(to, value));}int cost[1111][111];bool vis[1111][111];int spfa() {memset(vis, 0, sizeof(vis));memset(cost, 0x3f, sizeof(cost));cost[s][0] = 0;vis[s][0] = 1;priority_queue<Node> q;q.push(Node(s, 0, 0));while (!q.empty()) {Node cur = q.top();q.pop();if (cur.v == t) return cur.cost;vis[cur.v][cur.gas] = 1;if (cur.gas + 1 <= cap && cur.cost + price[cur.v] < cost[cur.v][cur.gas + 1]) {if (!vis[cur.v][cur.gas + 1]) {cost[cur.v][cur.gas + 1] = cur.cost + price[cur.v];q.push(Node(cur.v, cur.gas + 1, cur.cost + price[cur.v]));}}for (int i = 0; i < edges[cur.v].size(); i++) {Edge e = edges[cur.v][i];if (cur.gas - e.value >= 0 && cur.cost < cost[e.to][cur.gas - e.value]) {if (!vis[e.to][cur.gas - e.value]) {cost[e.to][cur.gas - e.value] = cur.cost;q.push(Node(e.to, cur.gas - e.value, cur.cost));}}}}return INF;}int main() {scanf("%d %d", &V, &E);for (int i = 0; i < V; i++) {scanf("%d", &price[i]);}int a, b, c;for (int i = 0; i < E; i++) {scanf("%d %d %d", &a, &b, &c);addEdge(a, b, c);addEdge(b, a, c);}scanf("%d", &Q);for (int i = 0; i < Q; i++) {scanf("%d %d %d", &cap, &s, &t);int ans = spfa();if (ans == INF) printf("impossible\n");else printf("%d\n", ans);}return 0;}
0 0
- poj3635 Full Tank? (bfs+heap)
- Poj3635 Full Tank?
- poj3635 Full Tank
- poj3635 Full Tank?(spfa+dp)
- poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。
- poj3635 Full Tank? 最短路+dp
- POJ3635 Full Tank?(最短路+DP)
- poj3635—Full Tank?(spfa+dp)
- POJ3635--Full Tank?(优先队列+最短路)
- UVA 11367 Full Tank? (dij+heap+dp)
- pku 3635 Full Tank? 优先队列+BFS
- POJ 3635 Full Tank?(BFS)
- UVA 11367 Full Tank?(bfs最短路)
- poj 3635 Full Tank? ( bfs+dp思想 )
- CSUOJ 1891 Full Tank? 最短路 bfs
- poj3635---BFS
- POJ 3635 Full Tank?
- poj 3635 Full Tank?
- APK安装过程及原理详解
- windows下安装,配置gcc编译器
- PHP中require和include路径问题总结
- MathType缺少字体MT Extra的解决办法
- SQL 添加链接服务器
- poj3635 Full Tank? (bfs+heap)
- 搜索音乐文件
- 服务器托管/租用的异同
- Android DrawerLayout 高仿QQ5.2双向侧滑菜单
- 关于字体库的制作
- ubuntu 安装 rJava 出错解决方案
- 单元测试学习1
- 评论的延迟加载
- Leetcode 3Sum Closest