HDU 5383 yu-gi-oh! (game King!) + Fee Flow)

Source: Internet
Author: User

HDU 5383 yu-gi-oh!

Problem Description
"Yu-gi-oh!", also known as "Dueling Monsters", is a popular trading card game which have nearly years history. Next year, YGO would reach its 20th birthday.

Stilwell have n monsters on the desk, each monster have its Leveli and Atki. There is kinds of monsters, Tuner monsters and Non-tuner monsters.

Now, the Stilwell plans to the finish some "Synchro summon", and "Synchro summon" is a kind of special summon following these rule S (a little different from the standard YGO rules):

(1) A "Synchro summon" needs, monsters as the material of this summon, and they must be one Tuner monster and one non-t Uner Monster.
In other words, we can cost one Tuner monster and one Non-tuner monster to get a Synchro monster ("Cost" means remove form The desk, "get" means put on to the desk).

(2) To simplify this problem, Synchro monsters is neither Tuner monsters nor non-tuner monsters.

(3) The level sum of material must is equal to the level of Synchro monster we summon.
For example:
A Level 3 Tuner monster + A level 2 non-tuner monster = A level 5 Synchro monster
A Level 2 Tuner monster + A level 4 non-tuner monster = A level 6 Synchro monster
A Level 4 Tuner monster + A level 4 non-tuner monster = A level 8 Synchro monster

(4) The material of some Synchro monster has some limits, the material must contain some specific monster.
For example:
A Level 5 Synchro monsterαrequires A Level 3 Tuner monsterαto is its material
A Level 6 Synchro monsterβrequires A level 4 Non-tuner monsterβto is its material
A Level 8 Synchro monsterγrequires A Level 4 Tuner monsterγ+ A level 4 Non-tuner monsterγto is its material
A Level 5 Synchro monsterφdoesn ' t require any monsters to is its material
Then
A Level 3 Tuner monsterα+ A level 2 non-tuner monster = A level 5 Synchro Monsterα
A Level 3 Tuner monsterδ+ A Level 2 Non-tuner monster≠a level 5 Synchro Monsterα
A Level 2 Tuner monster + A level 4 Non-tuner monsterβ= A level 6 Synchro monsterβ
A Level 3 Tuner Monster + A level 3 Non-tuner monsterζ≠a level 6 Synchro monsterβ
A Level 4 Tuner monsterγ+ A level 4 Non-tuner monsterγ= A level 8 Synchro monsterγ
A Level 4 Tuner monsterσ+ A level 4 Non-tuner monsterγ≠a level 8 Synchro Monsterγ
A Level 4 Tuner monsterγ+ A level 4 Non-tuner monster? ≠a Level 8 Synchro Monsterγ
A Level 3 Tuner monster + A level 2 non-tuner monster = A level 5 Synchro monsterφ
A Level 3 Tuner monsterα+ A level 2 non-tuner monster = A level 5 Synchro monsterφ

Stilwell have m kinds of Synchro Monster cards, the quantity of each Synchro Monster cards are infinity.

Now, given Leveli and atki of every card on desk and every kind of Synchro Monster cards. Please finish some Synchro summons (maybe zero) to Maximum∑atki of the cards on desk.

Input
The first line of the input contains a single number T, the number of test cases.

For each test case, the first line contains the integers n, M.

Next n lines, each line contains three integers tuneri, Leveli, and Atki, describe a monster on the desk. If This monster are a Tuner monster, then Tuneri=1, else tuneri=0 for Non-tuner monster.

Next m lines, each line contains integers Levelj, atkj, RJ, and following RJ integers is the required material of this Sy Nchro Monster (the integers given is the identifier of the required material).
The input data guarantees, the required material list is available, the other Tuner monsters or both Non-tuner monsters won ' t be required. If ri=2 The level sum of the required material would be equal to the level of Synchro Monster.

T≤10, n,m≤300, 1≤leveli≤12, 0≤atki≤5000, 0≤ri≤2

Output
T lines, find the Maximum∑atki after some Synchro summons.

Sample Input

5
2 2
1 3 1300
0 2 900
5 2300 1 1
8 2500 0
2 1
1 3 1300
1 2 900
5 2300 1 1
3 1
1 3 1300
0 2 900
0 2 800
5 2300 1 1
3 1
1 1 233
0 1 233
0 1 200
2 466 2) 1 2
6 3
1 3 1300
0 2 900
0 5 1350
1 4 1800
0 10 4000
0 10 1237
5 2300 1 1
8 3000 0
6 2800 0

Sample Output

2300
2200
3200
666
11037

