Topcoder SRM 358: SharksDinner

Source: Internet
Author: User
Some sharks want dinner, and they want to eat each other. Each pair has a size, speed, and intelligence. Shark A can eat shark B and enter the size, speed, and intelligence of A to be greater than or equal to shark B. In addition, each shark can eat a maximum of two other sharks.
Given int [] Size, Int [] SpeedAnd int [] IntelligenceThe minimum number of sharks that survive.

Analysis: the minimum number of sharks that survive is required, and the maximum number of sharks to be eaten is required.
Suppose there are n sharks. We create a bipartite graph. Each shark has 3 Ax1, Ax2, and Bx vertices. Ax1 and Ax2 are in the left graph of the Bipartite Graph, in the right corner of the Bipartite Graph, if shark x can eat shark y, we create an edge Ax1-> By, Ax2->, the problem is converted to finding the maximum matching match of the Bipartite Graph. The final result is n-match.
The maximum matching of a bipartite graph uses the Hungary algorithm (search for augmented path)

Code: # Include <iostream>
# Include <vector>
Using namespace std;

Struct st {
Int a [3];
St (){
For (int I = 0; I <3; I ++) a [I] = 0;
}
St (int A, int B, int C ){
A [0] =;
A [1] = B;
A [2] = C;
}
};

Const int MAXN = 250;

Bool eq (const st & x, const st & y ){
For (int I = 0; I <3; I ++)
If (x. a [I]! = Y. a [I])
Return false;
}

// Judge whether shark x can eat shark y
Bool can_eat (const st & x, const & y ){
If (eq (x, y) return false;
For (int I = 0; I <3; I ++)
If (x. a [I] <y. a [I]) return false;
Return true;
}

St a [MAXN]; // saves shark Information
Bool gr [MAXN] [MAXN]; // Bipartite Graph
Int n, m; // number of nodes in two subgraphs
Int match [MAXN]; // records the matching information of a bipartite graph.
Bool us [MAXN]; // records whether the node in the second subgraph of the bipartite graph has been used

// Search for the augmented path from the v node in the first subgraph of the Bipartite Graph
Bool go (int v ){
For (int I = 0; I <m; I ++ ){
If (us [I]) continue;
If (gr [v] [I]) {
If (match [I] =-1 ){
Us [I] = 1;
Match [I] = v;
Return true;
}
}
}
For (int I = 0; I <m; I ++ ){
If (us [I]) continue;
If (gr [v] [I]) {
Us [I] = 1;
If (go (match [I]) {
Match [I] = v;
Return true;
}
}
}
Return false;
}

// Perform the maximum binary match
Int matcher (){
Int res = 0;
Memset (match, 255, sizeof match );

// Perform the n-path addition operation
For (int I = 0; I <n; I ++ ){
Memset (us, 0, sizeof us );
If (go (I) ++ res;
}
Return res;
}

Class SharksDinner {
Public:
Static int min1_vors (vector <int> A, vector <int> B, vector <int> C ){
N = A. size ();
Memset (gr, 0, sizeof (gr ));
For (int I = 0; I <n; I ++)
A [I] = st (A [I], B [I], C [I]);

// Construct a Bipartite Graph
For (int I = 0; I <n; I ++ ){
For (int j = 0; j <n; j ++ ){
If (can_eat (a [I], a [j]) {
Gr [2 * I] [j] = 1;
Gr [2 * I + 1] [j] = 1;
}
}
For (int j = 0; j <I; j ++ ){
If (eq (a [I], a [j]) {
Gr [2 * I] [j] = 1;
Gr [2 * I + 1] [j] = 1;
}
}
}
N = 2 * A. size ();
M = A. size ();
Return m-matcher ();
}
};

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.