POJ 2516 Minimum Cost fee Flow

Source: Internet
Author: User

There are M supply points for goods. It provides k types of goods and N stores. These N stores need to order a certain amount of these k types of goods from the goods points, each supply point has different supply for these k types of goods, and each store has different requirements for k types of goods. Each supply point has different costs for delivering a single item of different types of goods to each store, the relationship between supply points, stores, k types of goods, and costs is now given. Now, the order you give for the store allows you to decide how to make the minimum cost for all supply points to complete the order task, if the task can be completed, the minimum cost is output; otherwise,-1 is output.
For my poor English skills... You can only find the translation on the Internet .. It's too watery.
The first time I wrote A fee stream, I wrote some comments based on other people's code. This is the first A in the cost flow. ..
Post Code
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 105
# Define inf 1 <28
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
Using namespace std;
 
Int c [Max] [Max]; // traffic limit
Int f [Max] [Max]; // maximum stream
Int dis [Max];
Int w [Max] [Max]; // fee
Bool visit [Max];
Int path [Max];
Int S, T;
Int q [Max * 100];
Int spfa () // the Shortest Path
{
Int I, j;
For (I = 0; I <= T; I ++)
Dis [I] = inf, path [I] =-1, visit [I] = 0;
Dis [S] = 0;
Visit [S] = 1;
Int num = 0, cnt = 0;
Q [num ++] = S;
While (num> cnt)
{
Int temp = q [cnt ++];
Visit [temp] = 0;
For (I = 1; I <= T; I ++)
If (c [temp] [I]> f [temp] [I] & dis [temp] + w [temp] [I] <dis [I])
{
Path [I] = temp;
Dis [I] = dis [temp] + w [temp] [I];
If (! Visit [I])
{
Visit [I] = 1;
Q [num ++] = I;
}
}
}
If (path [T] =-1)
Return 0;
Return 1;
}
Void getMaxflow () // find the augmented path and adjust the traffic
{
While (spfa ())
{
Int maxFlow = inf;
Int pre = T;
While (path [pre]! =-1)
{
MaxFlow = min (maxFlow, c [path [pre] [pre]-f [path [pre] [pre]);
Pre = path [pre];
}
Pre = T;
While (path [pre]! =-1) // adjust the traffic
{
F [path [pre] [pre] + = maxFlow;
F [pre] [path [pre] =-f [path [pre] [pre];
Pre = path [pre];
}
}
}
Int need [Max] [Max], have [Max] [Max];
Int cost [Max] [Max] [Max];
Int main ()
{
Int I, j, k, l, n, m, d;
 
While (scanf ("% d", & n, & m, & k), (n + m + k ))
{
For (I = 1; I <= n; I ++) // guest I
For (j = 1; j <= k; j ++) // quantity of cargo j required
Scanf ("% d", & need [I] [j]);
For (I = 1; I <= m; I ++) // warehouse I
For (j = 1; j <= k; j ++) // quantity of goods with j
Scanf ("% d", & have [I] [j]);
For (I = 1; I <= k; I ++) // item I
For (j = 1; j <= n; j ++) // deliver to location j.
For (d = 1; d <= m; d ++) // from Location d
Scanf ("% d", & cost [I] [d] [j]); // fee
S = 0, T = n + m + 1; // super source point 0, super sink point n + m + 1;
// Cout <1 <endl;
Bool flag = 0;
Int ans = 0;
For (I = 1; I <= k; I ++)
{
Memset (c, 0, sizeof (c ));
Memset (f, 0, sizeof (f ));
Memset (w, 0, sizeof (w ));
For (j = 1; j <= m; j ++) // The size from the source point to each warehouse is the inventory of such items in the warehouse.
C [0] [j] = have [j] [I];
For (j = 1; j <= n; j ++) // the size of each customer's arrival destination is the customer's demand for items.
C [m + j] [T] = need [j] [I];
For (j = 1; j <= m; j ++)
For (d = 1; d <= n; d ++)
C [j] [d + m] = have [j] [I]; // The size between each warehouse and each guest is the inventory of such items in the warehouse.
For (j = 1; j <= m; j ++)
For (d = 1; d <= n; d ++)
W [j] [d + m] = cost [I] [j] [d], w [d + m] [j] =-w [j] [d + m]; // cost, negative cost for reflux
GetMaxflow ();
// Cout <1 <endl;
For (j = 1; j <= n; j ++)
If (c [j + m] [T]! = F [j + m] [T]) // If supply is unavailable, output-1
{
Flag = 1;
Break;
}
If (flag)
Break;
For (j = 1; j <= m; j ++)
For (d = 1; d <= n; d ++)
Ans + = f [j] [d + m] * w [j] [d + m]; // Total Cost
// Cout <1 <endl;
}
If (flag)
Cout <"-1" <endl;
Else
Cout <ans <endl;
}
Return 0;
}

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.