"Expensive gifts" Detailed shortest ideas

That Italy will not repeat them here. I want to talk about it is the idea.
First, we need to look at the problem backward. The relationship between the first person and the other person, as his relationship with the other characters, then we create a fictional character to 0, the right side of him and all the characters are all original value.
into thinking diagram is that each character is a point on the map, then redeem the offer is the edge, and then we build the reverse side, by a virtual point of departure, continually updated relaxation to a distance of 1, and finally the shortest distance that we have to answer.
Note: this problem there is a level of restrictions, so we enumerate to rank each character to the lower bound, their ranks + m for each interval upper bound to get the smallest one answer.

#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<math.h>
#include<stack>
#include<map>
#include<limits.h>
#include<vector>
#include<string.h>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
const int N = 3*1e6+5;
const int M = 2*1e5+5;
#define pi acos(-1)
#define INF 1e9
#define INM INT_MIN
#define MAX 205
#define pb(a)  push_back(a)
#define mk(a,b) make_pair(a,b)
#define dbg(x) cout << "now this num is " << x << endl;
#define sd(a) scanf("%d",&a)
#define sld(a) scanf("%lld",&a)
#define sdd(a,b) scanf("%d %d",&a,&b)
#define sddd(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pr(a) printf("%d\n",a)
#define plr(a) printf("%lld\n",a)
#define pr_(a) printf("%d ",a)
#define _pr(a) printf(" %d",a)
int n,m,cost[105][105],cnt = 1,head[105],dis[105],ans = INF;
struct Node
{
    int price,level,sum;
}p[105];//结构体存信息
struct Edge
{
    int to,next,cost;
}e[10005];
int djst(int ml,int mr)//区间上下界
{
    for(int i=0;i<=n;++i) dis[i] = INF;
    dis[0] = 0;//虚拟点
    priority_queue<pii,vector<pii>,greater<pii> > Q;
    Q.push(pii(0,dis[0]));
    while(!Q.empty())
    {
        int u = Q.top().first,d = Q.top().second;
        Q.pop();
        if(dis[u] < d) continue;
        for(int i=head[u];i;i=e[i].next)
        {
            int y = e[i].to;
            if(p[y].level >= ml && p[y].level <= mr && dis[y] > dis[u]+e[i].cost)
            {
                dis[y] = dis[u]+e[i].cost;
                Q.push(pii(y,dis[y]));
            }
        }
    }
    return dis[1];
}
void add(int u,int v,int w)
{
    e[cnt].to = v;
    e[cnt].cost = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}
int main()
{
    sdd(m,n);
    for(int i=1;i<=n;++i)
    {
        sddd(p[i].price,p[i].level,p[i].sum);
        int t = p[i].sum;
        while(t--)
        {
            int x,z;
            sdd(x,z);
            add(x,i,z);//反向建边
        }
        add(0,i,p[i].price);//虚拟点到所有人的距离就是他们自己的价格
    }
    for(int i=1;i<=n;++i)//不断枚举区间更新
    {
        int ml = p[i].level;//划定下界
        ans = min(ans,djst(ml,ml+m));
    }
    pr(ans);
}
Published 14 original articles · won praise 0 · Views 2942

Guess you like

Origin blog.csdn.net/weixin_45671214/article/details/104352926