用的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搜索)。的更多相关文章

  1. POJ-3635 Full Tank? (记忆化广搜)

    Description After going through the receipts from your car trip through Europe this summer, you real ...

  2. POJ3635 Full Tank?(DP + Dijkstra)

    题目大概说,一辆带有一个容量有限的油箱的车子在一张图上行驶,每行驶一单位长度消耗一单位油,图上的每个点都可以加油,不过都有各自的单位费用,问从起点驾驶到终点的最少花费是多少? 这题自然想到图上DP,通 ...

  3. POJ3635 Full Tank?【Dijkstra+DP】

    题意: n个城市之间有m条双向路.每条路要耗费一定的油量.每个城市的油价是固定并且已经给出的.有q个询问,表示从城市s走到e,油箱的容量为c,求最便宜的方案. 思路: 用Dijkstra+Heap即可 ...

  4. POJ3635 Full Tank?

    [题解] 用dijkstra算法求最短路.同时考虑在每个节点加油(一单位)与否. [代码] #include <iostream> #include <map> #includ ...

  5. POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?

    然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...

  6. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  7. 2014 UESTC暑前集训搜索专题解题报告

    A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...

  8. csuoj 1511: 残缺的棋盘

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 ...

  9. hiho_1139_二分+bfs搜索

    题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分     最小化最大值,考虑采用二分搜索.对所有的边长进 ...

随机推荐

  1. SQL中的SELECT_简单查询语句总结

    --以scott用户下的dept和emp表为例 --注意:如果scott用户不能使用,请使用system用户登录--解锁scott用户ALTER USER SCOTT ACCOUNT UNLOCK;- ...

  2. 关于Android发送短信获取送达报告的问题

    最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...

  3. Tcl介绍和基础语法

    Tcl的背景 Tcl(读作tickle)诞生于80年代的加州大学伯克利分校,作为一种简单高效可移植性好的脚本语言,目前已经广泛应用在几乎所有的EDA工具中.Tcl 的最大特点就是其语法格式极其简单,采 ...

  4. vue-cli下面的config/index.js注解 webpack.base.conf.js注解

    config/indexjs详解上代码: 'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io ...

  5. python+selenium(python基础)

    1.编辑器的选择 好刀不误砍柴工,那么我们写代码也需要一个利器,虽然python自带有python shell ,但我们在执行代码的时候,需要开很多窗口,最重要的一点是,代码文件的管理很不方便,笔者推 ...

  6. (转)编写Spring的第一个案例并测试Spring的开发环境

    http://blog.csdn.net/yerenyuan_pku/article/details/52832145 Spring4.2.5的开发环境搭建好了之后,我们来编写Spring的第一个案例 ...

  7. js join()和split()方法、reverse() 方法、sort()方法

    ############  join()和split()方法  join() 方法用于把数组中的所有元素放入一个字符串. 元素是通过指定的分隔符进行分隔的. 指定分隔符方法join("#&q ...

  8. uva10163 Storage Keepers

    习题9-9 注意前提是最小值最大.很少做两次dp的题. 初始化要细心. #include<iostream> #include<cmath> #include<algor ...

  9. k8s部署测试实例

    查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...

  10. 模拟--P1328 生活大爆炸版石头剪刀布 题解

    P1328 生活大爆炸版石头剪刀布 这也是打表么?? #include <iostream> using namespace std; static const auto y = []() ...