poj1062

因为有等级的限制条件,所以导致这道题瞬间变得鬼畜起来,那就想办法消除掉——通过枚举区间。

各个人物作为点,有贸易往来的作为边,优惠后的价格作为边的权值,则在某个点作为贸易终点娶得美人归的花费就是它的价值加上酋长到该点的最小花费

#include 
#include 
#include 
using namespace std;

const int N = 110;
const int inf = 0x3f3f3f3f;
int map[N][N], lev[N], d[N], value[N];
bool within_lim[N], v[N];//within_lim为满足等级限制的标记数组
int lev_lim, n;
int dijkstra(){
    int minimum = inf;
    memset(v, 0, sizeof(v));
    for(int i = 1; i <= n; i ++)   
        d[i] = (i == 1 ? 0 : inf);
    for(int i = 1; i <= n; i ++){
        int x = 0, m = inf;
        for(int y = 1; y <= n; y ++)
            if(! v[y] && d[y] <= m && within_lim[y]){//在所有未标号且满足等级限制的结点中,选出d值最小的结点x
                x = y;
                m = d[y];
            }
        v[x] = 1;
        for(int y = 1;y <= n;++y){
            if(within_lim[y])//满足等级限制
                d[y] = min(d[y], d[x] + map[x][y]);
        }
    }
    for(int i = 1;i <= n;i ++){
        d[i] += value[i];//对于每个d[i]值,还需加上进入该结点的花费,再进行比较
        if(d[i] < minimum)  minimum = d[i];
    }
    return minimum;
}
int main(){
    cin >> lev_lim >> n;
    for(int i = 0; i <= n; i ++)
        for(int j = 0; j <= n; j ++)
            map[i][j] = (i == j ? 0 : inf);
    for(int i = 1; i <= n; i ++){
        int t;
        cin >> value[i] >> lev[i] >> t;
        for(int j = 1; j <= t; j ++){
            int k;
            cin >> k;
            cin >> map[i][k];
        }
    }
    
    int kinglev = lev[1];
    int min_cost = inf, cost;
    for(int i = 0; i <= lev_lim; i ++){
        memset(within_lim, 0, sizeof(within_lim));
        for(int j = 1; j <= n;j ++)
            if(lev[j] >= kinglev - lev_lim + i && lev[j] <= kinglev + i)//枚举等级允许范围的结点
                within_lim[j] = 1;
        
        cost = dijkstra();
        if(cost < min_cost)
            min_cost = cost;
    }
    cout << min_cost << endl;
    return 0;
}

 

你可能感兴趣的:(poj1062)