Code:

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=2005;
const int maxd=1000000+233;
const int INF=10000000;
# define pb push_back
int sss,ttt;
int s,t;
int E1,E2;
int mapp[maxd],A[maxn],val[maxd];
struct Edge{
int from,to,cap;
Edge(int u,int v,int c):from(u),to(v),cap(c) {}
};
vector<Edge>edges;
struct Dicnic{
vector<int>G[maxn];
int d[maxn],vis[maxn],cur[maxn];
queue<int>Q;
void init(){
for(int i=0;i<maxn;++i)G[i].clear();
edges.clear();
}
void addedge(int u,int v,int c,int cnt)
{
edges.pb(Edge(u,v,c));
edges.pb(Edge(v,u,0));
int m=edges.size();
G[u].pb(m-2); //正向弧
G[v].pb(m-1); //反向弧
E1=m-2,E2=m-1;
if(cnt>0)mapp[cnt]=m-1;
}
int BFS()
{
memset(vis,0,sizeof(vis));
d[s]=0,vis[s]=1;Q.push(s);
while(!Q.empty())
{
int u=Q.front();Q.pop();
int sz=G[u].size();
for(int i=0;i<sz;++i){
Edge e=edges[G[u][i]];
if(!vis[e.to]&&e.cap>0&&e.to!=sss&&e.to!=ttt){
d[e.to]=d[u]+1,vis[e.to]=1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t)return a;
int sz=G[x].size();
int f,flow=0;
for(int i=cur[x];i<sz;++i)
{
Edge e=edges[G[x][i]];
cur[x]=i;
if(d[e.to]==d[x]+1&&e.cap>0&&e.to!=sss&&e.to!=ttt)
{
f=dfs(e.to,min(a,e.cap));
if(f)
{
int u=G[x][i];
a-=f;
edges[u].cap-=f;
edges[u^1].cap+=f;
flow+=f;
if(a==0)break;
}
}
}
return flow;
}
int maxflow(){
int ans=0;
while(BFS()){
memset(cur,0,sizeof(cur));
ans+=dfs(s,INF);
}
return ans;
}
}op;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
op.init();
memset(A,0,sizeof(A));
int sum=0,cnt=0,ss,tt;
ss=0,tt=n+m+1;
for(int i=0;i<m;++i){
int a;scanf("%d",&a);
op.addedge(n+1+i,tt,INF-a,0);
A[n+1+i]-=a,A[tt]+=a;
}
for(int i=1;i<=n;++i){
int c,d;scanf("%d%d",&c,&d);
op.addedge(ss,i,d,0);
for(int j=1;j<=c;++j)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
++cnt;
op.addedge(i,x+n+1,z-y,cnt);
val[cnt]=y;
A[i]-=y,A[x+n+1]+=y;
}
}
s=1500,t=1501;
for(int i=0;i<=n+m+1;++i)
{
if(A[i]>0){op.addedge(s,i,A[i],0);sum+=A[i];}
if(A[i]<0)op.addedge(i,t,-A[i],0);
}
op.addedge(tt,ss,INF,0);
sss=-1,ttt=-2;
int ans1=op.maxflow();
sss=1500,ttt=1501;
if(ans1!=sum){printf("-1\n");printf("\n");}
else
{
ans1=edges[E2].cap;
edges[E1].cap=edges[E2].cap=0;
s=ss,t=tt;
ans1+=op.maxflow();
printf("%d\n",ans1);
for(int i=1;i<=cnt;++i)
printf("%d\n",edges[mapp[i]].cap+val[i]);
printf("\n");
}
return 0;
}

  

