[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2879

[算法]

首先 , 将每种食物建一个点 , 将每位厨师做的每一道菜建一个点

建图如下 :

1. 将原点与每种食物连一条流量为Ai , 费用为0的边

2. 将每种食物像每位厨师的每道菜连一条流量为1 , 费用为Ti,j * k的边(其中 , i表示第i种食物 , j表示第j位厨师 , k表示该厨师做的倒数第k道菜)

3. 将每位厨师做的每道菜向汇点连一条流量为1 , 费用为0的边

在这张图上跑最小费用最大流即为答案

由于边太多 , 我们需要动态加边 , 否则将无法在时限内通过此题

时间复杂度 : O(Costflow(NM + P , NMP))

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 80
#define MAXM 110
#define MAXP 1010
const int INF = 2e9; struct edge
{
int to , w , cost , nxt;
} e[MAXN * MAXM * MAXP]; int n , m , cnt , tot , ans , S , T;
int a[MAXN] , dist[MAXM * MAXP] , head[MAXM * MAXP] , pre[MAXM * MAXP] , incf[MAXM * MAXP];
int t[MAXN][MAXM];
bool inq[MAXM * MAXP]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v , int w , int cost)
{
++tot;
e[tot] = (edge){v , w , cost , head[u]};
head[u] = tot;
++tot;
e[tot] = (edge){u , , -cost , head[v]};
head[v] = tot;
}
inline bool spfa()
{
queue< int > q;
for (int i = ; i <= T; i++)
{
dist[i] = INF;
incf[i] = INF;
inq[i] = false;
pre[i] = ;
}
q.push(S);
inq[S] = true;
dist[S] = ;
while (!q.empty())
{
int cur = q.front();
q.pop();
inq[cur] = false;
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w , cost = e[i].cost;
if (w > && dist[cur] + cost < dist[v])
{
dist[v] = dist[cur] + cost;
pre[v] = i;
incf[v] = min(incf[cur] , w);
if (!inq[v])
{
q.push(v);
inq[v] = true;
}
}
}
}
if (dist[T] < INF) return true;
else return false;
}
inline void update()
{
int now = T;
while (now != S)
{
int pos = pre[now];
e[pos].w -= incf[T];
e[pos ^ ].w += incf[T];
now = e[pos ^ ].to;
}
ans += dist[T] * incf[T];
int pos = (e[pre[T] ^ ].to - n - ) / cnt + , v = (e[pre[T] ^ ].to - n - ) % cnt + ;
for (int i = ; i <= n; i++) addedge(i , e[pre[T] ^ ].to + , , t[i][pos] * (v + ));
} int main()
{ read(n); read(m);
for (int i = ; i <= n; i++)
{
read(a[i]);
cnt += a[i];
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
read(t[i][j]);
}
}
tot = ;
S = n + m * cnt + , T = S + ;
for (int i = ; i <= n; i++) addedge(S , i , a[i] , );
for (int i = ; i <= m * cnt; i++) addedge(n + i , T , , );
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
addedge(i , n + (j - ) * cnt + , , t[i][j]);
}
}
while (spfa()) update();
printf("%d\n" , ans); return ; }

