poj 3635/hdu 1676 Full Tank? 车辆加油+最短路

http://acm.hdu.edu.cn/showproblem.php?pid=1676

给出一张图,n<=1000,m<=10000. 有一辆车想从图的一个地方到达另外一个地方,每个点是一个卖油的地方,每个地方买的有价格不一样,车的最大装油量是c<=100,求初始点到终止点的最小花费。

可以 dp[i][j] 表示走到i点剩余j个单位的汽油时的最小花费,难点在于车需要加油,加多少油,转换问题后发现还是简单的bfs

就是维护一个费用优先队列,每到达一个节点都有两种可扩展的状态,一是加一个单位的油,二是走向邻接点,然后不断的将状态加入优先队列中

适当剪枝即可,vis控制一种状态只更新一次

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 0x7fffffff;
const int maxn = 1005,maxm = 10005;
struct edge{
    int v,w,next;
    edge(){};
    edge(int vv,int ww,int nnext):v(vv),w(ww),next(nnext){};
}e[maxm<<1];
int head[maxn],p[maxn],ecnt,dp[maxn][105];//i城市j
bool vis[maxn][105];
int n,m,c,st,en;
void add(int u,int v,int w)
{
    e[ecnt] = edge(v,w,head[u]);
    head[u] = ecnt++;
    e[ecnt] = edge(u,w,head[v]);
    head[v] = ecnt++;
}
void init()
{
    clr1(head),ecnt = 0;
}
struct node{
    int u,cost,last;
    node(){};
    node(int uu,int cc,int tt):u(uu),cost(cc),last(tt){};
    bool operator < (const node &a)const{
        return a.cost < cost;
    }
};
void bfs()
{
    for(int i = 0;i < n;++i)
        for(int j = 0;j <= 100;++j)
            dp[i][j] = inf;
    clr0(vis);
    priority_queue<node> q;
    q.push(node(st,0,0));
    dp[st][0] = 0;
    while(!q.empty()){
        node cur = q.top();q.pop();
        int u = cur.u,cost = cur.cost,last = cur.last;
        if(u == en){
            printf("%d\n",cost);
            return ;
        }
        vis[u][last] = 1;
        if(last + 1 <= c && !vis[u][last + 1] && dp[u][last + 1] > dp[u][last]+p[u]){
            dp[u][last+1] = dp[u][last] + p[u];
            q.push(node(u,dp[u][last+1],last+1));
        }
        for(int i = head[u];i != -1;i = e[i].next){
            int v = e[i].v,w = e[i].w;
            if(last >= w && !vis[v][last - w] && dp[v][last - w] > cost){
                dp[v][last - w] = cost;
                q.push(node(v,cost,last - w));
            }
        }
    }
    puts("impossible");
}
int main()
{
    init();
    RD2(n,m);
    for(int i = 0;i < n;++i)
        RD(p[i]);
    int u,v,w,q;
    while(m--){
        RD3(u,v,w);
        add(u,v,w);
    }
    RD(q);
    while(q--){
        RD3(c,st,en);
        bfs();
    }
    return 0;
}
时间: 2024-08-26 00:50:30

poj 3635/hdu 1676 Full Tank? 车辆加油+最短路的相关文章

poj 3635 Full Tank? ( 图上dp )

题意: 已知每个点的加油站的油价单价(即点权),每条路的长度(边权). 有q个询问,每个询问包括起点s.终点e和油箱容量. 问从起点走到终点的最小花费.如果不可达输出impossible,否则输出最小的旅途费用. 算法: 其实要分析状态= =感觉就像是dp. 最直接的想法是  每到一个点都加上要走到下一个点所需要的油量.但是走的路不同,到底怎么处理加多少的问题呢? 因此想到分解状态,即拆点.每到一个点都+1单位的油量,然后把这个状态加入队列.另外如果现在油箱内的油足够达到下一点, 则更新状态,把

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符

POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Description A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they have the same length, and

POJ 2492 || HDU 1829:A Bug&#39;s Life(并查集)

传送门: POJ:点击打开链接 HDU:点击打开链接 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 27624   Accepted: 8979 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visit

POJ 2104&amp;HDU 2665 Kth number(主席树入门+离散化)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

最小生成树,POJ和HDU几道题目的解题报告(基于自己写的模板)

首先POJ题目: 链接:1251 Jungle Roads 题目大意:纯求最小生成树,结果为最小权值边的和.采用邻接表 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 #define maxn 30 //最大顶点个数 8 int n; //顶点数,边数 9 10 struct arcn

POJ 1201 &amp;&amp; HDU 1384 Intervals(差分约束系统)

题目地址:POJ 1201   HDU 1384 根据题目意思,可以列出不等式如下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路spfa来解决这个不等式.用max来当源点,0为终点.最终的-d[0]就是答案. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <