Minimum-cost flow problem
[R + lpSolve], 201 186 149 144 bytes



function(c,a,d,`^`=rep,N=ncol(c),G=diag(N),P=t(1^N),M=P%x%G+G%x%-P)lpSolve::lp(,a,rbind(M,diag(N*N)),c('=','&lt;')^c(N,N*N),c(d,0^(N-2),-d,c))$objv


Try it online!

The code constructs the following Linear Problem and solve it using lpSolve package :

$$
\begin{align}
      min &amp; \sum_{\forall x \in V}\  \sum_{\forall y \in V} A_{x,y} \cdot f_{x,y} \\
subject\ to : &amp; \\
          &amp; \sum_{x \in V} f_{v,x} - f_{x,v} = 0 &amp; \forall v \in V : v \neq \{s,t\} \\
          &amp; \sum_{x \in V} f_{s,x} - f_{x,s} = d \\
          &amp; \sum_{x \in V} f_{t,x} - f_{x,t} = -d \\
          &amp; f_{x,b} \leq C_{x,b}                 &amp; \forall x \in V,\forall y \in V
\end{align}
$$
where :  


\$V\$ is the set of vertices
\$s\$ s is the source vertex
\$t\$ s is the target (or sink) vertex
\$A_{x,y}\$ is the flow cost for edge x -&gt; y
\$f_{x,y}\$ is the flow of edge x -&gt; y in the optimal solution
\$d\$ is the required flow at sink (i.e. the demand at \$t\$ and the production at \$s\$)
\$C_{x,y}\$ is the maximum capacity of edge x -&gt; y