2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

时间:2020-12-09 12:38:28

Yu-Gi-Oh!

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 5383
64-bit integer IO format: %I64d      Java class name: Main

 
"Yu-Gi-Oh!", also known as "Dueling Monsters", is a popular trading card game which has nearly 20 years history. Next year, YGO will reach its 20th birthday.

Stilwell has n monsters on the desk, each monster has its leveli and ATKi. There are two kinds of monsters, Tuner monsters and Non-Tuner monsters.

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

(1) A "Synchro Summon" needs two monsters as the material of this summon, and they must be one Tuner monster and one Non-Tuner 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 are neither Tuner monsters nor Non-Tuner monsters.

(3) The level sum of two material must be 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 be its material
A Level 6 Synchro Monster β requires A Level 4 Non-Tuner monster β to be its material
A Level 8 Synchro Monster γ requires A Level 4 Tuner monster γ + A Level 4 Non-Tuner monster γ to be its material
A Level 5 Synchro Monster φ doesn't require any monsters to be 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 has m kinds of Synchro Monster cards, the quantity of each Synchro Monster cards is 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 two integers n, m.

Next n lines, each line contains three integers tuneri, leveli, and ATKi, describe a monster on the desk. If this monster is a Tuner monster, then tuneri=1, else tuneri=0for Non-Tuner monster.

Next m lines, each line contains integers levelj, ATKj, rj, and following rj integers are the required material of this Synchro Monster (the integers given are the identifier of the required material).
The input data guarantees that the required material list is available, two Tuner monsters or two Non-Tuner monsters won't be required. If ri=2 the level sum of two required material will 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

Source

 
解题:费用流。。。Orz
 
哎 ,还是写类比较好,可以把相同变量隔离开来
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
class FUCK {
public:
struct arc {
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -) {
to = x;
flow = y;
cost = z;
next = nxt;
}
} e[maxn*maxn];
int head[maxn],d[maxn],p[maxn],tot,S,T;
bool in[maxn];
void init() {
memset(head,-,sizeof head);
tot = ;
}
void add(int u,int v,int flow,int cost) {
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool spfa() {
queue<int>q;
q.push(S);
memset(d,0x3f,sizeof d);
memset(in,false,sizeof in);
memset(p,-,sizeof p);
d[S] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]) {
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
if(d[T] >= ) return false;
return p[T] > -;
}
int solve(int ret = ) {
while(spfa()) {
int minF = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
minF = min(minF,e[i].flow);
for(int i = p[T]; ~i; i = p[e[i^].to]) {
e[i].flow -= minF;
e[i^].flow += minF;
}
ret += minF*d[T];
}
return ret;
}
};
class YGO {
public:
int tunner[maxn],atk[maxn],lev[maxn],w[maxn][maxn],ret;
int n,m;
FUCK cao;
void update(int a,int b,int val) {
if(tunner[a] < tunner[b]) w[a][b] = max(w[a][b],val);
if(tunner[b] < tunner[a]) w[b][a] = max(w[b][a],val);
}
void init() {
memset(w,,sizeof w);
scanf("%d%d",&n,&m);
cao.init();
ret = cao.S = ;
cao.T = n + ;
for(int i = ; i <= n; ++i) {
scanf("%d%d%d",tunner+i,lev+i,atk+i);
ret += atk[i];
if(tunner[i]) cao.add(i,cao.T,,);
else cao.add(cao.S,i,,);
}
for(int i = ; i <= m; ++i) {
int lv,ak,nm,a,b;
scanf("%d%d%d",&lv,&ak,&nm);
if(nm == ) {
for(int j = ; j <= n; ++j) {
for(int k = j+; k <= n; ++k)
if(lev[j] + lev[k] == lv)
update(j,k,ak - atk[j] - atk[k]);
}
}
if(nm == ) {
scanf("%d",&a);
for(int j = ; j <= n; ++j) {
if(lev[a] + lev[j] == lv)
update(a,j,ak - atk[a] - atk[j]);
}
}
if(nm == ) {
scanf("%d%d",&a,&b);
update(a,b,ak - atk[a] - atk[b]);
}
}
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
if(w[i][j]) cao.add(i,j,,-w[i][j]);
printf("%d\n",ret - cao.solve());
} } BB;
int main() {
int kase;
scanf("%d",&kase);
while(kase--) BB.init();
return ;
}