[NOI 2012] 美食节的更多相关文章

  1. Solution -「NOI 2012」「洛谷 P2050」美食节

    \(\mathcal{Description}\)   Link.   美食节提供 \(n\) 种菜品,第 \(i\) 种的需求量是 \(p_i\),菜品由 \(m\) 个厨师负责制作,第 \(j\) ...

  2. 高等数学(拉格朗日乘子法):NOI 2012 骑行川藏

    [NOI2012] 骑行川藏 输入文件:bicycling.in   输出文件:bicycling.out   评测插件 时间限制:1 s   内存限制:128 MB NOI2012 Day1 Des ...

  3. 【矩阵乘】【NOI 2012】【cogs963】随机数生成器

    963. [NOI2012] 随机数生成器 ★★ 输入文件:randoma.in 输出文件:randoma.out 简单对照 时间限制:1 s 内存限制:128 MB **[问题描写叙述] 栋栋近期迷 ...

  4. NOI 2012 随机数生成器

    看到全是矩阵的题解,我来一发递推+分治 其实这题一半和poj1845很像(或是1875?一个叫Sumdiv的题) 言归正传,我们看看怎么由f(0)推出f(n) 我们发现,题目中给出了f(n)=af(n ...

  5. 解题:NOI 2012 骑行川藏

    题面 入手点是每段路程中能量$e$与时间$t$的关系,$t-e$这个函数的导数对于各个路段一样,否则我们可以从导数大的一段路抽出一部分能量分给导数小的,这样会更优 毕姥爷在考场上的做法:猜一猜,然后拿 ...

  6. NOI 2012 魔幻棋盘 | 二维差分 + 二维线段树

    题目:luogu 2086 二维线段树,按套路差分原矩阵,gcd( x1, x2, ……, xn ) = gcd( xi , x2 - x1 , ……, xn - xn-1 ),必须要有一个原数 xi ...

  7. NOI 2012 【迷失游乐园】

    这道题,额,反正我是刚了2天,然后就萎了......(是不是觉得我很菜) 题目描述: 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩. 进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐 ...

  8. 【数论Day1】 最大公约数(gcd)题目

    20170529-3数论_gcd 题解: http://www.cnblogs.com/ljc20020730/p/6919116.html 日期 序号 题目名称 输入文件名 输出文件名 时限 内存 ...

  9. 【FINAL】NOI

    我就是复习一下..根本就不是什么题解...谁也看不懂的... NOI2007 社交网络         最短路 货币兑换         斜率优化动态规划 项链工厂         线段树 生成树计数 ...

随机推荐

  1. NYOJ760-See LCS again,有技巧的暴力!

    See LCS again 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...

  2. [Istioc]Istio部署sock-shop时rabbitmq出现CrashLoopBackOff

    因Istio官网自带的bookinfo服务依赖关系较少,因此想部署sock-shop进行进一步的实验. kubectl apply -f <(istioctl kube-inject -f so ...

  3. spoj 839 最小割+二进制

    #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...

  4. [HNOI2015]实验比较 树形dp+组合数学

    在合并的时候有可以加等于,或者继续用小于, 比如siz[x]和siz[y]合并,小于的区间为max(siz[x],siz[y])<=k<=siz[x]+siz[y], 然后就是合并成多少个 ...

  5. 关于datetime,date,timestamp,year,time时间类型小结

    关于datetime,date,timestamp,year,time时间类型 datetime占用8个字节 日期范围:”1000-01-01 00:00:00” 到”9999-12-31 23:59 ...

  6. X230 安装 EI Capitan 10.11.5 总结

    /*     写这个文章的目的主要是为了帮助我自己理清思路,如果能顺便帮助到您.even better   */ 在动手之前大致浏览了 远景论坛(国内第一黑苹果社区)置顶帖的全部内容 [新人请看]远景 ...

  7. POJ3233:Matrix Power Series

    对n<=30(其实可以100)大小的矩阵A求A^1+A^2+……+A^K,K<=1e9,A中的数%m. 从K的二进制位入手.K分解二进制,比如10110,令F[i]=A^1+A^2+……+ ...

  8. pcre7.0在vc6.0编译

    (0)从http://gnuwin32.sourceforge.net/packages/pcre.htm  (pcre windows)下下载最新的windows平台源代码pcre-7.0-src. ...

  9. EF关联

    public CustomerMap() { this.ToTable("Customer"); this.HasKey(c => c.Id); this.Property( ...

  10. openstack DVR的AIO 问题

    问题描述 : 创建public 网络,创建路由器,并且把路由器的gateway 设置指向网络后有下面几种错误 路由器对应的linux network namespace 建立起来了,但是里面并没有对应 ...