mltang

博客园 首页 新随笔 联系 订阅 管理

这题一开始没看清楚

等级差距不超过1

1->2->3 就是错误的,因为3-1==2 ,意思是间接的也不行

其次等级最小是1,最大是n

你要到达1号首领的位置

假设1号等级x,限制m,最大上限hi,下限lo

hi - lo <= m

lo <= x <= hi

枚举 hi 和 lo 

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 105;
const int INF = 0x3f3f3f3f;
int m,n;bool vis[maxn];
int dis[maxn];
int arr[maxn][maxn];
int lv[maxn];
int mind = 0x3f3f3f3f;
void djs(int ,int );
int main()
{
    int cnt = 0;
    memset(arr,0x3f,sizeof(arr));
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(lv,0x3f,sizeof(lv));
    scanf("%d%d",&m,&n);
    for(int i = 1; i <= n; ++i)
    {
        int mon,level,t;
        scanf("%d%d%d",&mon,&level,&t);
        lv[i] = level;
        arr[0][i] = mon;
        for(int j = 1; j <= t; ++j)
        {
            int c,d;
            scanf("%d%d",&c,&d);
            //e[cnt++] = node{i,d,c};
            arr[c][i] = d;
        }
    }
    for(int h = lv[1] + m,l = lv[1]; h >= lv[1]; h --,l -- )
        djs(h,l);
    cout << mind << endl;

}
void djs(int hi,int lo)
{
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[0] = 0;
    lv[0] = hi;
    for(int i = 0; i <= n; ++i)
    {
        int index = -1; int minn = 0x3f3f3f3f;
        for(int j = 0; j <= n; ++j)
        {
            if(vis[j]) continue;
            if(index == -1 || minn > dis[j])
                index = j,minn = dis[j];
        }
        vis[index] = true;
        for(int j = 0; j <= n; ++j)
        {
            if( (lo<=lv[j]&&lv[j]<=hi) || lv[index] == -1)
                dis[j] = min(dis[j],dis[index] + arr[index][j]);
        }
    }
      mind = min(mind,dis[1]);
}

 

posted on 2018-10-18 16:08  mltang  阅读(120)  评论(0编辑  收藏  举报