The main topic: There are N monsters on the field, there is a way of M homology call. When the two monsters are of different types and the levels of the two monsters are added to the new strange level, the two monsters can summon new monsters as offerings. It is now required to find out what the maximum field attack is, after a coherent call (which can not be called by homology). Idea: Set a super source point to all monsters of type 0, with a capacity of 1 and a cost of 0. Set a super sink point so that all monsters of type 1 are connected to it, with a capacity of 1 and a cost of 0. The two monsters that can be called in tune are then connected, with a capacity of 1, at the expense of the original attack of the two monsters and minus the attack of the new monster after the call of homology. At the end of the day, the minimum cost to get the first field attack minus is the answer.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intPO =305;Const intINF =0x3f3f3f3f;Const intN =1010;Const intM =100000;typedef Long LongllintN, M, Cnt1, Cnt2, S, t, sum;intPre[n], inq[n];intA[n], D[n], gra[n][n];structdesk{//de[i][j] Record type I of the monster in the first J information    intID, type, level, ATK;} de[2][PO];structrec{//rec[i] The category of the I beast on the record field, and the position in the array of that category    intf, id;} REC[PO];structedge{intFrom, to;intCAP, Flow;int Cos;}; vector<Edge>Edges vector<int>G[M];voidInit () {memset(GRA,0,sizeof(GRA)); Cnt1 = Cnt2 =1; sum =0; for(inti =0; I <= n * n *2;    i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo,intCapint Cos{Edges.push_back (Edge) {from, to, Cap,0,Cos}); Edges.push_back (Edge) {To, from,0,0, -Cos});intm = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1);}intBF (intSintTint& Flow,int& cost) { Queue<int>Q;memset(INQ,0,sizeof(INQ));memsetA0,sizeof(a));memset(Pre,0,sizeof(pre)); for(inti =0; I <= n +2;    i++) D[i] = INF; D[s] =0;    A[s] = INF; Inq[s] =1; Pre[s] =0; Q.push (s); while(! Q.empty ()) {intU = Q.front ();        Q.pop (); Inq[u] =0; for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(E.cap > E.flow && d[e.to] > D[u] + E.Cos) {d[e.to] = D[u] + E.Cos;                A[e.to] = min (A[u], e.cap-e.flow); Pre[e.to] = G[u][i];if(!inq[e.to]) {Inq[e.to] =1;                Q.push (e.to); }            }           }    }if(D[t] >=0)return 0;    Flow + = A[t]; Cost + = d[t] * A[t]; for(intu = t; U! = S;        U = edges[pre[u]].from) {Edges[pre[u]].flow + = a[t]; edges[pre[u]^1].flow-= a[t]; }return 1;}intMCMF (intSintTint& cost) {intFlow =0; Cost =0; while(BF (S, t, flow, cost));returnCost;}voidInput () {scanf("%d%d", &n, &m); s =0, T = n +2; for(inti =1; I <= N; i++) {intType, level, ATK;scanf(" %d%d%d", &type, &level, &AMP;ATK);if(!type) {//There are different arrays by monster typeREC[I].F =0, rec[i].id = Cnt1; de[0][cnt1++] = (Desk) {I, type, level, ATK}; Addedge (S, I,1,0);        sum + = ATK; }Else{REC[I].F =1, rec[i].id = Cnt2; de[1][cnt2++] = (Desk) {I, type, level, ATK}; Addedge (i, T,1,0);        sum + = ATK; }    }intLevel1, Level2; for(inti =1; I <= m; i++) {//When two different types of strange levels are added equal to the new strange level, then these two different types of strange can synthesize new strange, the new strange attack is larger than the original two strange attack, this synthesis is effective        intLevel, ATK, R;scanf(" %d%d%d", &level, &AMP;ATK, &r);if(r = =2) {intA, B;scanf("%d%d", &a, &b);            Level1 = De[rec[a].f][rec[a].id].level; Level2 = De[rec[b].f][rec[b].id].level;if(REC[A].F) Swap (A, b);if(Level1 + Level2 = = level)            {Gra[rec[a].id][rec[b].id] = max (gra[rec[a].id][rec[b].id], ATK); }        }Else if(r = =1) {intAscanf("%d", &a);if(!REC[A].F) { for(intj =1; J < Cnt2; J + +) {Level1 = de[0][rec[a].id].level; Level2 = de[1][j].level;if(Level1 + Level2 = = level)                    {Gra[rec[a].id][j] = max (gra[rec[a].id][j], ATK); }                }                }Else{ for(intj =1; J < Cnt1; J + +) {Level1 = de[1][rec[a].id].level; Level2 = de[0][j].level;if(Level1 + Level2 = = level)                    {Gra[j][rec[a].id] = max (gra[j][rec[a].id], ATK); }                }                }        }Else{ for(intj =1; J < Cnt1; J + +) { for(intK =1; K < Cnt2; k++) {Level1 = de[0][j].level; Level2 = de[1][k].level;if(Level1 + Level2 = = level)                    {Gra[j][k] = max (gra[j][k], ATK); }                }            }        }    } for(inti =1; i < cnt1; i++) { for(intj =1; J < Cnt2; J + +) {intATK1 = de[0][I].ATK;intATK2 = de[1][J].ATK;if(Gra[i][j] >0&& Gra[i][j] > atk1 + atk2) {Addedge (de[0][i].id, de[1][j].id,1, Atk1 + atk2-gra[i][j]); }            }        }}intMain () {intTscanf("%d", &t); while(t--)        {init (); Input ();intCostprintf("%d\n", SUM-MCMF (S, T, cost)); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint.

HDU 5383 yu-gi-oh! (game King!) + Fee Flow)

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.