P3403 跳楼机(同余最短路)

题目链接:点击这里

题目大意:
给定一个正整数 h h h ,三个正整数 a , b , c a,b,c a,b,c ,求满足 a x + b y + c z = n ( x , y , z ∈ N , n ≤ h ) ax+by+cz=n(x,y,z\in N,n\le h) ax+by+cz=n(x,y,zN,nh) 的方程数

题目分析:
同余最短路可以解决给你 n n n 个整数求他们能拼成 k k k 以内的多少个整数或用这 n n n 个数不能拼成的最大/最小值事多少的一种最短路的建图思路
对于本题:
因为对于任意的 a i a_i ai 可以拆分为 a i = x × b a s e + r ( 其 中 x = ⌊ a i b a s e ⌋ ) a_i=x\times base+r(其中x=\lfloor \frac{a_i}{base} \rfloor) ai=x×base+rx=baseai ,不妨取为 a 1 a_1 a1 (理论上这个 b a s e base base 取得越小越优秀,因为可以建更少数量的点)
我们考虑对于每一个余数 r r r 都建一个点并重新定义最短路的 d i s dis dis 数组的含义: d i s i dis_i disi 表示通过加其他的 a i a_i ai 能到达的最小的模 a 1 a_1 a1余数是 r r r 的数
据此定义容易得到其转移方程为 d i s ( d i s i + v a l ) m o d    a 1 = d i s i + v a l dis_{(dis_i+val)\mod a_1}=dis_i+val dis(disi+val)moda1=disi+val ,此转移方程同样可以用最短路算法来转移。转移方法就是对于每一个点 r r r 都向点 ( r + v a l ) m o d    a 1 (r+val)\mod a_1 (r+val)moda1 连一条权值为 v a l val val 的边,然后在这张图上以 0 0 0 为起点跑最短路即可
有了 d i s dis dis 数组,那么对于每一个余数 r r r 产生的贡献即为 ⌊ h − d i s r a 1 ⌋ + 1 \lfloor \frac{h-dis_r}{a_1} \rfloor +1 a1hdisr+1
可以证明的是每次的贡献都是来自同一剩余系的不同的元素,故贡献的产生是相互独立的,不会重复计算答案
PS:这个题数据思路没拉满,不然数据范围会被卡的很难受(感谢出题人

具体细节见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 1e6;
const int maxm = 5005;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
struct Edge{
	int nxt,to,val;
}edge[maxn];
int n,m,h,a[maxn],cnt,head[maxn],dis[maxn];
bool vis[maxn];
void addedge(int from,int to,int val)
{
	edge[++cnt].nxt = head[from];
	edge[cnt].to = to;
	edge[cnt].val = val;
	head[from] = cnt;
}
struct node{
	int id,dis;
	bool operator < (const node &b) const {
		return dis > b.dis;
	}
};
void dijkstra(int s)
{
	memset(dis,0x3f,sizeof(dis));
	dis[s] = 0;
	priority_queue<node>qu;
	qu.push({s,0});
	while(!qu.empty())
	{
		int h = qu.top().id;qu.pop();
		if(vis[h]) continue;
		vis[h] = true;
		for(int i = head[h];i;i = edge[i].nxt)
		{
			int to = edge[i].to,val = edge[i].val;
			if(dis[to] > dis[h]+val)
			{
				dis[to] = dis[h]+val;
				qu.push({to,dis[to]});
			}
		}
	}
}
int solve(int x)
{
	int res = 0;
	for(int i = 0;i < a[1];i++) 
		if(dis[i] <= x) res += (x-dis[i])/a[1]+1;
	return res;
}
signed main()
{
	n = 3;h = read();
	for(int i = 1;i <= n;i++) a[i] = read();
	for(int i = 0;i < a[1];i++)
		for(int j = 2;j <= n;j++) addedge(i,(i+a[j])%a[1],a[j]);
	dijkstra(0);
	printf("%lld\n",solve(h-1));
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值