Poj 1062 expensive dowry difficult AC

Source: Internet
Author: User

The Chinese Question is wrong.

Key Point: two people with a level gap greater than m cannot trade directly or indirectly, and 1 must be the end of the graph, therefore, the levels of all vertices in the shortest path must be in the range of [level [1]-M, level [1] + M]. Therefore, you can enumerate the levels,

For (I = level [1]-m; I <= level [1]; I ++)

{

Shortest PathAlgorithmMake sure that

The level of each vertex in the shortest path is between [I, I + M ].

}

In this way, the bloody AC is always so inspiring.

View code

# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
Const Int INF = 1000000000 ;
Int Lev [ 110 ], P [ 110 ];
Int Map [ 110 ] [ 110 ];
Int Vis [ 110 ];
Int Dis [ 110 ];
Int Dijkstra ( Int Cost [] [110 ], Int N, Int X, Int M)
{
Int I, J, K, minc;
Memset (VIS, 0 , Sizeof (VIS ));
Dis [ 0 ] = 0 ; Vis [ 0 ] = 1 ;
For (I = 1 ; I <n; I ++) dis [I] = cost [ 0 ] [I];
For (I = 1 ; I <n; I ++)
{
Minc = inf;
For (J = 0 ; J <n; j ++)
If (! Vis [J] & dis [J] <minc)
{
Minc = dis [J];
K = J;
}
If (Minc = inf) Break ;
Vis [k] = 1 ;
For (J = 0 ; J <n; j ++)
If (! Vis [J] & dis [J]> dis [k] + cost [k] [J] & lev[ K]> = x & lev[ K] <= X + M)
Dis [J] = dis [k] + cost [k] [J];
}
Return Dis [ 1 ];
}
Int Main ()
{
Int M, N;
Int I, J, K, X, price, ID;

While (Scanf ( " % D " , & M, & N )! = EOF)
{
For (I = 0 ; I <= 100 ; I ++)
{
Map [I] [I] =0 ;
For (J = I + 1 ; J <= 100 ; J ++)
{
Map [I] [J] = map [J] [I] = inf;
}
}
For (I = 1 ; I <= N; I ++)
{
Scanf ( " % D " , & P [I], & lev[ I], & X );
For (J = 1 ; J <= x; j ++)
{
Scanf ( " % D " , & ID, & price );
Map [ID] [I] = price;
}
}
For (I = 1 ; I <= N; I ++)
Map [ 0 ] [I] = P [I];
Lev [0 ] = Lev [ 1 ];
Int Low = lev [ 1 ]-M;
Int Ans = inf;
For (I = low; I <= lev [ 1 ]; I ++)
{
Int TMP = Dijkstra (MAP, N + 1 , I, m );
If (TMP <ans)
Ans = TMP;
}
Printf ( " % D \ n " , ANS );
}
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.