poj 1026

时间:2022-06-03 16:13:27

这题一开始没看清楚

等级差距不超过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 = ;
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 = ;
memset(arr,0x3f,sizeof(arr));
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
memset(lv,0x3f,sizeof(lv));
scanf("%d%d",&m,&n);
for(int i = ; i <= n; ++i)
{
int mon,level,t;
scanf("%d%d%d",&mon,&level,&t);
lv[i] = level;
arr[][i] = mon;
for(int j = ; 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[] + m,l = lv[]; h >= lv[]; h --,l -- )
djs(h,l);
cout << mind << endl; }
void djs(int hi,int lo)
{
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
dis[] = ;
lv[] = hi;
for(int i = ; i <= n; ++i)
{
int index = -; int minn = 0x3f3f3f3f;
for(int j = ; j <= n; ++j)
{
if(vis[j]) continue;
if(index == - || minn > dis[j])
index = j,minn = dis[j];
}
vis[index] = true;
for(int j = ; j <= n; ++j)
{
if( (lo<=lv[j]&&lv[j]<=hi) || lv[index] == -)
dis[j] = min(dis[j],dis[index] + arr[index][j]);
}
}
mind = min(mind,dis[]);
}