fork download
  1. //Lib
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<ctime>
  7.  
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<string>
  12. #include<queue>
  13. #include<stack>
  14. #include<set>
  15. #include<map>
  16. using namespace std;
  17. //Macro
  18. #define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)
  19. #define drep(i,a,b) for(int i=a,tt=b;i>=tt;--i)
  20. #define erep(i,e,x) for(int i=x;i;i=e[i].next)
  21. #define irep(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
  22. #define read() (strtol(ipos,&ipos,10))
  23. #define sqr(x) ((x)*(x))
  24. #define pb push_back
  25. #define PS system("pause");
  26. typedef long long ll;
  27. typedef pair<int,int> pii;
  28. const int oo=~0U>>1;
  29. const double inf=1e100;
  30. const double eps=1e-6;
  31. string name="", in=".in", out=".out";
  32. //Var
  33. struct E
  34. {
  35. int next,node;
  36. ll cap,v;
  37. }e[1000008];
  38. int n,m,st,ed,tot=1;
  39. ll ans;
  40. int h[508],s[258],pre[508],epre[508];
  41. ll t[258][10],w[258][10];
  42. ll dis[508];
  43.  
  44. bool vis[508];
  45. queue<int> q;
  46. void add(int a,int b,ll c,ll d)
  47. {
  48. e[++tot].next=h[a];e[tot].node=b;e[tot].cap=c;e[tot].v=d;h[a]=tot;
  49. e[++tot].next=h[b];e[tot].node=a;e[tot].cap=0;e[tot].v=-d;h[b]=tot;
  50. }
  51. void Init()
  52. {
  53. scanf("%d%d",&m,&n);st=m+n+1;ed=st+1;
  54. int c;
  55. rep(i,1,n)scanf("%d",&c),add(st,i,c,0);
  56. rep(i,1,m)rep(j,1,n)
  57. {
  58. scanf("%d",&c);
  59. if(c)add(j,i+n,oo,0);
  60. }
  61. rep(i,1,m)
  62. {
  63. scanf("%d",&s[i]);
  64. rep(j,1,s[i])
  65. scanf("%d",&t[i][j]);
  66. rep(j,1,s[i]+1)
  67. scanf("%d",&w[i][j]);
  68. }
  69. rep(i,1,m)
  70. {
  71. rep(j,1,s[i])
  72. add(i+n,ed,t[i][j]-t[i][j-1],w[i][j]);
  73. add(i+n,ed,oo,w[i][s[i]+1]);
  74. }
  75. }
  76. bool SPFA()
  77. {
  78. memset(dis,55,sizeof dis);
  79. q.push(st);dis[st]=0;int u,v;bool flag=false;
  80. while(!q.empty())
  81. {
  82. u=q.front();q.pop();vis[u]=false;
  83. erep(i,e,h[u])
  84. {
  85. if(dis[v=e[i].node]>dis[u]+e[i].v&&e[i].cap)
  86. {
  87. dis[v]=dis[u]+e[i].v;pre[v]=u;epre[v]=i;
  88. if(!vis[v])q.push(v),vis[v]=true;
  89. if(v==ed)flag=true;
  90. }
  91. }
  92. }
  93. return flag;
  94. }
  95. ll Flow()
  96. {
  97. int now;
  98. ll sum=0,low=oo;
  99. now=ed;
  100. while(now!=st)
  101. {
  102. low=min(low,e[epre[now]].cap);
  103. sum+=e[epre[now]].v;
  104. now=pre[now];
  105. }
  106. now=ed;
  107. while(now!=st)
  108. {
  109. e[epre[now]].cap-=low;
  110. e[epre[now]^1].cap+=low;
  111. now=pre[now];
  112. }
  113. return (ll)low*sum;
  114. }
  115. void Work()
  116. {
  117. while(SPFA())
  118. ans+=Flow();
  119. cout<<ans<<endl;
  120. }
  121. int main()
  122. {
  123. // freopen((name+in).c_str(),"r",stdin);
  124. // freopen((name+out).c_str(),"w",stdout);
  125. Init();
  126. Work();
  127. return 0;
  128. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty