题意:

已知每一个点的加油站的油价单位价格(即点权)。每条路的长度(边权)。

有q个询问。每一个询问包含起点s、终点e和油箱容量。

问从起点走到终点的最小花费。假设不可达输出impossible,否则输出最小的旅途费用。

算法:

事实上要分析状态= =感觉就像是dp。

最直接的想法是  每到一个点都加上要走到下一个点所须要的油量。可是走的路不同,究竟怎么处理加多少的问题呢?

因此想到分解状态。即拆点。每到一个点都+1单位的油量,然后把这个状态增加队列。另外假设如今油箱内的油足够达到下一点,

则更新状态,把新状态增加优先队列。

dp[i][j]表示到第i个城市剩余油量为j的花费。

简单的说。就是优先队列优化的最短路算法多了一维。

为了把全部可能的状态考虑到。

每到一个点仅仅有两种操作:1、加一单位的油   2、更新能走到的下一个点。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxm 10010
#define maxn 1010 using namespace std; struct Edge
{
int to,val,next;
}edge[maxm*2]; struct node
{
int id,cost,oil;
bool operator <(const node &b) const
{
return cost > b.cost;
}
};
bool vis[maxn][110];
int cnt,head[maxn],dp[maxn][110],ans,st,ed,cap,p[maxn];
priority_queue<node> q; void add(int x,int y,int z)
{
edge[cnt].to = y;
edge[cnt].val = z;
edge[cnt].next = head[x];
head[x] = cnt++;
} bool bfs()
{
memset(dp,0x3f,sizeof(dp));
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
int id,oil,cost;
ans = 0; dp[st][0] = 0;
node now,cur,next;
now.id = st;
now.cost = 0;
now.oil = 0;
q.push(now);
while(!q.empty())
{
cur = q.top();
q.pop();
id = cur.id,oil = cur.oil,cost = cur.cost;
if(id == ed)
{
ans = cost;
return true;
}
if(vis[id][oil]) continue;
vis[id][oil] = true;
if(oil < cap)
{
next.id = id;
next.cost = cost+p[id];
next.oil = oil+1;
if(dp[next.id][next.oil]>next.cost && !vis[next.id][next.oil])
{
dp[next.id][next.oil] = next.cost;
q.push(next);
}
}
for(int i=head[id];i!=-1;i = edge[i].next)
{
int u = edge[i].to;
int w = edge[i].val;
if(oil>=w && dp[u][oil-w]>cur.cost && !vis[u][oil-w])
{
next.oil = oil-w;
next.id = u;
next.cost = cost;
dp[u][next.oil] = next.cost;
q.push(next);
}
}
}
return false;
} int main()
{
int n,m,u,v,l,que;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
cnt = 0; for(int i=0;i<n;i++)
scanf("%d",&p[i]);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&l);
add(u,v,l);
add(v,u,l);
}
scanf("%d",&que);
while(que--)
{
scanf("%d%d%d",&cap,&st,&ed);
if(bfs())
printf("%d\n",ans);
else printf("impossible\n");
}
}
return 0;
}

poj 3635 Full Tank? ( 图上dp )的更多相关文章

  1. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  2. zoj1232Adventure of Super Mario(图上dp)

    题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...

  3. POJ 3635 Full Tank? 【分层图/最短路dp】

    任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. poj 3635 Full Tank? ( bfs+dp思想 )

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5857   Accepted: 1920 Descri ...

  5. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  6. 洛谷 P2656 (缩点 + DAG图上DP)

    ### 洛谷 P2656 题目链接 ### 题目大意: 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖 ...

  7. Codeforces 918D MADMAX 图上dp 组合游戏

    题目链接 题意 给定一个 \(DAG\),每个边的权值为一个字母.两人初始各占据一个顶点(可以重合),轮流移动(沿着一条边从一个顶点移动到另一个顶点),要求每次边上的权值 \(\geq\) 上一次的权 ...

  8. [POJ 3635] Full Tank?

    题目 Description 已知每个点的加油站的油价单价(即点权),每条路的长度(边权). 有q个询问,每个询问包括起点s.终点e和油箱容量c. 问从起点走到终点的最小花费.如果不可达输出impos ...

  9. [Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3119 Solution 这题显然要先把缩点做了. 然后我们就可以考虑如何处理走反向边的问题. 像我这样的 ...

随机推荐

  1. LoadRunner error -27498

    URL=http://172.18.20.70:7001/workflow/bjtel/leasedline/ querystat/ subOrderQuery.do错误分析:这种错误常常是因为并发压 ...

  2. 【LeetCode】38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  3. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  4. poj 2196 Specialized Four-Digit Numbers

    如果一个数字 十进制的各位数的和 == 十六进制的各位数的和 == 十二进制的各位数的和,则输出,从2992到9999 #include <cstdio> int toDD(int n) ...

  5. [WinForm] 使用 WebBrowser 操作 HTML 頁面的 Element-摘自网络

    前言 在 Window Form 應用程式如果需要瀏覽網頁時可以崁入 WebBrowser 控制項,但如果需要操作崁入的 HTML 的網頁元素,就需要額外的操作,以下紀錄幾種操作 HTML 元素的方法 ...

  6. First & First

    First有记录则返回,否则返回null FirstOrDefault有记录则返回,否则NEW一个新的实体对象返回  

  7. 服务框架Dubbo(转)

    add by zhj:该开源项目已经停止更新了,不过倒是可以学学该软件的架构设计 原文:http://www.oschina.net/p/dubbo Dubbo 是阿里巴巴公司开源的一个高性能优秀的服 ...

  8. js 控制 table style css

    var table = objj.getElementsByTagName('table'); alert(table[i].width); if(table==null) return; for(v ...

  9. LoadRunner执行自动化以及报告自动化的方法

    There are three major articles KB articles on Automating LR: 1. Command line arguments for the LoadR ...

  10. Uncaught TypeError: Object #<Object> has no method 'fancybox'

    Uncaught TypeError: Object #<Object> has no method 'fancybox' 2011-10-24 16:51:19|  分类: html|举 ...