【POJ 3635】 Full Tank
【题目链接】
http://poj.org/problem?id=3635
【算法】
优先队列BFS
实现类似于堆优化dijkstra
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1010
#define MAXM 10010
#define MAXC 110
const int INF = 2e9; int i,n,m,q,tot,u,v,d,c,s,t,ans;
int head[MAXN];
bool visited[MAXN][MAXC];
int dist[MAXN][MAXC];
int p[MAXN]; struct info
{
int pos,w,d;
friend bool operator < (info a,info b)
{
return a.d > b.d;
}
} ;
struct Edge
{
int to,w,nxt;
} e[MAXM<<]; inline void add(int u,int v,int w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
}
inline int bfs(int c,int s,int t)
{
int i,j;
priority_queue< info > q;
info cur;
for (i = ; i <= n; i++)
{
for (j = ; j <= c; j++)
{
dist[i][j] = INF;
}
}
memset(visited,false,sizeof(visited));
dist[s][] = ;
while (!q.empty()) q.pop();
q.push((info){s,,});
while (!q.empty())
{
cur = q.top();
q.pop();
if (visited[cur.pos][cur.w]) continue;
if (cur.pos == t) return dist[cur.pos][cur.w];
visited[cur.pos][cur.w] = true;
if (cur.w < c)
{
if (dist[cur.pos][cur.w] + p[cur.pos] < dist[cur.pos][cur.w+])
{
dist[cur.pos][cur.w+] = dist[cur.pos][cur.w] + p[cur.pos];
q.push((info){cur.pos,cur.w+,dist[cur.pos][cur.w+]});
}
}
for (i = head[cur.pos]; i; i = e[i].nxt)
{
if (cur.w >= e[i].w)
{
if (dist[cur.pos][cur.w] < dist[e[i].to][cur.w-e[i].w])
{
dist[e[i].to][cur.w-e[i].w] = dist[cur.pos][cur.w];
q.push((info){e[i].to,cur.w-e[i].w,dist[e[i].to][cur.w-e[i].w]});
}
}
}
}
return -;
} int main()
{ scanf("%d%d",&n,&m);
for (i = ; i < n; i++) scanf("%d",&p[i]);
for (i = ; i <= m; i++)
{
scanf("%d%d%d",&u,&v,&d);
add(u,v,d);
add(v,u,d);
}
scanf("%d",&q);
while (q--)
{
scanf("%d%d%d",&c,&s,&t);
ans = bfs(c,s,t);
if (ans == -) printf("impossible\n");
else printf("%d\n",ans);
} return ; }
【POJ 3635】 Full Tank的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- UVM基础之----uvm_object
uvm_void The uvm_void class is the base class for all UVM classes. uvm_object: The uvm_object class ...
- CNN-CV识别简史2012-2017:从 AlexNet、ResNet 到 Mask RCNN
原文:计算机视觉识别简史:从 AlexNet.ResNet 到 Mask RCNN 总是找不到原文,标记一下. 一切从这里开始:现代物体识别随着ConvNets的发展而发展,这一切始于2 ...
- 论文deadline 最后三天
2015.12.29 星期二 内容整改 2015.12.30 星期三 参考文献,摘要等 2015.12.31 星期四 最后修改 尽最大的努力去做好论文的事情.
- (转) Arcgis for Javascript实现两个地图的联动
http://blog.csdn.net/gisshixisheng/article/details/40127895 今天在看天地图的时候,有一个多时相的地图显示功能,感觉很好玩,作为技术控的我晚上 ...
- 如何用windbg查看_eprocess结构
打开菜单: File->Symbol File Path... 输入: C:/MyCodesSymbols; SRV*C:/MyLocalSymbols*http://msdl.microsof ...
- PHP常用的设计模式
工厂模式 工厂模式是我们最常用的实例化对象模式,是用工厂方法代替new操作的一种模式. 使用工厂模式的好处是如果你想要更改所实例化的类名等,则只需要更改该工厂方法内容即可,不需逐一寻找代码中具体实例化 ...
- Python 不定长参数、全局变量、局部变量 day4
一.不定长参数 在函数定义中,经常会碰到*args 和**kwargs作为参数. 事实上在函数中,*和**才是必要的,args和kwargs可以用其他名称代替 *args 是指不定数量的非键值对参数. ...
- 1 TaskQueue 实现Task 队列
class Program { static void Main(string[] args) { List<Person> list = new List<Person>() ...
- strcpy & memcpy区别
这两个经常使用的函数,主要区别有: strcpy 返回值是char *, strcpy(x1, x2); x1 x2必须都是char* 类型 memcpy(x1, x2, sizeof(xx)); m ...
- Promise嵌套问题/async await执行顺序
/* 原则: 执行完当前promise, 会把紧挨着的then放入microtask队尾, 链后面的第二个then暂不处理分析, */ 一. new Promise((resolve, reject) ...