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

Minimum Cost
Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 11859Accepted: 3993

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
题目输入很烦,需要耐心。只要能想到把每件物品分开来处理就好办了,构图时源点连供应商,流量为供给量,费用为0;店主与汇点连边,流量为需求量,费用为0;再根据每个供应商供给给不同店主的运输费用各自连边,流量为无穷,费用为输入给出的费用。题目可能算是给了点暗示吧,因为对于每件物品都有一个N*M的矩阵表示运输费用,注意在N*M矩阵中,i 行 j 列表示的是第 j 个供应商给第 i 和店主供货时的运输费用,这就是考验细心的了。题目还要求如果不能满足需求,则输出-1。所以提前统计一下每种物品的总需求量和总供给量,如果某物品的 需求量>供给量 则标记一下,结果输出-1.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define SIZE 3000
#define inf 0xfffffff

using namespace std;

struct node
{
    int to,val,cost,next;
}edge[SIZE*668];

int N,M,K,sc,sk,pt,sum;
int head[SIZE],idx;
int offer[64][64],need[64][64],fare[64][64][64];
int demand[64],supply[64];
int dis[SIZE],pre[SIZE],pos[SIZE];
bool vis[SIZE];

void addnode(int from,int to,int val,int cost)
{
    edge[idx].to = to;
    edge[idx].val = val;
    edge[idx].cost = cost;
    edge[idx].next = head[from];
    head[from] = idx++;
    edge[idx].to = from;
    edge[idx].val = 0;
    edge[idx].cost = -cost;
    edge[idx].next = head[to];
    head[to] = idx++;
}

bool spfa()
{
    queue <int> Q;
    for(int i=0; i<=pt; i++)
    {
        dis[i] = inf;
        vis[i] = false;
    }
    dis[sc] = 0;
    pre[sc] = sc;
    vis[sc] = true;
    Q.push(sc);
    while(!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        vis[cur] = false;
        for(int i=head[cur]; i!=-1; i=edge[i].next)
        {
            int to = edge[i].to;
            if(edge[i].val > 0 && dis[to] > dis[cur] + edge[i].cost)
            {
                dis[to] = dis[cur] + edge[i].cost;
                pre[to] = cur;
                pos[to] = i;
                if(!vis[to])
                {
                    vis[to] = true;
                    Q.push(to);
                }
            }
        }
    }
    if(pre[sk] != -1 && dis[sk] < inf)
        return true;
    return false;
}

int Flow()
{
    int flow = 0,cost = 0;
    while(spfa())
    {
        int Min = inf;
        for(int i=sk; i!=sc; i=pre[i])
            Min = min(Min,edge[pos[i]].val);
        flow += Min;
        cost += Min*dis[sk];
        for(int i=sk; i!=sc; i=pre[i])
        {
            edge[pos[i]].val -= Min;
            edge[pos[i]^1].val += Min;
        }
    }
    return cost;
}

void read()
{
    memset(demand,0,sizeof(demand));
    memset(supply,0,sizeof(supply));
    memset(need,0,sizeof(need));
    memset(offer,0,sizeof(offer));
    memset(fare,0,sizeof(fare));
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=K; j++)
        {
            scanf("%d",&need[i][j]);
            demand[j] += need[i][j];
        }
    }
    for(int i=1; i<=M; i++)
    {
        for(int j=1; j<=K; j++)
        {
            scanf("%d",&offer[i][j]);
            supply[j] += offer[i][j];
        }
    }
    for(int k=1; k<=K; k++)
        for(int i=1; i<=N; i++)
            for(int j=1; j<=M; j++)
                scanf("%d",&fare[j][i][k]);
    bool flag = false;
    for(int i=1; i<=K; i++)
    {
        if(demand[i] > supply[i])
        {
            flag = true;
            break;
        }
    }
    if(flag)
    {
        puts("-1");
        return;
    }
    int ans = 0;
    for(int k=1; k<=K; k++)
    {
        idx = 0;
        memset(head,-1,sizeof(head));
        sc = 0, sk = N+M+1, pt = sk+1;
        for(int i=1; i<=M; i++)
        {
            addnode(sc,i,offer[i][k],0);
            for(int j=1; j<=N; j++)
                addnode(i,M+j,inf,fare[i][j][k]);
        }
        for(int i=1; i<=N; i++)
            addnode(M+i,sk,need[i][k],0);
        ans += Flow();
    }
    printf("%d\n",ans);
}

int main()
{
    while(~scanf("%d%d%d",&N,&M,&K))
    {
        if(!N && !M && !K)
            break;
        read();
    }
    return 0;
}


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值