POJ训练计划2516_Minimum Cost(网络流/费用流)

解题报告

题意:
有n个商店,m个提供商,k种商品</span>
n*k的矩阵,表示每个商店需要每个商品的数目;
m*k矩阵,表示每个提供商拥有每个商品的个数
然后对于每个物品k,都有n*m的矩阵
i行j列表示
从j提供商向i商店运送一个k商品的代价是多少
判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用
思路:
建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用,
不过这样其实不行的,会发现n跟k的关系没办法解决
如果把k种商品分k次处理就行了,相当于一次运一种
对于每一种商品,建一次图,求最小费用,如果最大流不能满足顾客的需求就pass。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;
struct E {
    int v,cost,cap,next;
} edge[100000];
int head[5000],dis[5000],pre[5000],vis[5000],f[5000],m1[55][55],m2[55][55],s,t,n,m,k,cnt,cost,flow;
void add(int u,int v,int cost,int cap) {
    edge[cnt].v=v;
    edge[cnt].cost=cost;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cost=-cost;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int _spfa() {
    for(int i=s; i<=t; i++) {
        dis[i]=inf,vis[i]=pre[i]=f[i]=0;
    }
    dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf;
    queue<int>Q;
    Q.push(s);
    while(!Q.empty()) {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) {
                pre[v]=i;
                dis[v]=dis[u]+edge[i].cost;
                f[v]=min(f[u],edge[i].cap);
                if(!vis[v]) {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(dis[t]==inf)return 0;
    flow+=f[t];
    cost+=f[t]*dis[t];
    for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) {
        edge[i].cap-=f[t];
        edge[i^1].cap+=f[t];
    }
    return 1;
}
void mcmf() {
    cost=flow=0;
    while(_spfa());
}
int main() {
    int i,j,l,a,b,c;
    while(~scanf("%d%d%d",&n,&m,&k)) {
        int sum[1000];
        s=0,t=m+n+1;
        if(!n&&!m&&!k)break;
        memset(sum,0,sizeof(sum));
        memset(m1,0,sizeof(m1));
        memset(m2,0,sizeof(m2));
        for(i=1; i<=n; i++) {
            for(j=1; j<=k; j++) {
                scanf("%d",&m1[i][j]);
                sum[j]+=m1[i][j];
            }
        }
        for(i=1; i<=m; i++) {
            for(j=1; j<=k; j++)
                scanf("%d",&m2[i][j]);
        }
        int f=0;
        int ans=0;
        for(l=1; l<=k; l++) {
            cnt=0;
            memset(head,-1,sizeof(head));
            memset(edge,0,sizeof(edge));
            for(i=1; i<=n; i++) {
                for(j=1; j<=m; j++) {
                    scanf("%d",&a);
                    add(j,m+i,a,inf);
                }
            }
            if(f)continue;
            for(i=1; i<=m; i++)
                add(s,i,0,m2[i][l]);
            for(i=1; i<=n; i++)
                add(m+i,t,0,m1[i][l]);
            mcmf();
            if(flow<sum[l]) {
                f=1;
            } else ans+=cost;
        }
        if(f)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
}

Minimum Cost

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 13529   Accepted: 4633

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods
(marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport. It‘s known that the cost to transport one unit goods for different kinds
from different supply places to different shopkeepers may be different. Given each supply places‘ storage of K kinds of goods, N shopkeepers‘ order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different
shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, K (0 < N, M, K < 50), which are described above. The next N lines give the shopkeepers‘ orders, with each line containing K integers (there integers are
belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places‘ storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in
that supply place. Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place
to the i-th shopkeeper. The input is terminated with three "0"s. This test case should not be processed.

Output

For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".

Sample Input

1 3 3
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0

Sample Output

4
-1

POJ训练计划2516_Minimum Cost(网络流/费用流),布布扣,bubuko.com

时间: 2024-10-07 18:02:11

POJ训练计划2516_Minimum Cost(网络流/费用流)的相关文章

POJ训练计划2195_Going Home(网络流/费用流)

解题报告 题目传送门 思路: bfs建图跑一下费用流就行. #include <iostream> #include <cstdio> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; struct E { int v,cost,cap,next; } edge[100000]; int head[1000],cnt,dis[1000],pre[10

POJ训练计划1459_Power Network(网络流最大流/Dinic)

解题报告 这题建模实在是好建,,,好贱,,, 给前向星给跪了,纯dinic的前向星竟然TLE,sad,,,回头看看优化,,, 矩阵跑过了,2A,sad,,, /************************************************************************* > File Name: PowerN.cpp > Author: _nplus > Mail: [email protected] > Time: 2014年07月19日 星期

POJ 2516 Minimum Cost 【费用流】

建模比较难想.. #include<iostream> #include<deque> #include<vector> #include<cstring> #include<cmath> #define INF 2e9 using namespace std; int T,ans; struct edge{ int v,cap,reverse,cost; }; vector<int> edges[1005];//邻接表 vector

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ 2516 Minimum Cost(网络流之费用流)

题目地址:POJ 2516 我晕啊...这题一上来就想到了对每种货物分开求..但是马上就放弃了..感觉这样求50次费用流太耗时..后来就果断拆点,拆了好长时间,一直TLE..即使降到了2600个点也TLE..然后又想起了这个分开求的方法,又突然觉得100个点的费用流几乎不费什么时间..最多也只是求50次而已,还是可以试试的..于是一试居然还真过了... 说到这里,思路应该已经知道了吧.就是对每种货物分开求,因为每种货物是相互独立的.每一次的建图思路就是: 源点与供应商连边,流量权值为供应商这种货

POJ 2516 Minimum Cost(最小费用最大流,坑题)

题目链接:http://poj.org/problem?id=2516 题意:有N个店,M个供货商,K种商品.已知供货商的仓库里每种商品的数量以及每种商品运送到每个店的费用,每个店铺对各种商品的需求数量,求最少话费. Input  第一行:N,M,K. 然后1 - N行,每行 K列 ,第I行第J个数代表 第I个店铺 需要第J种物品多少件. 然后 N+1 - M行  ,每行 K列 , 第I行第J个数代表 第I个供货商 有第J种物品多少件. 然后是K个矩阵  ,每个N行M列,第ji个矩阵的第i行第j

POJ 2195 Going Home(网络流-费用流)

Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17777   Accepted: 9059 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertical

POJ 3422 HDU 2686,3376 费用流拆点建图

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376 http://acm.hdu.edu.cn/showproblem.php?pid=2686 http://poj.org/problem?id=3422 POJ 3422为从矩阵左上角走到右下角,最多走k次,每个格子里的数字只能算一次,后面可以重复经过,求经过的各个数字的和的最大值. 拆点,入点向出点连流量为1,费用为当前格子负值的边,向下方,右方连边,流量为k,费用为0,起点连流量为1,

POJ2135_Farm Tour(网络流/费用流)

解题报告 题目传送门 题意: 一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路. 思路: 如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了. 一次mcmf模版. #include <iostream> #include <cstring> #include <queue> #include <cstdio> #define inf 0x3f3f3f3f using namespace st