POJ 3635 Full Tank?

53 篇文章 1 订阅
37 篇文章 0 订阅

Full Tank?

Time Limit: 1000MS Memory Limit: 65536K
Description
After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?
To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.
Input
The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ u, v < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.
Output
For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or “impossible” if there is no way of getting from s to e with the given car.
Sample Input
5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4
Sample Output

170
impossible
Source

Nordic 2007

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

网上大部分的思路都是类似于dij的那种扩展。
首先定义一个二维数组dp。 dp[i][j] 表示走到i点剩余j个单位的汽油时的最小花费
然后维护一个优先队列。 每次有两种可扩展的状态,一是加一个单位的油,二是走向邻接点,然后不断的将状态加入优先队列中

心情不来刷一发搜索

//长得像Dijkstra的BFS
//刚开始都没想到用dp并且思路很乱,不是一个单位一个单位加油的,一次加好多,
//类似贪心的一个思路,结果.............
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
#define MAXN 1005
#define MAXM 100005
#define INF 100000000
struct Edge{ int to,w,next; }e[MAXM];
struct Node{
    int v,cost,f;
    bool operator < (const Node &a) const {
        return a.cost < cost;
    }
};
int head[MAXN],tot,n,m,cap;
int dp[MAXN][105],used[MAXN][105],p[MAXN];
int s,t,ask;
priority_queue<Node> q;
void prepare(){
    for(int i=0;i<=n;++i)
        for(int j=0;j<=100;++j) dp[i][j]=INF;
    dp[s][0]=0;
    memset(used,0,sizeof used );
    while(!q.empty()) q.pop();
}

inline void Add_Edge(int u,int v,int w){
    e[++tot].to=v,e[tot].w=w,e[tot].next=head[u],head[u]=tot;
}

int Dijkstra(){
    Node pc,pk; pc.v=s; pc.cost=0; pc.f=0; q.push(pc);
    while(!q.empty()){
        pc=q.top();q.pop();
        int u=pc.v,cost=pc.cost,f=pc.f;
        used[u][f]=1;
        if(u==t) return cost;
        if(f+1<=cap&&!used[u][f+1]&&dp[u][f+1]>dp[u][f]+p[u]){
            dp[u][f+1]=dp[u][f]+p[u];
            pk.v=u; pk.f=f+1; pk.cost=dp[u][f+1];
            q.push(pk);
        }
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to,w=e[i].w;
            if(f>=w&&!used[v][f-w]&&dp[v][f-w]>cost){
                dp[v][f-w]=cost,pk.v=v,pk.f=f-w;
                pk.cost=dp[v][f-w];q.push(pk);
            }
        }
    }
    return -1;
}
int main(){ 
    scanf("%d%d",&n,&m);
    memset(head,-1,sizeof head ); tot=0;
    for(int i=0;i<n;i++) scanf("%d",&p[i]);//price
    for(int u,v,w,i=1;i<=m;++i){
        scanf("%d%d%d",&u,&v,&w);
        Add_Edge(u,v,w),Add_Edge(v,u,w);
    }
    scanf("%d",&ask);
    for(int u,v,i=1;i<=ask;++i){
        scanf("%d%d%d",&cap,&s,&t);
        prepare();
        int ans=Dijkstra();
        if(ans!=-1) printf("%d\n",ans);
        else printf("impossible\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
POJ3635是一道经典的数学题,需要使用一些数学知识和算法进行解决。 题目描述: 给定四个正整数 a、b、p 和 k,求 a^b^p mod k 的值。 解题思路: 首先,我们可以将指数 b^p 写成二进制形式:b^p = c0 * 2^0 + c1 * 2^1 + c2 * 2^2 + ... + ck * 2^k,其中 ci 为二进制数的第 i 位。 然后,我们可以通过快速幂算法来计算 a^(2^i) mod k 的值。具体来说,我们可以用一个变量 x 来存储 a^(2^i) mod k 的值,然后每次将 i 加 1,如果 ci 为 1,则将 x 乘上 a^(2^i) mod k,最后得到 a^b^p mod k 的值。 代码实现: 以下是 Java 的代码实现: import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); BigInteger a = sc.nextBigInteger(); BigInteger b = sc.nextBigInteger(); BigInteger p = sc.nextBigInteger(); BigInteger k = sc.nextBigInteger(); BigInteger ans = BigInteger.ONE; for (int i = 0; i < p.bitLength(); i++) { if (b.testBit(i)) { ans = ans.multiply(a.modPow(BigInteger.ONE.shiftLeft(i), k)).mod(k); } } System.out.println(ans); } } 其中,bitLength() 函数用于获取二进制数的位数,testBit() 函数用于判断二进制数的第 i 位是否为 1,modPow() 函数用于计算 a^(2^i) mod k 的值,multiply() 函数用于计算两个 BigInteger 对象的乘积,mod() 函数用于计算模数。 时间复杂度: 快速幂算法的时间复杂度为 O(log b^p),其中 b^p 为指数。由于 b^p 的位数不超过 32,因此时间复杂度为 O(log 32) = O(1)。 总结: POJ3635 是一道经典的数学题,需要使用快速幂算法来求解。在实现时,需要注意 BigInteger 类的使用方法,以及快速幂算法的细节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值