• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yongchaoD

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

洛谷P1875 佳佳的魔法药水 (多起点dij)

题目:https://www.luogu.com.cn/problem/P1875
题意:一种药水可以直接买或者用另外两种药水合成。n种药水就,给出n种药水直接买的价钱,和m种合成方案。求得到0药水的最低价格和方案数。

思路:还是最短路问题,跑dij,但是是多个起点,枚举u药水和另一种来合成新药水,要保证合成用到的两瓶药水,都已经求出了两个的最短价格,才能求合成药水是否还有更便宜的价钱。
用一个vis数组,来标记i药水是否更新了最优价格。
接下来就是如何解决记录方案数问题,用一个ans[]数组来记录合成i药水的最便宜价格的方案数。
如果有i更便宜价格由a,b合成,ans[i]=ans[a]ans[b]。如果价格相同,ans+=ans[a]ans[b]。ans[]都初始化为1,因为有些药水只能买,不能被合成。

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+9;
int n,U,V,W;
vector<int>cost(N,0),vis(N,0),ans(N,0);
vector<pair<int,int>>G[N];

priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;

void dij(){
    while(!q.empty()){
        int a=q.top().first,u=q.top().second;//first(a):便宜水价格,second(u)便宜水种类
        q.pop();
        vis[u]=1;//标记u药水被访问过,这样合成一大药水,能知道两瓶小药水是否是最便宜价格
        for(auto [x,y]:G[u]){//枚举谁(x)能和u合成新药水(y)
            if(vis[x]){//另一瓶药水x确保已经被更新了最便宜价格
                if(cost[y]>a+cost[x]){//y药水有更优价格
                    cost[y]=a+cost[x];
                    ans[y]=ans[u]*ans[x];
                    q.push({cost[y],y});
                }
                else if(cost[y]==a+cost[x]){
                    ans[y]+=ans[u]*ans[x];
                }
            }
        }
    }
}

signed main()
{
    scanf("%lld",&n);
    for(int i=0;i<n;i++){
        scanf("%lld",&cost[i]);
        q.push({cost[i],i});//first:便宜水价格,便宜水种类
        ans[i]=1;//初始化每瓶药水的方案书:直接买
    }

    while(scanf("%lld%lld%lld",&U,&V,&W)!=EOF){//u+v=w
        G[U].push_back({V,W});
        if(U==V) continue;//狗屎数据,有u==v的情况
        G[V].push_back({U,W});
    }
    dij();
    printf("%lld %lld",cost[0],ans[0]);
    return 0;
}

posted on 2024-07-06 16:40  yongchaoD  阅读(23)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3