Logu P4015 transportation problems, logu p4015 Transportation

Source: Internet
Author: User

Logu P4015 transportation problems, logu p4015 Transportation
Description

WW has mm warehouses and nn retail stores. The second warehouse contains a_iai units of goods; the jj retail store requires B _jbj units of goods.

Goods supply and demand balance, that is, \ sum \ limits _ {I = 1} ^ {m} a_ I = \ sum \ limits _ {j = 1} ^ {n} B _ji = 1 Σ mai = j = 1 sigma nbj.

C _ {ij} cij is charged for shipping per unit of goods from Warehouse ii to jj retail store.

Design a transportation plan to transport all the goods in the warehouse to the retail store to minimize the total transportation cost.

Input/Output Format

Input Format:

 

There are 22 positive integers mm and nn in row 11th, indicating the number of warehouses and the number of retail stores respectively.

The following row contains mm positive integers a_iai, indicating that the second warehouse contains a_iai goods.

In the next row, nn is a positive integer B _jbj, indicating that the first jj retail store needs goods in B _jbj units.

In the next line of mm, each line has an nn integer, which indicates the cost of c _ {ij} cij for transporting the goods per unit to the jj retail store from the second warehouse.

 

Output Format:

 

The two lines output the minimum transportation fee and the maximum transportation fee respectively.

 

Input and Output sample input sample #1: Copy
2 3220 280170 120 21077 39 105150 186 122
Output example #1: Copy
4850069140
Description

1 \ leq n, m \ leq 1001 ≤ n, m ≤ 100

 

A bare Charge flow

The link capacity from S to warehouse is a, and the cost is 0

From the store to the T-connected capacity of B, the cost is 0 side

Connecting capacity from warehouse to store is INF, and the cost is the side of c

 

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>#define AddEdge(x,y,z,f) add_edge(x,y,z,f),add_edge(y,x,-z,0)using namespace std;const int INF=1e8+10;const int MAXN=1e4+10;inline int read(){    char c=getchar();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}    return x*f;}int N,M,S,T;int ansflow,anscost;int A[MAXN],B[MAXN],C[1001][1001];struct node{    int u,v,w,f,nxt;}edge[MAXN];int head[MAXN],num=2;inline void add_edge(int x,int y,int z,int f){    edge[num].u=x;    edge[num].v=y;    edge[num].w=z;    edge[num].f=f;    edge[num].nxt=head[x];    head[x]=num++;}int dis[MAXN],vis[MAXN],Pre[MAXN];int SPFA(){    queue<int>q;    q.push(S);    memset(dis,0x3f,sizeof(dis));    memset(vis,0,sizeof(vis));    dis[S]=0;    while(q.size()!=0)    {        int p=q.front();q.pop();        vis[p]=0;        for(int i=head[p];i!=-1;i=edge[i].nxt)        {            if(edge[i].f>0&&dis[edge[i].v]>dis[p]+edge[i].w)            {                dis[edge[i].v]=dis[p]+edge[i].w;                Pre[edge[i].v]=i;                if(!vis[edge[i].v])                    q.push(edge[i].v),vis[edge[i].v]=1;            }        }    }    return dis[T]<=INF;}int F(){    int nowflow=INF;    for(int now=T;now!=S;now=edge[Pre[now]].u)        nowflow=min(nowflow,edge[Pre[now]].f);    for(int now=T;now!=S;now=edge[Pre[now]].u)        edge[Pre[now]].f-=nowflow,        edge[Pre[now]^1].f+=nowflow;    anscost+=nowflow*dis[T];}void MCMF(){    while(SPFA()) F();    printf("%d\n",abs(anscost));    anscost=0;}int main(){    #ifdef WIN32    freopen("a.in","r",stdin);    #endif    memset(head,-1,sizeof(head));    N=read(),M=read();    S=N+M+1;T=N+M+2;    for(int i=1;i<=N;i++)        A[i]=read();    for(int i=1;i<=M;i++)        B[i]=read();    for(int i=1;i<=N;i++)        for(int j=1;j<=M;j++)            C[i][j]=read();                for(int i=1;i<=N;i++)        AddEdge(S,i,0,A[i]);            for(int i=1;i<=M;i++)            AddEdge(i+N,T,0,B[i]);    for(int i=1;i<=N;i++)        for(int j=1;j<=M;j++)            AddEdge(i,j+N,C[i][j],INF);    MCMF();    memset(head,-1,sizeof(head));    num=2;    for(int i=1;i<=N;i++)        AddEdge(S,i,0,A[i]);    for(int i=1;i<=M;i++)            AddEdge(i+N,T,0,B[i]);    for(int i=1;i<=N;i++)        for(int j=1;j<=M;j++)            AddEdge(i,j+N,-C[i][j],INF);    MCMF();    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.