Shoot the Bullet ZOJ - 3229有上下界网络流的更多相关文章

  1. Shoot the Bullet ZOJ - 3229 有源汇有上下界的最大流

    /** zoj提交评判不了,所以不知道代码正不正确.思路是应该没问题的.如果有不对的地方,请多指教. 题目:Shoot the Bullet ZOJ - 3229 链接:https://vjudge. ...

  2. ZOJ 3229 有上下界最大流

    1: /** 2: ZOJ 3229 有上下界的最大流 3: 两次求最大流的过程,非二分 4: 有源汇上下界的最大流问题, 首先连接 sink -> src, [0,INF]. 5: 根据net ...

  3. zoj 3229 Shoot the Bullet(有源汇上下界最大流)

    Shoot the Bullethttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 Time Limit: 2 Second ...

  4. Shoot the Bullet(有源汇带上下界最大流)

    有源汇带上下界最大流 在原图基础上连一条汇点到源点流量为inf的边,将有源汇网络流转化为无源汇网络流用相同方法判断是否满流,如果满流再跑一边源点到汇点的最大流就是答案 例题:Shoot the Bul ...

  5. zoj3229 Shoot the Bullet(有源汇有上下界的最大流)

    题意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给给定的C个女神拍照,每天拍照数不能超过D张,而且给每个女神i拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能少于Gi,如果有解求屌 ...

  6. ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)

    #pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...

  7. ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

    题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...

  8. 【有上下界网络流】【ZOJ】2314 Reactor Cooling

    [算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...

  9. ZOJ 3496 Assignment | 二分+有上下界网络流

    题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3496 大概意思:给你一个网络,有源汇,在保证最大流的情况下求下面两 ...

随机推荐

  1. 关于约束ENABLE NOVALIDATE的一个疑问

    http://www.dbunix.com/?p=188 关于约束ENABLE NOVALIDATE的一个疑问 CREATE TABLE test (id varchar2(12), name var ...

  2. Django(七)

    一.ModelForm操作及验证 1.class Meta:class Meta: #注意以下字段不能加逗号 model = models.UserInfo #这里的all代指所用的字段,也可以是一个 ...

  3. @Zookeeper可视化工具。 ZK 安装 node-zk-browser。2015.10.22亲测可用

    zookeeper基本是基于API和console进行znode的操作,并没有一个比较方便的操作界面,这里也发现了taobao 伯岩写的一个工具,可以比较方便的查询zookeeper信息. 工具的开发 ...

  4. javap命令

    javap命令 学习了:https://www.cnblogs.com/frinder6/p/5440173.html javap命令查看java类的字节码: 对于synchronized块,可以显示 ...

  5. 【Storm】storm安装、配置、使用以及Storm单词计数程序的实例分析

    前言:阅读笔记 storm和hadoop集群非常像.hadoop执行mr.storm执行topologies. mr和topologies最关键的不同点是:mr执行终于会结束,而topologies永 ...

  6. android制作闪动的红心

    先上一张效果图吧: 说说这个东西的来源吧.今天突然想到笛卡尔心形图,想去看看能不能画个心出来,可是看到一篇不错的文章,那篇文章罗列了非常多关于心形的函数方程,这可把我高兴坏了,于是我选取了一个比較好看 ...

  7. Thinking in Java:容器深入研究

    1.虚线框表示Abstract类,图中大量的类的名字都是以Abstract开头的,它们仅仅是部分实现了特定接口的工具,因此创建时能够选择从Abstract继承. Collections中的实用方法:挑 ...

  8. getLocationInWindow getLocationOnScreen getLeft , getTop, getBottom,getRight

    版权声明:本文为博主原创文章,未经博主允许不得转载. 最近做项目时,发现在activity的onCreate()和onResume()方法里调用View.getLocationInWindow() 时 ...

  9. 利用Powershell和ceye.io实现Windows账户密码回传

    利用Powershell和ceye.io实现Windows账户密码回传 转自:http://www.freebuf.com/articles/system/129068.html 最近在研究Power ...

  10. sublime界面主题

    一直以来都是使用的SUBLIME,真的很强大. 最近刚转到linux来学习C,把它重新配置了一遍,默认的字体颜色的搭配已经很不错了.不过界面的样子还是不太习惯.重新安装了下soda这个主题包,惭愧!即 ...