Poj optimal milking

Source: Internet
Author: User

Optimal milking

 

Question:

There are K machines and C is a cow. It is required to find the shortest distance from the nearest cow to each milk generator. A matrix of C + k is given, indicating the distance between various labels.

Each place has a maximum of M cattle.

 

Algorithm analysis:

Binary + Shortest Path + network stream

Difficult to think about. I am reading the solution report. Then, I reached my hand. Wrong started three times. After that, an unintentional change would be AC. Silent ....

When wrong is in progress, the Network Flow Graph can only be the distance between the machine and the cow. The cows and cows or machines do not need edge construction. At that time, my brains were exhausted !!!!

# Include <iostream> # include <algorithm> # include <vector> # include <queue> # include <cstdio> # include <cstring> using namespace STD; const int INF = 1 <20; const int maxn = 1000; struct edge {int from, to, Cap, flow, cost; edge (){}; edge (INT _ from, int _ to, int _ cap, int _ flow): From (_ from), to (_ to), Cap (_ cap ), flow (_ flow) {};}; vector <edge> edges; vector <int> G [maxn]; int cur [maxn], d [maxn]; bool VST [maxn]; int SRC, sink; Int Dist [maxn] [maxn]; int K, C, M, V; void Init () {src = V + 1; sink = SRC + 1; for (INT I = 0; I <= sink; ++ I) g [I]. clear (); edges. clear () ;}void flody () {for (int K = 0; k <v; ++ K) for (INT I = 0; I <v; ++ I) for (Int J = 0; j <v; ++ J) if (Dist [I] [J]> Dist [I] [k] + dist [k] [J]) dist [I] [J] = DIST [I] [k] + dist [k] [J]; // For (INT I = 0; I <V; ++ I) {// For (Int J = 0; j <v; ++ J) // printf ("% d", DIST [I] [J]); // puts ("");//}} Void addedge (int from, int to, int cap) {edges. push_back (edge (from, to, Cap, 0); edges. push_back (edge (to, from, 0, 0); int SZ = edges. size (); G [from]. push_back (SZ-2); G [to]. push_back (SZ-1);} void build (INT limit) {Init (); For (INT I = K; I <v; ++ I) {// cow and Source Vertex addedge (SRC, I, 1) ;}for (INT I = 0; I <K; ++ I) {// machine and sink addedge (I, sink, m);} // note ---> I = K !!! J <K !!!! For (INT I = K; I <v; ++ I) {// connection between the cow and the machine for (Int J = 0; j <K; ++ J) {If (Dist [I] [J] <= Limit) {addedge (I, j, 1) ;}}} bool BFS () {memset (VST, 0, sizeof (VST); queue <int> q; q. push (SRC); D [SRC] = 0; VST [SRC] = 1; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <(INT) g [X]. size (); ++ I) {edge & E = edges [G [x] [I]; If (! VST [E. to] & E. cap> E. flow) {VST [E. to] = 1; d [E. to] = d [x] + 1; q. push (E. to) ;}}return VST [sink];} int DFS (int x, int A) {If (x = sink | A = 0) return; int flow = 0, F; For (Int & I = cur [X]; I <(INT) g [X]. size (); ++ I) {edge & E = edges [G [x] [I]; If (d [E. to] = d [x] + 1 & (F = DFS (E. to, min (A, E. cap-e. flow)> 0) {e. flow + = f; edges [G [x] [I] ^ 1]. flow-= f; flow + = f; A-= f; if (a = 0) break;} retur N flow;} int maxflow () {int flow = 0; while (BFS () {memset (cur, 0, sizeof (cur); flow + = DFS (SRC, INF);} return flow;} bool check (INT mid) {build (MID); int flow = maxflow (); // cout <"Flow: "<flow <Endl; return flow = C;} void solve () {flody (); int lB =-1, UB = inf + 100; while (UB-lb> 1) {int mid = (Lb + UB)/2; If (check (MID) UB = mid; else lB = mid; // cout <"mid:" <mid <"LB:" <LB <"UB:" <UB <Endl;} printf ("% d \ n", UB);} int main () {// freopen ("input.txt", "r", stdin); While (~ Scanf ("% d", & K, & C, & M) {v = K + C; int X; For (INT I = 0; I <v; ++ I) {for (Int J = 0; j <v; ++ J) {scanf ("% d", & X ); dist [I] [J] = (x = 0? INF: X);} Dist [I] [I] = 0;} solve ();} return 0 ;}


 

Another method is multi-match, which has not been written yet. Complete it.

Poj optimal milking

Related Article

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.