1062 expensive dowry (spfa + level enumeration)-from lanshui_Yang

Source: Internet
Author: User

Solution: first build the graph, note that it is a directed graph, and then use 1 as the source point, the enumeration level limit, that is to say, each time we use spfa to get 1 point to other points that can be reached (due to the level limit, not all points may be reached in one spfa), and finally find the minimum cost. Ps: the level of point 1 in this question may not be the highest. See the Code:

# Include <iostream> # include <string> # include <algorithm> # include <cstring> # include <queue> # include <cmath> # include <vector> # include <cstdio> # define mem (, b) memset (a, B, sizeof (a) using namespace std; inline void RD (int & a) {a = 0; char t; do {t = getchar () ;}while (t <'0' | t> '9'); a = t-'0 '; while (t = getchar ()> = '0' & t <= '9') {a = a * 10 + t-'0 ';}} inline void OT (Int a) {if (a >=10) {OT (a/10);} putchar (a % 10 + '0');} const int MAXN = 105; const int INF = 0x7fffffff; int m, n; struct Node {int adj; int c ;}; vector <Node> vert [MAXN]; int P [MAXN]; // The price of each item int L [MAXN]; // The level of each item holder int d [MAXN]; // record the Shortest Path void clr () from 1 to other points each time. // initialize {mem (P, 0); mem (L,-1); int I; for (I = 1; I <= n; I ++) vert [I]. clear ();} void init () {clr (); Int I, j; for (I = 1; I <= n; I ++) {scanf ("% d", & P [I], & L [I]); int t; scanf ("% d", & t); for (j = 1; j <= t; j ++) // create a graph {int B; scanf ("% d", & B); Node tmp; tmp. adj = B; scanf ("% d", & tmp. c); vert [I]. push_back (tmp) ;}} bool inq [MAXN]; queue <int> q; void spfa (int M) {while (! Q. empty () q. pop (); q. push (1); d [1] = 0; inq [1] = true; int u; while (! Q. empty () {u = q. front (); q. pop (); inq [u] = false; int I; for (I = 0; I <vert [u]. size (); I ++) {int v = vert [u] [I]. adj; int ci = vert [u] [I]. c; if (L [v]> = M & L [v] <= M + m) {if (d [u]! = INF & d [u] + ci <d [v]) {d [v] = d [u] + ci; if (! Inq [v]) {q. push (v); inq [v] = true ;}}}} void solve () {int X = max (L [1]-m, 0 ); int MIN = P [1]; int M; for (M = X; M <= L [1]; M ++) // enumeration level {int I; for (I = 1; I <= n; I ++) {d [I] = INF;} mem (inq, 0); spfa (M ); for (I = 2; I <= n; I ++) {if (L [I]> = M & L [I] <= M + m & d [I]! = INF & MIN> d [I] + P [I]) // note the judgment condition MIN = d [I] + P [I] ;}} printf ("% d \ n", MIN);} int main () {while (scanf ("% d", & m, & n )! = EOF) {init (); solve ();} 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.