Topic Links:
http://poj.org/problem?id=1062
Although said is a Chinese question, said or looked for a long time to understand test instructions. Abstract the problem is a short-circuit problems,
But because there is a trading limit, the maximum level difference for all the people in the transaction is no more than M.
In fact, using the test data for an example is quite clear:
1 410000 3 22 80003 50001000 2 14 2003000 2 14 20050 2 0
The starting level limit is 1 (the maximum level difference), followed by 4 items. The first is the chief's object. In fact, the above is handled as
The figure is in the following form:
Number 1th is the chief, regardless of the level of the problem, then there are many ways to pay the money. For example, you can pay once, that is, 10000 yuan, of course
can also be used for 1000 yuan to buy a 2nd item, and then use 8000 yuan, so altogether is 9000 yuan. What do you see from it?
That is, starting from number 1th, assuming that the final choice is N, then it is the shortest short of the number 1th to N and the cost of buying n items is the least.
Spend... On this basis, consider the next level limit, you can directly enumerate the minimum or maximum level of a transaction. Suppose enumeration
is the smallest level of w, then the basis of the node's effectiveness is the rank between [w, W+m]. Then we can find the shortest circuit in these points.
Put Dijkstra write residual, seek the shortest distance, with the original path to compare, not with the updated distance, has been WA, looked for a long time to find ...
If you have been WA, you can try to carefully check the shortest possible ....
1#include <cstdio>2#include <iostream>3#include <algorithm>4 using namespacestd;5 /*6 POJ1062:7 */8 Const intINF =1e7;9 Const intMAXM = the ;Ten intM, N; One structnodes A { - intVal, lever; - }; the nodes GOODS[MAXM]; - intMAP[MAXM][MAXM], LEV[MAXM]; - intDIS[MAXM], RES[MAXM]; - BOOLVIS[MAXM]; + // - voidinput () { + for(inti =0; i < MAXM; i++ ){ A for(intj =0; J < Maxm; J + +) Map[i][j] =INF; at //Map[i][i] = 0; - } -scanf"%d%d", &m, &N); - for(inti =1; I <= N; i++ ){ - intnum; -scanf"%d%d%d", &goods[i].val, &goods[i].lever, &num); inlev[i-1] =Goods[i].lever; - for(intj =0; J < num; J + + ){ to inttmp1, TMP2; +scanf"%d%d", &TMP1, &tmp2); -MAP[I][TMP1] =TMP2; the } * } $ }Panax Notoginseng // - voidDijkstraintu) { the //cout << "U" << u << endl; + for(inti =1; I <= N; i++ ){ ADis[i] =INF; theVis[i] =false ; + } -dis[1] =0 ; $ for(inti =1; I <= N; i++ ){ $ intTMP = INF, k =i; - if(Goods[i].lever < U | | goods[i].lever > U+M)Continue ; - //cout << i << Endl; the for(intj =1; J <= N; J + + ){ - if(Vis[j])Continue ;Wuyi if(tmp > DIS[J] && goods[j].lever >= u && goods[j].lever <= u+m) {/////tmp > Map[i][j] the //cout << "ssss" << Endl; -TMP =Dis[j]; WuK =J; - } About } $ //cout << "K" << K << Endl; -VIS[K] =true ; - if(Dis[k] = = INF) Break ; - for(intt =1; T <= N; t++ ){ A if(Goods[t].lever >= u && goods[t].lever <= u+m &&!)Vis[t]) +Dis[t] = min (dis[t], dis[k]+map[k][t]); the } - } $ for(inti =1; I <= N; i++ ){ theRes[i] =min (res[i], dis[i]); the } the } the // - voidsolve () { inSort (Lev, lev+N); the for(inti =1; I <= N; i++) Res[i] =INF; the intU =-1 ; About for(inti =0; i < N; i++ ){ the if(Lev[i] = = u)Continue ; theU =Lev[i]; the if(goods[1].lever <= u+m && goods[1].lever >=u) { + Dijkstra (u); - } the }Bayi intASW =INF; the for(inti =1; I <= N; i++ ){ theASW = min (ASW, res[i]+goods[i].val); - } -cout << ASW <<Endl; the } the // the intMain () { the //freopen ("1234.txt", "R", stdin); - input (); the solve (); the return 0 ; the}
POJ1062 the expensive dowry