ZOJ-3103 cliff climbing Short Circuit

Source: Internet
Author: User

Meaning: arrive at the T point from several s points. The difference is that you need to distinguish between the left foot and the right foot.

Solution: Starting from the S given in the question, you can set foot on both the left and right feet, and enter the queue all before spfa. After doing this, we found that spfa is used to process multi-source points and no super source points need to be created.

The Code is as follows:

# Include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <queue> using namespace STD; const int INF = 0x3f3f3f; int n, m, dis [2] [65] [35]; char G [65] [35], vis [2] [65] [35]; struct point {int X, Y; point (int x _, int Y _) {x = X _, y = Y _;} Point () {}}; vector <point> S; bool judge (INT lx, int ly, int RX, int ry) {If (LX <1 | RX <1 | lx> N | RX> N | ly <1 | ry <1 | LY> M | Ry> m) return false; If (ly <ry & ABS (Lx-Rx) + ABS (ly-ry) <= 3) return true; return false ;} int ldir [9] [2] = {0,-1}, {0,-2}, {0,-3}, {1,-1 }, {1,-2}, {2,-1}, {-1,-1}, {-1,-2}, {-2,-1 }}; int rdir [9] [2] = {0, 1}, {0, 2}, {0, 3}, {1, 1}, {2, 1}, {-1 }, {-1, 2}, {-2, 1}; void spfa () {point V; memset (VIS, 0, sizeof (VIS); memset (DIS, 0x3f, sizeof (DIS); queue <point> q; For (INT I = 0; I! = S. size (); ++ I) {q. push (s [I]); DIS [0] [s [I]. x] [s [I]. y] = 0; DIS [1] [s [I]. x] [s [I]. y] = 0; vis [0] [s [I]. x] [s [I]. y] = 1; vis [1] [s [I]. x] [s [I]. y] = 1;} while (! Q. empty () {v = Q. front (); q. pop (); // first step left if (vis [1] [v. x] [v. y]) {// if the right foot is in the queue vis [1] [v. x] [v. y] = 0; For (int K = 0; k <9; ++ K) {int I = v. X + ldir [k] [0], j = v. Y + ldir [k] [1]; If (Judge (I, j, V. x, V. y) & G [I] [J]! = 'X') {int Ti = (G [I] [J] = 's' | G [I] [J] = 'T ')? 0: G [I] [J]-'0'; If (DIS [0] [I] [J]> dis [1] [v. x] [v. y] + Ti) {dis [0] [I] [J] = dis [1] [v. x] [v. y] + Ti; If (! Vis [0] [I] [J]) {q. push (point (I, j); vis [0] [I] [J] = 1 ;}}}}} // if you step on the right foot if (vis [0] [v. x] [v. y]) {vis [0] [v. x] [v. y] = 0; For (int K = 0; k <9; ++ K) {int I = v. X + rdir [k] [0], j = v. Y + rdir [k] [1]; If (Judge (v. x, V. y, I, j) & G [I] [J]! = 'X') {int Ti = (G [I] [J] = 's' | G [I] [J] = 'T ')? 0: G [I] [J]-'0'; If (DIS [1] [I] [J]> dis [0] [v. x] [v. y] + Ti) {dis [1] [I] [J] = dis [0] [v. x] [v. y] + Ti; If (! Vis [1] [I] [J]) {q. push (point (I, j); vis [1] [I] [J] = 1 ;}}}} int main () {While (CIN> m> N, N | M) {getchar (); S. clear (); For (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {scanf ("% C % * C", & G [I] [J]); If (G [I] [J] = 's') {S. push_back (point (I, j) ;}} spfa (); int min = inf; For (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {If (G [I] [J] = 'T') {min = min (Min, min (Di S [0] [I] [J], DIS [1] [I] [J]) ;}} if (Min! = Inf) {printf ("% d \ n", min);} else printf ("-1 \ n");} 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.