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 in Gensokyo. 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 the Bunkachou. At the k-th day, there are Ck targets, Tk1, Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [Lki, Rki], 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, G1, G2, ..., 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

题解:

题意大概为:n天给m个女孩照相,每个女孩n天内总共至少要照Gi 张相片;  第i天可以照Di张相片,有Ci个女孩可以照相,照相的数目Ci个女孩有各自的上下界数目。问是否有满足这些限制的照相方案,没有输出-1,有的话,找到使照相总数最大的方案,并按顺序输出第1-n天给第i个女孩照相的数目。

有源汇上下界网络流

建图:添加 源汇点  s,t,   添加 超级源汇点  ss,tt 

原图:

s ---0 / d[i]---> i  (1 <= i <= n)

i ---lij / rij---> j+n+1   (1 <= i <= n, j为第i天能照相的女孩编号(0<=j<=m))

j+n+1 ---Gi / INF---> t

 

新图: in[i] 代表原图中所有点的必要弧对每个的出入度的和  in[i] > 0 说明入度 > 出度

in[i] > 0 :   ss ---in[i] --->  i ( 0<= i <= t)

in[i] < 0 :   i ---(-in[i]) --->  tt (0 <= i <= t)

原图中的所有边权值变为  c[i] - b[i]

新增   t  ---INF---> s  (是源汇点流量平衡)

 

1.  在原图的基础上建立新图  , 在新图上跑 maxflow(ss,tt)  如果跑出来的最大流 == 使ss满流,说明存在可行流,即存在可行方案,否则输出 -1;

2.  添加 t  ---(-INF)--- s  消去前面使流量平衡的边,然后跑 maxflow(s,t) = ans 就是答案

(其实这里不消也没关系,仔细深入想想,当跑完可行流时, s - t 会由一个可行流f  所以  t----INF ---> s 会变为 t ---- (INF-f) ---->s,

其反向边会变为s ----f----> t ,如果添加 一个 -INF  到 t - s , 会使t---(INF-f)---> s 的实际值变为 t ---- (-f) --- > s 。从这里我们可以得出两点结论,一个时,当我跑maxflow(s,t) 会直接把可行流算进去,所以不用再加; 另一个是,我的bfs + dfs跑增光路时,遇到 t  便会停止增广, 所以跑 maxflow(s,t) 时  t - s 的所有边实际上并未使用,所以不消也可以得出正确答案,但是我们为了。。。。还是加上为宜~V~)。

3.  Segment Fault  坑死人不偿命的zoj  ,一直段错误, 我前前后后交了 10 多发 ,查了好几个小时的代码, 数组不断的加大,

最后不断删代码暴力提交发现,删到main 函数都是 segment fault  居然是 数组开大了, 数组开大了不应该是报 MLE 吗?????

数组从2e6 改成 1e6 1 A二过!!!!!!!!啊啊啊啊啊啊阿!!!

 

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e3+10;
const int maxm = 1e6 + 10;
const int INF = 0x3f3f3f3f;

int n,m,tot;

int l[maxn],h[maxn],cur[maxn];
struct edge
{
  int to;
  int c;
  int next;
  edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
 }es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
    es[tot] = edge(v,c,h[u]);
    h[u] = tot++;
    es[tot] = edge(u,0,h[v]);
    h[v] = tot++;
}

bool bfs(int s, int t)
{
   memset(l,0,sizeof(l));
   l[s] = 1;
   queue <int> q;
   q.push(s);
   while(!q.empty())
   {
    int u = q.front();
    q.pop();
    if(u == t)  return true;
    for(int i = h[u]; i != -1; i = es[i].next)
        {
         int v = es[i].to;
         if(!l[v] && es[i].c) {l[v] = l[u] + 1; q.push(v);}
        }
   }
   return false;
}

int dfs(int x, int t, int mf)
{
    if(x == t) return mf;
    int ret = 0;
    for(int &i = cur[x]; i != -1; i = es[i].next)
    {
      if(es[i].c && l[x] == l[es[i].to] - 1)
      {
        int f = dfs(es[i].to,t,min(es[i].c,mf - ret));
        es[i].c -= f;
        es[i^1].c += f;
        ret += f;
        if(ret == mf) return ret;
      }
    }
    return ret;
}

int dinic(int s, int t)
{
  int ans = 0;
  while(bfs(s,t))
  {
   for(int i = 0; i <= n+m+5; i++) cur[i] = h[i];
   ans += dfs(s,t,INF);
   }
  return ans;
}



int in[maxn];
int g[maxn];
int d[maxn];
int b[maxm*2];

int main()
{
   while(~scanf("%d%d",&n,&m))
  {
   memset(h,-1,sizeof(h));
   memset(in,0,sizeof(in));
   tot = 0;
   int s = n+m+1,t = n+m+2,ss = 0,tt = t+1;
   for(int i = 1; i <= m; i++)
      scanf("%d",&g[i]);
   int c,totc = 0;
   for(int i = 1; i <= n; i++)
   {
       scanf("%d%d",&c,&d[i]);
       while(c--)
       {
         int y,l,r;
         scanf("%d%d%d",&y,&l,&r);
         y++;
         add_edge(i,y+n,r-l);
         in[i] -= l;
         in[y+n] += l;
         b[totc+1] = l;
         totc += 2;
       }
   }
   for(int i = 1; i <= m; i++) 
   {
       add_edge(i+n,t,INF);
       in[i+n] -= g[i];
       in[t] += g[i];
   }
   for(int i = 1; i <= n; i++)
      add_edge(s,i,d[i]);
   add_edge(t,s,INF);
   
   int res = 0;
   for(int i = 1; i <= t; i++)
   {
      if(in[i] > 0) {add_edge(ss,i,in[i]); res += in[i];}
      if(in[i] < 0) add_edge(i,tt,-in[i]);
   } 
   int ans = dinic(ss,tt);
   if(ans == res)
   {
      add_edge(t,s,-INF);
      int _ans = dinic(s,t);
      printf("%d\n",_ans);
      for(int i = 1; i < totc; i += 2)
        printf("%d\n",es[i].c + b[i]);
   }
   else printf("-1\n");
   printf("\n");
  }
   return 0;
}               

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值