poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。
用的BFS+优先队列+二进制压缩状态判重+链式前向星, TLE,好像有人这样过了。。。好像要用A*算法,还不太会,所以暂时放弃。但是也学会了很多,学习了链式前向星,更深理解了BFS求最优的时候,什么时候是第一次搜到结果就是最优,该题,通过枚举加的油量,每次加一个单位,从够下一条路开始到满容量,枚举所有路,花的钱少的在队优先(头),故先出队找到目标结点的必然最优,因为后面的都是前面再加钱的。。。。好好想想。。。
#include<iostream> //链式前向星+二进制状态压缩判重+优先队列
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int prize[1002];
struct edge
{
int pre; //该边关于该点的前一条边
int to; //通哪个点
int w; //权
};
struct state
{
int dian;
int key; //二进制压缩判重
int cur_money;
int cur_fuel;
state()
{
key=0;
}
bool operator <(const state & a)const //按花费小的排序
{
return a.cur_money<cur_money;
}
};
edge bian[20002];
int head[1002]; //每个顶点的边的头指针
int mincost=0x3f3f3f3f;
void bfs(int from,int capacity,int to)
{
priority_queue<state>q;
state start;
start.dian=from;
start.key=(start.key|(1<<from));
start.cur_fuel=0;
start.cur_money=0;
q.push(start);
while(!q.empty())
{
state cur=q.top();
q.pop();
if(cur.cur_money>=mincost)continue;
if(cur.dian==to)
{
if(cur.cur_money<mincost)
mincost=cur.cur_money;
return;
//continue;
}
for(int j=head[cur.dian];j!=-1;j=bian[j].pre) //枚举边
{
if ((cur.key&(1<<bian[j].to))!=0)continue; //判重(后来知道这样判重是不对的,可以重顶点,要顶点+油量双重判重才可以)
for(int i=0;i+cur.cur_fuel<=capacity;i++) //充i油,前进。
{
state next(cur);
if(cur.cur_fuel+i<bian[j].w)continue; //不够路费的剪枝
next.cur_fuel=next.cur_fuel+i-bian[j].w;
next.cur_money=next.cur_money+i*prize[cur.dian];
if(next.cur_money>=mincost)break; //最优性剪枝
next.dian=bian[j].to;
if(next.dian==to)
{
if(next.cur_money<mincost)
mincost=next.cur_money;
break;
}
q.push(next);
next.key=(next.key|(1<<bian[j].to));
}
}
}
return ;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&prize[i]);
int s,e,lenth;
memset(head,-1,sizeof(head));
for(int i=0;i<2*m;i++)
{
scanf("%d%d%d",&s,&e,&lenth);
bian[i].to=e;
bian[i].pre=head[s]; //head[s]:顶点s的某边,只是暂时存储,链接俩个边关系作用。
head[s]=i;
bian[i].w=lenth;
i++;
bian[i].to=s;
bian[i].pre=head[e]; //head[s]:顶点s的某边,只是暂时存储,链接俩个边关系作用。
head[e]=i;
bian[i].w=lenth;
}
/*for(int k=0;k<n;k++)
for(int j=head[k];j!=-1;j=bian[j].pre) //枚举边
{
printf("%d to %d has %d\n",k,bian[j].to,bian[j].w);
}*/
int que;scanf("%d",&que);
int capacity,from,to;
while(que--)
{
mincost=0x3f3f3f3f;
scanf("%d%d%d",&capacity,&from,&to);
if(head[to]==-1||head[from]==-1){printf("impossible\n");continue;}//无解
bool mark1=1;
for(int j=head[from];j!=-1;j=bian[j].pre) //枚举边
{
if(bian[j].w<=capacity){mark1=0;break;}
}
bool mark2=1;
for(int j=head[to];j!=-1;j=bian[j].pre) //枚举边
{
if(bian[j].w<=capacity){mark2=0;break;}
}
if(mark1||mark2){printf("impossible\n");continue;} //无解
bfs(from,capacity,to);
if(mincost!=0x3f3f3f3f)printf("%d\n",mincost);
else printf("impossible\n");
}
return 0;
}
poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。的更多相关文章
- POJ-3635 Full Tank? (记忆化广搜)
Description After going through the receipts from your car trip through Europe this summer, you real ...
- POJ3635 Full Tank?(DP + Dijkstra)
题目大概说,一辆带有一个容量有限的油箱的车子在一张图上行驶,每行驶一单位长度消耗一单位油,图上的每个点都可以加油,不过都有各自的单位费用,问从起点驾驶到终点的最少花费是多少? 这题自然想到图上DP,通 ...
- POJ3635 Full Tank?【Dijkstra+DP】
题意: n个城市之间有m条双向路.每条路要耗费一定的油量.每个城市的油价是固定并且已经给出的.有q个询问,表示从城市s走到e,油箱的容量为c,求最便宜的方案. 思路: 用Dijkstra+Heap即可 ...
- POJ3635 Full Tank?
[题解] 用dijkstra算法求最短路.同时考虑在每个节点加油(一单位)与否. [代码] #include <iostream> #include <map> #includ ...
- POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?
然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
- 2014 UESTC暑前集训搜索专题解题报告
A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...
- csuoj 1511: 残缺的棋盘
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边长进 ...
随机推荐
- ETH Dapp 体验报告
Dapp 体验报告 Dapp是分散式的应用程序.DApp运行在去中心化的网络上,也就是区块链网络中.网络中不存在中心化的节点可以完整的控制DApp. 必须依赖合约部署,没有一个中心化的服务器托管. 对 ...
- Vue踩坑第一步,安装Vue最新版本
学习vue第一步肯定是安装vue-cli,那么肯定想去搜下如何安装vue-cli呢? 网上搜到的结果大都是: npm i vue-cli -g 输入vue -V发现: 输入node -v发现: 自己明 ...
- Linux OpenGL 实践篇-10-framebuffer
在之前的实践中我们都是在当前的窗口中渲染,即使用的缓存都是由glutCreateWindow时创建的缓存,我们可称之为默认缓存.它是唯一一个可以被图形服务器的显示系统识别的帧缓存,我们在屏幕上看到的只 ...
- mysql中的 enum (枚举)
mysql enum是指字段的类型 表示枚举类型 mysql> alter table student add adders enum("sichuang","sh ...
- autoOpenBrowser: true, 运行npm后自动打开浏览器
autoOpenBrowser: true, 运行npm后自动打开浏览器
- 解决margin塌陷问题
父元素添加: border: 1px solid transparent; 或者 over-flow:hidden;
- c++如何使用全局变量
在xxxx.h文件中使用extern声明变量: extern int i; 在xxxx.cpp文件中定义变量: int i; 声明和定义都只需一次.
- ideal取消按下两次shift弹出搜索框 修改idea,webstrom,phpstrom 快捷键double shift 弹出search everywhere
因为经常需要在中英文之间切换,所以时常使用shift键,一不小心就把这个Searchwhere 对话框调出来了,很是麻烦. 因此痛定思痛, 我决定将这个按两下shift键就弹出搜索框的快捷键禁用了! ...
- 简单批处理命令直接启动你的AVD
大家都知道,要想启动AVD,一般方法是先打开Android SDK and AVDmanager,再选择你要启动的AVD选择start(废话) 那么,有没有一种简单的方法在任何位置一键启动你指定的av ...
- nginx发布web网站
修改/conf/nginx.conf配置文件 server { listen *:; # Listen server_name ""; # Don't worry if " ...