给定数字n,m(1<=n,m<=500000)
将n变为n*2花费2,将n变为n-3花费3,要求过程中所有数字都在[1,500000]区间内。
求将n变为m的最少花费
思路:建图
将每个数字视为图中的点,数字之间的转换视为图中的边,有向图。
500000个点,(i,i*2)权值为2,(i,i-3)权值为3
转换为求n至m的最短路径
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int n,m;
vector<pair<long long,int>> edge[];
long long dis[];
typedef pair<long long,int> P;//first 最短距离,second顶点编号 void dijkstra(int s)
{
memset(dis,INF, sizeof(dis));
priority_queue<P,vector<P>,greater<P>> que; //最小堆
que.push(P(,s));
dis[s]=;
while(que.size())
{
P p=que.top();que.pop();
int v = p.second;
//vis[v]=1;
if(dis[v]<p.first)continue;
for(int i=;i<edge[v].size();i++)
{
int to = edge[v][i].second;
long long cost = edge[v][i].first;
//if(!vis[to]&&dis[to]>dis[v]+cost)
if(dis[to]>dis[v]+cost)
{
dis[to]=dis[v]+cost;
que.push(P(dis[to],to));
}
}
}
if(dis[m]==INF)
cout<<-<<endl;
else
cout<<dis[m];
} int main() {
cin >> n >> m;
for (int i = ; i <= ; i++)
{
if(*i<)
edge[i].push_back(make_pair(,*i));
if(i->)
edge[i].push_back(make_pair(,i-));
}
dijkstra(n);
return ;
}
思路2:暴力BFS
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
int n,m;
struct node
{
int x;
LL cost;
};
int vis[]; int main() {
cin >> n >> m;
node cur,now;
cur.x=n,cur.cost=;
queue<node> q;
q.push(cur);
LL ans=1e18;
vis[cur.x]=;
while(q.size())
{
now=q.front();q.pop();
if(now.x==m)
{
ans=min(ans,now.cost);
continue;//剪枝
}
cur.x=now.x*;
cur.cost=now.cost+;
if(cur.x<=&&!vis[cur.x])
{
vis[cur.x]=;
q.push(cur);
}
cur.x=now.x-;
cur.cost=now.cost+;
if(cur.x>&&!vis[cur.x])
{
vis[cur.x]=;
q.push(cur);
}
}
if(ans==1e18) printf("-1\n");
else cout<<ans<<endl;
return ;
}