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

本文介绍了一个基于经典游戏Yu-Gi-Oh!的问题,该问题涉及特殊的召唤规则,并通过使用费用流算法求解如何最大化桌面上卡片的攻击力总和。文章提供了详细的代码实现。

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 nm.

Next n lines, each line contains three integers tunerileveli, 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 leveljATKjrj, 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.

T10n,m3001leveli120ATKi50000ri2
 

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
 
哎 ,还是写类比较好,可以把相同变量隔离开来
 
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 510;
  4 const int INF = 0x3f3f3f3f;
  5 class FUCK {
  6 public:
  7     struct arc {
  8         int to,flow,cost,next;
  9         arc(int x = 0,int y = 0,int z = 0,int nxt = -1) {
 10             to = x;
 11             flow = y;
 12             cost = z;
 13             next = nxt;
 14         }
 15     } e[maxn*maxn];
 16     int head[maxn],d[maxn],p[maxn],tot,S,T;
 17     bool in[maxn];
 18     void init() {
 19         memset(head,-1,sizeof head);
 20         tot = 0;
 21     }
 22     void add(int u,int v,int flow,int cost) {
 23         e[tot] = arc(v,flow,cost,head[u]);
 24         head[u] = tot++;
 25         e[tot] = arc(u,0,-cost,head[v]);
 26         head[v] = tot++;
 27     }
 28     bool spfa() {
 29         queue<int>q;
 30         q.push(S);
 31         memset(d,0x3f,sizeof d);
 32         memset(in,false,sizeof in);
 33         memset(p,-1,sizeof p);
 34         d[S] = 0;
 35         while(!q.empty()) {
 36             int u = q.front();
 37             q.pop();
 38             in[u] = false;
 39             for(int i = head[u]; ~i; i = e[i].next) {
 40                 if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
 41                     d[e[i].to] = d[u] + e[i].cost;
 42                     p[e[i].to] = i;
 43                     if(!in[e[i].to]) {
 44                         in[e[i].to] = true;
 45                         q.push(e[i].to);
 46                     }
 47                 }
 48             }
 49         }
 50         if(d[T] >= 0) return false;
 51         return p[T] > -1;
 52     }
 53     int solve(int ret = 0) {
 54         while(spfa()) {
 55             int minF = INF;
 56             for(int i = p[T]; ~i; i = p[e[i^1].to])
 57                 minF = min(minF,e[i].flow);
 58             for(int i = p[T]; ~i; i = p[e[i^1].to]) {
 59                 e[i].flow -= minF;
 60                 e[i^1].flow += minF;
 61             }
 62             ret += minF*d[T];
 63         }
 64         return ret;
 65     }
 66 };
 67 class YGO {
 68 public:
 69     int tunner[maxn],atk[maxn],lev[maxn],w[maxn][maxn],ret;
 70     int n,m;
 71     FUCK cao;
 72     void update(int a,int b,int val) {
 73         if(tunner[a] < tunner[b]) w[a][b] = max(w[a][b],val);
 74         if(tunner[b] < tunner[a]) w[b][a] = max(w[b][a],val);
 75     }
 76     void init() {
 77         memset(w,0,sizeof w);
 78         scanf("%d%d",&n,&m);
 79         cao.init();
 80         ret = cao.S = 0;
 81         cao.T = n + 1;
 82         for(int i = 1; i <= n; ++i) {
 83             scanf("%d%d%d",tunner+i,lev+i,atk+i);
 84             ret += atk[i];
 85             if(tunner[i]) cao.add(i,cao.T,1,0);
 86             else cao.add(cao.S,i,1,0);
 87         }
 88         for(int i = 1; i <= m; ++i) {
 89             int lv,ak,nm,a,b;
 90             scanf("%d%d%d",&lv,&ak,&nm);
 91             if(nm == 0) {
 92                 for(int j = 1; j <= n; ++j) {
 93                     for(int k = j+1; k <= n; ++k)
 94                         if(lev[j] + lev[k] == lv)
 95                             update(j,k,ak - atk[j] - atk[k]);
 96                 }
 97             }
 98             if(nm == 1) {
 99                 scanf("%d",&a);
100                 for(int j = 1; j <= n; ++j) {
101                     if(lev[a] + lev[j] == lv)
102                         update(a,j,ak - atk[a] - atk[j]);
103                 }
104             }
105             if(nm == 2) {
106                 scanf("%d%d",&a,&b);
107                 update(a,b,ak - atk[a] - atk[b]);
108             }
109         }
110         for(int i = 1; i <= n; ++i)
111             for(int j = 1; j <= n; ++j)
112                 if(w[i][j]) cao.add(i,j,1,-w[i][j]);
113         printf("%d\n",ret - cao.solve());
114     }
115 
116 } BB;
117 int main() {
118     int kase;
119     scanf("%d",&kase);
120     while(kase--) BB.init();
121     return 0;
122 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4734652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值