您的位置:首页 > 博客中心 > 互联网 >

H - Minimum Cost

时间:2022-04-27 19:03

 

#include
#include
#include
#include
#include
using namespace std;

const int maxn=1024;
const int INF=0x3f3f3f3f;

struct Edge{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};

struct MCMF{
    int n,m;
    vectoredges;
    vectorG[maxn];
    int inq[maxn];//是否在队列中
    int d[maxn];//Bellman-Ford
    int p[maxn];//上一条弧
    int a[maxn];//可改进量

    void init(int n){
        this->n=n;
        for(int i=0;iQ;
        Q.push(s);
        while(!Q.empty()){
            int u=Q.front();Q.pop();
            inq[u]=0;
            for(int i=0;ie.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){Q.push(e.to);inq[e.to]=1;}
                }
            }
        }
        if(d[t]==INF)return false;
        flow+=a[t];
        cost+=(long long)d[t]*(long long)a[t];
        for(int u=t;u!=s;u=edges[p[u]].from){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
        }
        return true;
    }

    //需要保证初始网络中没有负权圈
    int MincostMaxflow(int s,int t,long long &cost){
        int flow=0;cost=0;
        while(BellmanFord(s,t,flow,cost));
        return flow;
    }
}MM;

int main(){
    int N,M,K;//汇点个数,源点个数,物品种类
    int need[64][64];//need[i][j]表示第i个店主需要第j种商品的数量
    int totalneed[64];//
    int storage[64][64];//storage[i][j]表示第i个仓库里存储的第j种商品的数量
    int totalstorage[64];//
    int cost[64][64];//cost[i][j]表示当前这个商品从第j个仓库运送到第i个店主的单位价格
    bool flag;
    int mincost;
    int maxflow;
    long long cost;

    while(~scanf("%d%d%d",&N,&M,&K)){
        if(N==0&&M==0&&K==0)break;
        memset(totalneed,0,sizeof(totalneed));
        memset(totalstorage,0,sizeof(totalstorage));
        flag=true;
        mincost=0;

        for(int i=0;itotalstorage[i]){
                flag=false;
                break;
            }
        }

        for(int k=0;k

 

本类排行

今日推荐

热门手游