OUC _ preparations for the provincial competition _ #5 _ F question poj3662

Source: Internet
Author: User
Http://poj.org/problem? Id = 3662 question: Find a path from 1 to n. You can select k paths for free and ask what is the maximum edge length after k entries are exceeded in the path. During the competition, I came up with a correct algorithm: Dual answer + DP determination, but I fell down one place, and the dual answer was reversed several times, but it didn't happen again, when we get back, we can change the binary mode. dp [I] [j] indicates that the maximum value of j paths greater than L is obtained when we reach the I point. It is also possible to directly solve the problem of no solution and 0 through dp, directly expressing the status as dp [I] [j] indicates that the maximum code1 value reaches the I point using j free routes, or two people wrote a part of [code lang = "cpp"] bool spfa (int L) {memset (vv, 0, sizeof (vv); memset (dp,-1, sizeof (dp); while (! Q. empty () q. pop (); bool ffff; ffff = false; node u, tu; u. v = s; u. big = 0; vv [s] [0] = 1; dp [s] [0] = 0; q. push (u); while (! Q. empty () {u = q. front (); q. pop (); if (u. v = n) {if (u. big = k) {if (dp [n] [k]! =-1) {ans = min (ans, dp [n] [k]) ;}} if (u. big & gt; = k) ffff = true; vv [u. v] [u. big] = 0; continue;} for (int I = head [u. v]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; int tmp; if (edge [I]. val & lt; = L) tmp = max (dp [u. v] [u. big], edge [I]. val); else tmp = dp [u. v] [u. big]; if (edge [I]. val & gt; L) {if (tmp & lt; dp [v] [u. big + 1] | dp [v] [u. big + 1] =-1) & amp; (u. big + 1 & lt; = k) {dp [v] [u. big + 1] = tmp; if (! Vv [v] [u. big + 1]) {tu. v = v; tu. big = u. big + 1; q. push (tu); vv [v] [u. big + 1] = 1 ;}}} else {if (tmp & lt; dp [v] [u. big] | dp [v] [u. big] =-1) {dp [v] [u. big] = tmp; if (! Vv [v] [u. big]) {tu. v = v; tu. big = u. big; q. push (tu); vv [v] [u. big] = 1 ;}}} vv [u. v] [u. big] = 0;} return ffff;} bool vis [maxn]; int que [maxn]; int dis [maxn]; bool check () {memset (vis, false, sizeof (vis); memset (dis,-1, sizeof (dis); int hh = 0, tail = 1; que [1] = 1; vis [1] = 1; dis [1] = 0; while (hh & lt; tail) {int u = que [++ hh]; for (int j = head [u]; j! =-1; j = edge [j]. next) {int v = edge [j]. v; if (dis [u] + 1 & lt; dis [v] | dis [v] =-1) {dis [v] = dis [u] + 1; if (! Vis [v]) {que [++ tail] = v; vis [v] = 1 ;}} vis [u] = 0 ;}if (dis [n]! =-1 & amp; dis [n] & lt; = k) return true; return false;} int main () {int u, v, val; while (scanf (& quot; % d & quot;, & amp; n, & amp; p, & amp; k )! = EOF) {cnt = 0; s = 1; memset (head,-1, sizeof (head); int mmjh = 0; for (int I = 0; I & lt; p; I ++) {scanf (& quot; % d & quot;, & amp; u, & amp; v, & amp; val); addedge (u, v, val); mm1_= max (mm1_, val);} if (check () {printf (& quot; 0 \ n & quot;); continue;} int left, right, mid; left = 1; right = mmjh; ans = inf; while (left & lt; right) {mid = (left + right)/2; bool flag; flag = spfa (mid); if (flag = 0 ){ Left = mid + 1;} else {right = mid;} if (ans = inf) {ans =-1;} printf (& quot; % d \ n & quot;, ans);} return 0;} [/code] code2 [code lang = "cpp"] void spfa () {memset (vv, 0, sizeof (vv); for (int I = 0; I & lt; = n; I ++) {for (int j = 0; j & lt; = k; j ++) {dp [I] [j] = inf ;}} while (! Q. empty () q. pop (); node u, tu; u. v = s; u. big = 0; vv [s] [0] = 1; dp [s] [0] = 0; q. push (u); while (! Q. empty () {u = q. front (); q. pop (); if (u. v = n) {// printf (& quot; % d \ n & quot;, u. v, u. big, dp [u. v] [u. big]); ans = min (ans, dp [u. v] [u. big]);} for (int I = head [u. v]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; int tmp; tmp = max (dp [u. v] [u. big], edge [I]. val); if (dp [v] [u. big] & gt; tmp) {dp [v] [u. big] = tmp; if (! Vv [v] [u. big]) {tu. v = v; tu. big = u. big; q. push (tu); vv [v] [u. big] = 1 ;}} if (u. big & lt; k & amp; dp [u. v] [u. big] & lt; dp [v] [u. big + 1]) {dp [v] [u. big + 1] = dp [u. v] [u. big]; if (! Vv [v] [u. big + 1]) {tu. v = v; tu. big = u. big + 1; q. push (tu); vv [v] [u. big + 1] = 1 ;}}// printf (& quot; \ n & quot;); vv [u. v] [u. big] = 0 ;}} int main () {int u, v, val; while (scanf (& quot; % d & quot;, & amp; n, & amp; p, & amp; k )! = EOF) {cnt = 0; s = 1; memset (head,-1, sizeof (head); for (int I = 0; I & lt; p; I ++) {scanf (& quot; % d & quot;, & amp; u, & amp; v, & amp; val); addedge (u, v, val) ;}ans = inf; spfa (); if (ans = inf) {ans =-1;} printf (& quot; % d \ n & quot;, ans);} return 0;} [/code]

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.