POJ - 1062(超级源点 + dijkstra或spfa)

题目链接

思路:
建图:
1.以S = 0 为超级源点,所有的点到由S到该点的距离等于其本身的价格,
2.每一个物品可以由替代还品 + 优惠价得到,也就是替代品->原物品的距离为优惠价
最后再枚举满足长等级的所有区间跑dijkstra取最小值

代码:

#include <iostream>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include <vector>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int N = 110;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;

int m, n, S; //S为超级源点
int lv[N], dis[N];
vector<PII> a[N];
bool vis[N];

int dijkstra(int l, int r)
{
    memset(dis, INF, sizeof dis);
    memset(vis, false, sizeof vis);
    priority_queue<PII, vector<PII>, greater<PII>> pq;
    dis[S] = 0;
    pq.push({0, S});
    while (pq.size())
    {
        PII t = pq.top();
        pq.pop();
        int idx = t.second, distance = t.first;
        if (vis[idx])
            continue;
        vis[idx] = true;
        for (int i = 0; i < a[idx].size(); i++)
        {
            int j = a[idx][i].second, w = a[idx][i].first;
            if (lv[j] >= l && lv[j] <= r) //判断此时加入点是否符合等级区间
            {
                if (dis[j] > distance + w)
                {
                    dis[j] = distance + w;
                    pq.push(PII(dis[j], j));
                }
            }
        }
    }
    return dis[1];
}

int main()
{
    cin >> m >> n;
    for (int i = 1; i <= n; i++)
    {
        int p, l, x;
        cin >> p >> l >> x;
        a[S].push_back({p, i});
        lv[i] = l;
        for (int j = 1; j <= x; j++)
        {
            int l, w;
            cin >> l >> w;
            a[l].push_back(PII(w, i)); //替代物品到原物品 i 的距离为优惠价w
        }
    }
    int ans = INF;
    for (int i = lv[1] - m; i <= lv[1]; i++) //枚举满足酋长等级的所有等级区间
    {
        int l = i, r = i + m;
        ans = min(ans, dijkstra(l, r));
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值