ZOJ 3229 Shoot the Bullet 无源汇上下界最大流

简介:
Shoot the Bullet

Time Limit: 2 Seconds       Memory Limit: 32768 KB       Special Judge

Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies, youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been in Gensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns the Bunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautiful danmaku(barrange) or cute girls living inGensokyo. She is the biggest connoisseur of rumors about the girls of Gensokyo among the tengu. Her intelligence gathering abilities are the best in Gensokyo!

During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at least Gx photos of girl x in total in theBunkachou. At the k-th day, there are Ck targets, Tk1Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [LkiRki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use her last spell card to attack Aya. What's more, Aya cannot take more than Dk photos at the k-th day. Under these constraints, the more photos, the better.

Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

Input

There are about 40 cases. Process to the end of file.

Each case begins with two integers 1 <= n <= 365, 1 <= m <= 1000. Then m integers, G1G2, ..., Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then C different targets. Each target is described by three integers, 0 <= T < m, 0 <= L <= R <= 100.

Output

For each case, first output the number of photos Aya can take, -1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

Output a blank line after each case.

Sample Input

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 3 9
1 3 9
2 3 9

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9

2 3
12 12 12
3 15
0 3 9
1 3 9
2 3 9
3 21
0 0 3
1 3 6
2 6 12

Sample Output

36
6
6
6
6
6
6

36
9
6
3
3
6
9

-1

External Links

TeamShanghaiAlice(banner) 
Wikipedia
Touhou Wiki


把每一天看成一个点,每一个女孩也看成一个点,添加源和汇,源向每一天连上[0,d]的边。每一天与每一个女孩假设有拍照任务的话连上[l,c]的边,每一个女孩与汇连上[g,oo]的边,于是构成一个有上下界的图,直接用上下界最大流来搞即可了

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

const int MAXN=2222;

struct Edge{
    int from,to,cap,flow;
};
bool cmp(const Edge& a,const Edge& b){
    return a.from < b.from || (a.from == b.from && a.to < b.to);
}
struct Dinic{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];
    int d[MAXN];
    int cur[MAXN];
    void init(int n){
        this->n=n;
        for(int i=0;i<=n;i++)G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back((Edge){from,to,cap,0});
        edges.push_back((Edge){to,from,0,0});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool BFS(){
        CLR(vis,0);
        queue<int> Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty()){
            int x=Q.front();
            Q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge& e=edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x,int a){
        if(x==t||a==0)return a;
        int flow=0,f;
        for(int& i=cur[x];i<G[x].size();i++){
            Edge& e=edges[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)break;
            }
        }
        return flow;
    }
    //当所求流量大于need时就退出。减少时间
    int Maxflow(int s,int t,int need){
        this->s=s;this->t=t;
        int flow=0;
        while(BFS()){
            CLR(cur,0);
            flow+=DFS(s,INF);
            if(flow>need)return flow;
        }
        return flow;
    }
    //最小割割边
    vector<int> Mincut(){
        BFS();
        vector<int> ans;
        for(int i=0;i<edges.size();i++){
            Edge& e=edges[i];
            if(vis[e.from]&&!vis[e.to]&&e.cap>0)ans.push_back(i);
        }
        return ans;
    }
    void Reduce(){
        for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
    }
    void ClearFlow(){
        for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
    }
};

int w[MAXN],d[MAXN],low[100010];

