Poj 2195 going home

Source: Internet
Author: User
1/* 2 it is really important to create a question map for the network flow! 3. In this question, the person's location is connected to the House's location. The traffic from man to house is 1, the cost is the distance between the two and the traffic on the direction side is 0. The cost is the opposite number of the forward side (that is, when the backward side is scaled up, the cost is reduced, change the previous incorrect option) 5. Add a Source Vertex and a sink vertex (Source Vertex maps to man, house maps to sink vertex, cost is 0, traffic is 1) 6 */7 # include <iostream> 8 # include <cmath> 9 # include <cstdio> 10 # include <cstring> 11 # include <queue> 12 # define Max 0x3f3f3f3f 13 # define n 205 14 using namespace STD; 15 16 class node 17 {18 public: 19 int X, Y; 20}; 21 22 Node Xym [N]; 23 node xyh [N]; 24 int cost [N] [N], Cap [N] [N]; 25 int cntm, cnth; 26 int pre [N * 2], DIST [N * 2], vis [N * 2], n, m; 27 28 void Adde (int I, Int J, int CT, int CP) 29 {30 cost [I] [J] = CT; 31 CAP [I] [J] = CP; 32 cost [J] [I] =-CT; 33 // CAP [J] [I] = 0; 34} 35 36 int S, T, mincost; 37 38 void buildmap () 39 {40 int I, j; 41 memset (Cap, 0, sizeof (CAP); 42 s = 0; t = cntm + cnth + 1; 43 for (I = 0; I <cntm; ++ I) 44 Adde (0, I + 1, 0, 1); 45 for (I = 0; I <cnth; ++ I) 46 Adde (cntm + I + 1, t, 0, 1 ); 47 for (I = 0; I <cntm; ++ I) 48 for (j = 0; j <cnth; ++ J) 49 Adde (I + 1, cntm + J + 1, ABS (xym [I]. x-xyh [J]. x) + ABS (xym [I]. y-xyh [J]. y), 1); 50} 51 52 queue <int> q; 53 54 int spfa () 55 {56 int U, V; 57 memset (Dist, 0x3f, sizeof (DIST); 58 Dist [0] = 0; 59 Q. push (0); 60 vis [0] = 1; 61 while (! Q. empty () 62 {63 U = Q. front (); 64 Q. pop (); 65 vis [u] = 0; 66 for (V = 0; v <= T; ++ V) 67 If (Cap [u] [v]> 0 & Dist [v]> Dist [u] + cost [u] [v]) 68 {69 Dist [v] = DIST [u] + cost [u] [v]; 70 pre [v] = u; 71 If (! Vis [v]) 72 {73 vis [v] = 1; 74 Q. push (V); 75} 76} 77} 78 If (Dist [T] = max) 79 return 0; 80 return 1; 81} 82 83 void updateedge () 84 {85 int U, minflow = max; 86 for (u = T; u! = S; u = pre [u]) // find the minimum traffic on this path through the Shortest Path 87 If (Cap [pre [u] [u] <minflow) 88 minflow = CAP [pre [u] [u]; 89 for (u = T; u! = S; u = pre [u]) 90 {91 CAP [pre [u] [u]-= minflow; 92 CAP [u] [pre [u] + = minflow; 93 mincost + = cost [pre [u] [u]; 94} 95} 96 97 int main () 98 {99 int I, j; 100 char C; 101 while (scanf ("% d", & N, & M) & (N | M )) 102 {103 cntm = cnth = 0; 104 mincost = 0; 105 for (I = 1; I <= N; ++ I) 106 {107 getchar (); 108 For (j = 1; j <= m; ++ J) 109 {110 scanf ("% C", & C); 111 If (C = 'M ') 112 {113 xym [cntm]. X = I; 114 xym [cntm ++]. y = J; 115} 116 else if (C = 'H') 117 {118 xyh [cnth]. X = I; 119 xyh [cnth ++]. y = J; 120} 121} 122} 123 buildmap (); 124 while (spfa () 125 updateedge (); 126 printf ("% d \ n", mincost ); 127} 128 return 0; 129}

 

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.