网络流裸题:

分两部分建图,求不要求满流的最大费用最大流.....

Yu-Gi-Oh!

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 401    Accepted Submission(s): 108

Problem Description
"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=0 for
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
 
Author
SXYZ
 

/* ***********************************************
Author :CKboss
Created Time :2015年08月17日 星期一 08时42分00秒
File Name :HDOJ5383.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int INF=0x3f3f3f3f;
const int maxv=400;
const int maxn=maxv*maxv; struct Edge
{
int to,next,cap,flow,cost;
}edge[maxn]; int n,m;
int Adj[maxv],Size,N; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void addedge(int u,int v,int cap,int cost)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
edge[Size].cost=cost;
edge[Size].cap=cap;
edge[Size].flow=0;
Adj[u]=Size++;
} void Add_Edge(int u,int v,int cap,int cost)
{
addedge(u,v,cap,cost);
addedge(v,u,0,-cost);
} int dist[maxv];
int vis[maxv],pre[maxv]; bool spfa(int s,int t)
{
queue<int> q;
for(int i=0;i<N;i++)
{
dist[i]=-INF; vis[i]=false; pre[i]=-1;
}
dist[s]=0; vis[s]=true; q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&
dist[v]<dist[u]+edge[i].cost)
{
dist[v]=dist[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(pre[t]==-1) return false;
return true;
} int MinCostMaxFlow(int s,int t,int &cost)
{
int flow=0;
cost=0;
while(spfa(s,t))
{
int Min=INF;
for(int i=pre[t];~i;i=pre[edge[i^1].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
}
if(dist[t]<0) break;
for(int i=pre[t];~i;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
}
return flow;
} struct Moster
{
Moster(){}
Moster(int l,int a):level(l),ATK(a){}
int level,ATK;
}; vector<Moster> m0,m1;
int turn[maxv],pos[maxv];
int GG[maxv][maxv]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
init(); m0.clear(); m1.clear();
memset(GG,0,sizeof(GG));
int sumATK=0;
int sz1=0,sz2=0;
for(int i=0,t,l,a;i<n;i++)
{
scanf("%d%d%d",&t,&l,&a);
if(t==0)
{
m0.push_back(Moster(l,a));
turn[i]=0; pos[i]=sz1++;
}
else if(t==1)
{
m1.push_back(Moster(l,a));
turn[i]=1; pos[i]=sz2++;
}
sumATK+=a;
}
for(int i=0,l,a,r;i<m;i++)
{
scanf("%d%d%d",&l,&a,&r);
if(r==0)
{
for(int j=0;j<sz1;j++)
{
for(int k=0;k<sz2;k++)
{
int u=j+1,v=k+sz1+1;
if(m0[j].level+m1[k].level==l)
{
if(a>m0[j].ATK+m1[k].ATK)
{
GG[u][v]=max(GG[u][v],a-m0[j].ATK-m1[k].ATK);
}
}
}
}
}
else if(r==1)
{
int x;
scanf("%d",&x); x--;
if(turn[x]==0)
{
int P=pos[x];
for(int j=0;j<sz2;j++)
{
int u=P+1,v=j+sz1+1;
if(m0[P].level+m1[j].level==l)
{
if(a>m0[P].ATK+m1[j].ATK)
{
GG[u][v]=max(GG[u][v],a-m0[P].ATK-m1[j].ATK);
}
}
}
}
else if(turn[x]==1)
{
int P=pos[x];
for(int j=0;j<sz1;j++)
{
int u=j+1,v=P+sz1+1;
if(m0[j].level+m1[P].level==l)
{
if(a>m0[j].ATK+m1[P].ATK)
{
GG[u][v]=max(GG[u][v],a-m0[j].ATK-m1[P].ATK);
}
}
}
}
}
else if(r==2)
{
int x,y;
scanf("%d%d",&x,&y); x--; y--;
if(turn[x]==1) swap(x,y);
int u=pos[x]+1,v=sz1+pos[y]+1;
if(a>m0[pos[x]].ATK+m1[pos[y]].ATK)
{
GG[u][v]=max(GG[u][v],a-m0[pos[x]].ATK-m1[pos[y]].ATK);
}
}
} for(int i=1;i<=sz1;i++)
{
for(int j=sz1+1;j<=sz1+sz2;j++)
{
if(GG[i][j]>0) Add_Edge(i,j,1,GG[i][j]);
}
} int S=0,T=sz1+sz2+1;
for(int i=1;i<=sz1;i++) Add_Edge(0,i,1,0);
for(int i=sz1+1;i<=sz1+sz2;i++) Add_Edge(i,T,1,0); int flow,cost; N=sz1+sz2+2;
flow=MinCostMaxFlow(S,T,cost);
printf("%d\n",sumATK+cost);
} return 0;
}

HDOJ 5383 Yu-Gi-Oh! 最大费用最大流的更多相关文章

  1. hdoj 1533 Going Home 【最小费用最大流】【KM入门题】

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. Matrix Again(最大费用最大流)

    Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Tota ...

  3. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  4. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  5. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  6. 【Codeforces717G】Underfail Hash + 最大费用最大流

    G. Underfail time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  7. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  8. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  9. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

随机推荐

  1. C语言指针的理解以及指针的指针的理解

    指针指向的是内存地址编号,内存地址编号指向的是对应的内容. 我们需要一个变量,来储存内存地址编号,这个变量的值是一个内存地址编号,但是我们可以通过修改变量的值,来不断的改变内存地址编号. 但是,我们如 ...

  2. CSS垂直居中和水平居中的几种方法

    垂直居中 方法一 这个方法把div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align属性. <!DOCTYPE html> <html lang=" ...

  3. Unity笔记(2)自学第一天

    学习记录: 界面使用:

  4. (8)string对象上的操作1

     读写操作 //读写string对象的测试.//本程序输入两string类,输出两string类. #include <iostream> #include <string> ...

  5. opencv总结

    2018-02-2623:59:02 唉,这软件我很烦躁,今天又搞了好几遍,出错提示的时候总是出问题! 而且,无论什么错误,都是提示一堆乱码! 定义ROI区域有两种方法,第一种是使用cv:Rect.顾 ...

  6. 上传一个npm包

    1.先创建一个npm账号 https://www.npmjs.com/signup 2.在cmd里输入命令进入项目文件夹 3.使用npm init 命令创建一个package.json(确保nodej ...

  7. 4星|《OKR工作法》:关注公司的真正目标,以周为单位做计划和考核

    本书篇幅比较小,两个小时就可以看完.主要内容讲OKR工作法的基本概念,然后用一个虚拟的创业公司的创业故事来演示实施OKR过程中可能遇到的问题.OKR给创业带来的好处. OKR工作法相对来说是比较简单的 ...

  8. 3星|林毅夫《战胜命运》:事实证明华盛顿共识是错误的,GIFF是穷国发展正道。

    本书是林毅夫与喀麦隆一位经济学家合著.基本的观点是:事实证明华盛顿共识是错误的,GIFF是穷国发展正道.GIFF的主要思想是政府找到对标国家,强力推行产业政策. 作为一个经济学外行,读后感觉关于华盛顿 ...

  9. ubuntu16.04安装teamviewer12

    安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb   ...

  10. (转)WKT转换工具terraformers

    http://blog.csdn.net/gisshixisheng/article/details/53150111 概述: 前面的文章中,提到了Arcgis中实现wkt转换为geometry,但是 ...