Dinic solver;

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        solver.init(n+m+3);
        int tmp;
        CLR(w,0),CLR(d,0),CLR(low,0);
        FOR(i,1,m){
            scanf("%d",&tmp);
            w[n+i]-=tmp;
            w[n+m+1]+=tmp;
        }
        int c;
        int g=0;
        FOR(i,1,n){
            scanf("%d%d",&c,&d[i]);
            int t;
            REP(j,c){
                scanf("%d%d%d",&t,&low[++g],&tmp);
                t++;
                solver.AddEdge(i,t+n,tmp-low[g]);
                w[i]-=low[g];
                w[t+n]+=low[g];
            }
        }
        FOR(i,1,n) solver.AddEdge(0,i,d[i]);
        FOR(i,1,m) solver.AddEdge(n+i,n+m+1,INF);
        solver.AddEdge(n+m+1,0,INF);
        int sum=0;
        FOR(i,0,n+m+1){
            if(w[i]>0){
                solver.AddEdge(n+m+2,i,w[i]);
                sum+=w[i];
            }
            if(w[i]<0)
                solver.AddEdge(i,n+m+3,-w[i]);
        }
        int ans=solver.Maxflow(n+m+2,n+m+3,2*INF);
        //cout<<sum<<" "<<ans<<endl;
        if(ans!=sum){
            printf("-1\n");
        }
        else{
            tmp=solver.Maxflow(0,n+m+1,INF);
            cout<<tmp<<endl;
            FOR(i,1,g)
             printf("%d\n",solver.edges[2*i-2].flow+low[i]);
        }
        printf("\n");
    }
    return 0;
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5153858.html,如需转载请自行联系原作者
相关文章
|
监控 安全 网络安全
探讨企业邮箱安全问题:必须关注的四个关键要点
电子邮件是企业沟通的核心,但伴随着钓鱼邮件和恶意软件等安全威胁。确保企业邮箱安全需关注四点:数据安全、电子邮件安全、基础设施安全和账户安全。邮件系统应具备预测性防护机制,防止网络钓鱼、品牌伪造和恶意软件。强大的垃圾邮件过滤器和加密技术(如S/MIME和PGP)保护邮件内容,同时多因素认证和异常活动监控增强账户安全性。选择邮件服务商时,要确保他们遵循ISO和GDPR等安全标准,提供安全的云环境。
304 1
|
10月前
|
人工智能 Oracle 关系型数据库
全球CRM系统市场份额分析:领导者与挑战者
随着全球企业对CRM系统的依赖性增加,CRM市场正经历快速增长和激烈竞争。本文分析了全球CRM市场的份额,介绍了市场领导者如Salesforce、Oracle、SAP、纷享销客等,以及挑战者如HubSpot CRM、Zoho CRM等,并预测了未来市场趋势,包括云计算的普及、人工智能的融合和本土化需求的增加。
|
11月前
|
JavaScript 前端开发 大数据
在JavaScript中,Object.assign()方法或展开语法(...)来合并对象,Object.freeze()方法来冻结对象,防止对象被修改
在JavaScript中,Object.assign()方法或展开语法(...)来合并对象,Object.freeze()方法来冻结对象,防止对象被修改
177 0
|
JavaScript 前端开发 IDE
TypeScript:前端世界的“甜蜜烦恼”——究竟该不该用?
TypeScript:前端世界的“甜蜜烦恼”——究竟该不该用?
175 0
|
存储 Kubernetes 安全
在k8S中,Secret 有哪些使用方式?
在k8S中,Secret 有哪些使用方式?
|
数据采集 监控 大数据
DATEWORES: 构建高效数据管道的最佳实践
【8月更文第14天】随着大数据技术的发展,数据管道已经成为现代数据处理流程的核心部分。本文旨在探讨如何利用DATEWORES——一个假设性的数据管道构建框架——来设计和实现高效的数据管道。我们将介绍DATEWORES的基本概念、架构设计,并通过具体案例演示如何运用该框架进行数据的抽取、转换与加载(ETL)。此外,我们还将讨论如何保证数据质量及数据完整性。
184 0
|
传感器
GEE——使用cart机器学习方法对Landsat影像条带修复以NDVI和NDWI为例(全代码)
GEE——使用cart机器学习方法对Landsat影像条带修复以NDVI和NDWI为例(全代码)
371 0
|
机器学习/深度学习 自然语言处理 TensorFlow
使用Python实现深度学习模型:Transformer模型
使用Python实现深度学习模型:Transformer模型
1519 0
使用Python实现深度学习模型:Transformer模型
|
前端开发 Java Maven
属性编辑器未在PropertyEditorManager中注册?
属性编辑器未在PropertyEditorManager中注册?