Poj dual core CPU

Source: Internet
Author: User

Dual core CPU

Question:

A dual-core CPU consisting of core A and core B is provided to run n modules. The execution cost of Module I on core A is Ai, And the execution cost on core B is bi. There are m modules (AI and Bi) that need to exchange data between each other. If the two modules are executed on the same core, there is no additional cost, otherwise, the cost of Wi will be generated. Calculate the minimum cost required to execute all modules.

Algorithm analysis:

Objects are divided into two sets at the minimum cost, which can be easily solved after the minimum cut.

This is a classic example of this type of problem. Consider dividing n modules into two sets based on which core they run.

Then, it is the problem of creating a graph. Because we divide the two cores into two sets, we can connect the modules running on the same core to the source or sink respectively. For example, when AI and T are connected, Bi and S are connected, and the phone charge required for converting the core is to connect the two modules to a Wi-Fi edge respectively. Finally, the minimum cut is the answer.

# include # include # include # include # include # include using namespace STD; const int maxn = 20000 + 10; const int INF = 1 <30; struct edge {int from, to, Cap, flow; edge (){}; edge (INT _ from, int _ to, int _ cap, int _ flow): From (_ from), to (_ to), Cap (_ cap ), flow (_ flow) {};}; vector edges; vector G [maxn]; int cur [maxn], d [maxn]; bool VST [maxn]; int n, m, S, T; void Init () {S = n + 1; t = n + 2; for (INT I = 0; I <= T; ++ I) G [I]. clear (); edges. clear ();} 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);} bool BFS () {queue q; memset (VST, 0, sizeof (VST); q. push (s); VST [s] = 1; d [s] = 0; while (! Q. empty () {int u = Q. front (); q. pop (); For (INT I = 0; I <(INT) g [u]. size (); ++ I) {edge & E = edges [G [u] [I]; If (! VST [E. to] & E. cap> E. flow) {d [E. to] = d [u] + 1; VST [E. to] = 1; q. push (E. to) ;}}return VST [T];} int DFS (int x, int A) {If (x = T | 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;} return F Low;} int maxflow () {int flow = 0; while (BFS () {memset (cur, 0, sizeof (cur); flow + = DFS (S, INF);} return flow;} int main () {// freopen ("input.txt", "r", stdin); While (~ Scanf ("% d", & N, & M) {Init (); int A, B, W; For (INT I = 1; I <= N; ++ I) {scanf ("% d", & A, & B); addedge (I, T, a); addedge (S, I, B );} for (INT I = 0; I


Poj dual core CPU

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.