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
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
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, G1, G2, ..., Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then C differenttargets. 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
題意: 給m個女神拍照,計劃拍照n天,每一天給定的C個女神拍照,每天拍照數不能超過D張,而且給每個女神i拍照有數量限制[Li,Ri],對於每個女神n天的拍照總和不能少於Gi,如果有解求最多能拍多少張照,並求每天給對應女神拍多少張照;否則輸出-1。
分析: 這是一個有源匯點的圖,需要新建超級源點ss和超級匯點,還有一些使滿足可行流的附加邊,必須保證附加邊滿流,才能保證符合條件,才能求最大流。如果不懂有上下界的參考:https://blog.csdn.net/lml11111/article/details/82939666
程式碼:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int MAXN=44222;
const int MAXM=888888;
struct Edge{
int to, c, next;
};
Edge edge[MAXM];
int head[MAXN], cnt;
int dis[MAXN], num[MAXN], cur[MAXN], pre[MAXN];
int s, t, nv,p;
int n,m;
void addedge(int u, int v, int c){
edge[cnt].to= v; edge[cnt].c = c; edge[cnt].next = head[u]; head[u] = cnt++;
edge[cnt].to = u; edge[cnt].c = 0; edge[cnt].next = head[v]; head[v] = cnt++;
}
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void bfs(int s,int t)
{
queue<int>q;
memset(dis,-1,sizeof(dis));
memset(num,0,sizeof(num));
while(!q.empty()) q.pop();
dis[t] = 0;
q.push(t);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dis[v]==-1){
dis[v]=dis[u]+1;
num[dis[v]]++;
q.push(v);
}
}
}
}
int ISAP(int s,int t){
memcpy(cur,head,sizeof(head));
bfs(s,t);
int flow = 0, u = pre[s] = s, i;
while(dis[s] < nv){
if(u==t){
int f = inf, neck;
for(i = s; i != t; i = edge[cur[i]].to){
if(f > edge[cur[i]].c){
f = edge[cur[i]].c;
neck = i;
}
}
for(i = s; i != t; i = edge[cur[i]].to){
edge[cur[i]].c -= f;
edge[cur[i] ^ 1].c += f;
}
flow += f;
u = neck;
}
for(i = cur[u]; ~i; i = edge[i].next) if(edge[i].c &&dis[u] == dis[edge[i].to] + 1) break;
if(~i){
cur[u] = i;
pre[edge[i].to] = u;
u = edge[i].to;
}
else{
if(0 == (--num[dis[u]])) break;
int mind = nv;
for(i = head[u]; ~i; i = edge[i].next){
if(edge[i].c && mind > dis[edge[i].to]){
mind = dis[edge[i].to];
cur[u] = i;
}
}
dis[u] = mind + 1;
num[dis[u]]++;
u = pre[u];
}
}
return flow;
}
int du[MAXN];
int dn[500][2100];
int id[500][2100];
int main()
{
while(~scanf("%d%d",&n,&m))
{
int s=0,t=n+m+1;
int ss=n+m+2,tt=n+m+3;
nv=tt+1;
int a,b,cc;
memset(du,0,sizeof(du));///附加邊的資訊
memset(dn,0,sizeof(dn));///記錄第i天給第j個人拍照的最低標準
memset(id,0,sizeof(id));///記錄第i天給第j個人拍照的邊號,以便查詢裡面的流量
init();
for(int i=1;i<=m;i++)
{
scanf("%d",&a);
addedge(n+i,t,inf-a);
du[n+i]-=a;
du[t]+=a;
}
int C,D;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&C,&D);
addedge(s,i,D);
for(int j=1;j<=C;j++)
{
scanf("%d%d%d",&a,&b,&cc);
addedge(i,a+n+1,cc-b);
du[i]-=b;
du[a+n+1]+=b;
dn[i][a]=b;
id[i][a]=cnt-2;
}
}
addedge(t,s,inf);
int sum=0;
for(int i=1;i<=t;i++)
{
if(du[i]<0)
{
addedge(i,tt,-du[i]);
}
else if(du[i]>0)
{
addedge(ss,i,du[i]);
sum+=du[i];
}
}
//printf("jfodjfidf\n");
nv=tt+1;
int maxflow=ISAP(ss,tt);
//printf("sssss %d %d\n",sum,maxflow);
if(maxflow==sum)
{
nv=t+1;
int ans=ISAP(s,t);
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
if(id[i][j])
printf("%d\n",edge[id[i][j]^1].c+dn[i][j]);
}
}
else printf("-1\n");
printf("\n");
}
return 0;
}