题目:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336
题意:
代码:
#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;
}