poj3635—Full Tank?(spfa+dp)

题目链接:传送门

Full Tank?
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7521 Accepted: 2438

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 ≤ uv < 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


题目大意:一辆车从起点到终点,给定汽车的邮箱容量,给出每个点加每单位油的价格,点与点间的距离,(起点油量为0)计算所需最少的费用。


解题思路:spfa+dp,dp[i][j]记录汽车到达第i个点所剩j单位油所用的费用,维护一个优先队列,将产生的新的状态压入队列,前一个状态向下产生的两个新的状态1.汽车在原结点加一个单位的油2.汽车驶向下一个结点。


#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <iostream>  
#include <queue>
#include <set>
#include <string>
#include <cctype>
#include <vector>
#include <bitset>
using namespace std;  
const int size1 = 10100;
const int size2 = 1010;

struct Node
{
	int node,cost,oil;
	Node( int n , int c , int o ){ node = n ; cost = c ; oil = o; }
	bool operator < ( const Node& a ) const{
		return cost>a.cost;
	}
};
int cap[size2],dp[size2][105],vis[size2][105];
int c,s,e;

struct Edge
{
	int node,Len;
	Edge*next;
}m_edge[size1*2];
Edge*head[size2];
int Ecnt;

void mkEdge( int u , int v , int w )
{
	m_edge[Ecnt].node = u;
	m_edge[Ecnt].Len = w;
	m_edge[Ecnt].next = head[v];
	head[v] = m_edge+Ecnt++;

	m_edge[Ecnt].node = v;
	m_edge[Ecnt].Len = w;
	m_edge[Ecnt].next = head[u];
	head[u] = m_edge+Ecnt++;
}

void initData()
{
	Ecnt = 0;
	fill( head , head+size2 , (Edge*)0 );
}

void bfs()
{
	memset( vis , 0 , sizeof(vis) );
	memset( dp , 1 , sizeof(dp) );
	priority_queue<Node>p;
	p.push(Node(s,0,0));
	dp[s][0] = 0;
	while( !p.empty() ){
		Node q = p.top();
		p.pop();
		int n = q.node,cost = q.cost,o = q.oil;
		vis[n][o] = 1;

		if( n == e ){
			printf("%d\n",cost);
			return;
		}

		//加一个单位的油
		if( o+1<=c && !vis[n][o+1] && dp[n][o]+cap[n]<dp[n][o+1] ){
			dp[n][o+1] = dp[n][o]+cap[n];
			p.push(Node(n,dp[n][o+1],o+1));
		}

		//走到下一个结点
		for( Edge*k = head[n] ; k ; k = k->next ){
			int L = k->Len,N = k->node;
			if( o>=L && !vis[N][o-L] && cost<dp[N][o-L] ){
				dp[N][o-L] = cost;
				p.push(Node(N,dp[N][o-L],o-L));
			}
		}
	}
	printf("impossible\n");
}

int main()
{
	int n,m,u,v,w,q;
	scanf("%d%d",&n,&m);
	initData();
	for( int i = 0 ; i < n ; ++i ){
		scanf("%d",&cap[i]);
	}
	for( int i = 0 ; i < m ; ++i ){
		scanf("%d%d%d",&u,&v,&w);
		mkEdge( u , v , w );
	}
	scanf("%d",&q);
	for( int i = 0 ; i < q ; ++i ){
		scanf("%d%d%d",&c,&s,&e);
		bfs();
	}
	return 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值