CSUOJ 1336 Interesting Calculator (spfa算法)

题目:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 

题意:

分析:spfa资料1 spfa资料2

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 1e5+6;
const LL INF= 9e18;
int X,Y; 
bool visit[maxn];
int step[maxn],f[5][20];
LL cost[maxn];
queue <int > q; 
inline void slack(int u,int v,int w) //松弛操作 
{
	if(cost[v]>cost[u]+w || cost[v]==cost[u]+w && step[v]>step[u]+1)
	{
		cost[v]=cost[u]+w;
		step[v]=step[u]+1;
		if(!visit[v])
		{
			q.push(v);
			visit[v]=true;
		}
	}
}
void Init()
{
	memset(visit,false,sizeof(visit));
	fill(step,step+maxn,INF);
	fill(cost,cost+maxn,INF);
}
void spfa()
{
	q.push(X);
	visit[X]=1;
	step[X]=0;
	cost[X]=0;
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		visit[temp]=false; 
		for(int i=0;i<10;i++)
			if(temp*10+i<=Y)
				slack(temp,temp*10+i,f[0][i]);
		for(int i=0;i<10;i++)
			if(temp+i<=Y)
				slack(temp,temp+i,f[1][i]);
		for(int i=0;i<10;i++)
			if(temp*i<=Y)
				slack(temp,temp*i,f[2][i]);
	}
}

int main()
{
	int ncase=1;
	while(scanf("%d%d",&X,&Y)!=EOF)
	{
		for(int i=0;i<3;i++)
			for(int j=0;j<10;j++)
				scanf("%d",&f[i][j]);
		Init();
		spfa();
		printf("Case %d: %I64d %d\n",ncase++,cost[Y],step[Y]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值