poj3635
来源:互联网 发布:mac显示桌面隐藏文件 编辑:程序博客网 时间:2024/05/16 07:09
非常好的bfs dp题
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #define MAXN 1005 #define MAXM 100005 #define INF 1000000000 using namespace std; struct Edge { int v, w, next; }edge[MAXM]; struct Node { int v, cost, f; bool operator <(const Node &a) const{ return a.cost < cost; } }; int head[MAXN], e, n, m, cap; int dp[MAXN][105], used[MAXN][105], p[MAXN]; int s, t, ask; priority_queue<Node>q; void init() { memset(head, -1, sizeof(head)); e = 0; } void ready() { for(int i = 0; i < n; i++) for(int j = 0; j <= 100; j++) dp[i][j] = INF; dp[s][0] = 0; memset(used, 0, sizeof(used)); while(!q.empty()) q.pop(); } void insert(int x, int y, int w) { edge[e].v = y; edge[e].w = w; edge[e].next = head[x]; head[x] = e++; } int bfs() { Node a, b; a.v = s, a.cost = 0, a.f = 0; q.push(a); while(!q.empty()) { a = q.top(); q.pop(); int u = a.v; int cost = a.cost; int f = a.f; used[u][f] = 1; if(u == t) return cost; if(f + 1 <= cap && !used[u][f + 1] && dp[u][f + 1] > dp[u][f] + p[u]) { dp[u][f + 1] = dp[u][f] + p[u]; b.v = u; b.f = f + 1; b.cost = dp[u][f + 1]; q.push(b); } for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; int w = edge[i].w; if(f >= w && !used[v][f - w] && dp[v][f - w] > cost) { dp[v][f - w] = cost; b.v = v; b.f = f - w; b.cost = dp[v][f - w]; q.push(b); } } } return -1; } int main() { int x, y, w; scanf("%d%d", &n, &m); init(); for(int i = 0; i < n; i++) scanf("%d", &p[i]); while(m--) { scanf("%d%d%d", &x, &y, &w); insert(x, y, w); insert(y, x, w); } scanf("%d", &ask); while(ask--) { scanf("%d%d%d", &cap, &s, &t); ready(); int ans = bfs(); if(ans != -1) printf("%d\n", ans); else printf("impossible\n"); } return 0; }
0 0
- poj3635
- poj3635
- poj3635
- poj3635---BFS
- poj3635加油车
- Poj3635 Full Tank?
- POJ3635-搜索,优先队列
- poj3635 Full Tank
- poj3635 Full Tank? (bfs+heap)
- poj3635 Full Tank?(spfa+dp)
- poj3635-DP+优先队列搜索最短路
- poj3635 Full Tank? 最短路+dp
- POJ3635 Full Tank?(最短路+DP)
- poj3635—Full Tank?(spfa+dp)
- POJ3635--Full Tank?(优先队列+最短路)
- POJ3635 周游诸城:变形的SPFA/Dijkstra最短路+动规思想+Heap
- POJ3635 周游诸城:变形的SPFA/Dijkstra最短路+动规思想+Heap
- poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。
- android中打开本地doc、pdf、ppt、text等文件功能的实现
- ubuntu下关闭触摸板
- sass入门
- 绘图 matplotlib使用
- 文件传输基础——Java IO流笔记
- poj3635
- Putty自动关闭问题
- 类和对象
- 初识Winform
- Only One Chance
- 哈夫曼树
- markdown字体、颜色、字号的设置
- 看懂二叉树的三种遍历
- Jsoup抓取数据