标签:style io os ar for sp on amp ad
Shoot the Bullet
题目:
一个叫雅各的人想在外太空的N天里给M个妹子照相。但是,雅各和妹子对照相都有要求。
每一天雅各有C个妹子愿意让他照相,而每个妹子对照相的都有一个要求,就是总相片书小于Gi,而如果某天出现了则当天的相片数在[L,R].因为,如果多了妹子就会爆发把雅各暴打一顿。而雅各也有要求,就是每天的相片数不超过D,多了雅各也会不高兴。
要求你求出满足条件下最多相片的方案。
算法分析:
有源汇上下界最大流。
这题的建图还是比较容易得到了,就是在输出图的时候我调试了好久。T_T
每一天[0,D]给源点建立,如果,该天某个妹子出现了,则把妹子和这一天连上一条边处理方法是上下界的处理(R-L)如果不懂得可以网上找论文。推荐的时周源的。
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <cstdio> #include <cstring> using namespace std; const int INF = 1 << 30; const int MAXN = 1500; struct Edge{ int from,to,cap,flow,cost; Edge(){}; Edge(int _from,int _to,int _cap,int _flow) :from(_from),to(_to),cap(_cap),flow(_flow){}; }; vector<Edge> edges; vector<int> G[MAXN]; vector<int> road[400]; bool vst[MAXN]; int cur[MAXN],d[MAXN]; int in[MAXN]; int low[400][1005]; int s,t,src,sink,cnt,N,M; void init(){ src = t + 1; sink = src + 1; for(int i = 0;i <= sink;++i) G[i].clear(); for(int i = 0;i <= N;++i) road[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)); int sz = edges.size(); G[from].push_back(sz - 2); G[to].push_back(sz - 1); } bool BFS(){ queue<int> Q; memset(vst,0,sizeof(vst)); Q.push(src); d[src] = 0; vst[src] = 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(!vst[e.to] && e.cap > e.flow){ vst[e.to] = 1; d[e.to] = d[x] + 1; Q.push(e.to); } } } return vst[sink]; } int DFS(int x,int a){ if(x == sink||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[e.to] == d[x] + 1 && (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; } int maxFlow(){ int flow = 0; while(BFS()){ memset(cur,0,sizeof(cur)); flow += DFS(src,INF); } return flow; } bool solve(){ int i,j,sum = 0; for(int i = 1;i < src;++i){ if(in[i] > 0){ sum += in[i]; addEdge(src,i,in[i]); } else { addEdge(i,sink,-in[i]); } } int flow = maxFlow(); if(flow != sum){ puts("-1"); return false; } return true; } int main() { //freopen("Input.txt","r",stdin); while(~scanf("%d%d",&N,&M)){ int x; s = 0; t = N + M + 1; init(); memset(in,0,sizeof(in)); for(int i = 1;i <= M;++i){ scanf("%d",&x); addEdge(i + N,t,INF); in[t] += x; in[i + N] -= x; } int c,d,ll,rr,b; for(int i = 1;i <= N;++i){ scanf("%d%d",&c,&d); addEdge(s,i,d); for(int j = 0;j < c;++j){ scanf("%d%d%d",&x,&b,&rr); ++x; addEdge(i,x + N,rr - b); in[i] -= b; in[x + N] += b; low[i][x] = b; road[i].push_back(x); } } ///!!!!!!!!!!!!!!!!!! niu!!! addEdge(t,s,INF); /* 最大流 s -> t : 0 , t - > s : INF 构造无源无汇上下界网络 最小流 s->t: INF , t - > s : 0 */ int ans = 0; if(solve()){ src = s; sink = t; int flow = maxFlow(); printf("%d\n",flow); for(int i = 1;i <= N;++i){ for(int j = 0;j < G[i].size();++j){ Edge& e = edges[G[i][j]]; if(e.to > N && e.to <= N + M){ low[i][e.to - N] += edges[G[i][j]].flow; //相邻边必满流 } } } for(int i = 1;i <= N;++i){ for(int j = 0;j < road[i].size();++j) printf("%d\n",low[i][road[i][j]]); } } puts(""); } return 0; }
标签:style io os ar for sp on amp ad
原文地址:http://blog.csdn.net/zhongshijunacm/article/details/39924837