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;
}


本系统采用Python编程语言中的Flask框架作为基础架构,实现了一个面向二手商品交易的网络平台。该平台具备完整的前端展示与后端管理功能,适合用作学术研究、课程作业或个人技术能力训练的实际案例。Flask作为一种简洁高效的Web开发框架,能够以模块化方式支持网站功能的快速搭建。在本系统中,Flask承担了核心服务端的角色,主要完成请求响应处理、数据运算及业务流程控制等任务。 开发工具选用PyCharm集成环境。这款由JetBrains推出的Python专用编辑器集成了智能代码提示、错误检测、程序调试与自动化测试等多种辅助功能,显著提升了软件编写与维护的效率。通过该环境,开发者可便捷地进行项目组织与问题排查。 数据存储部分采用MySQL关系型数据库管理系统,用于保存会员资料、产品信息及订单历史等内容。MySQL具备良好的稳定性和处理性能,常被各类网络服务所采用。在Flask体系内,一般会配合SQLAlchemy这一对象关系映射工具使用,使得开发者能够通过Python类对象直接管理数据实体,避免手动编写结构化查询语句。 缓存服务由Redis内存数据库提供支持。Redis是一种支持持久化存储的开放源代码内存键值存储系统,可作为高速缓存、临时数据库或消息代理使用。在本系统中,Redis可能用于暂存高频访问的商品内容、用户登录状态等动态信息,从而加快数据获取速度,降低主数据库的查询负载。 项目归档文件“Python_Flask_ershou-master”预计包含以下关键组成部分: 1. 应用主程序(app.py):包含Flask应用初始化代码及请求路径映射规则。 2. 数据模型定义(models.py):通过SQLAlchemy声明与数据库表对应的类结构。 3. 视图控制器(views.py):包含处理各类网络请求并生成回复的业务函数,涵盖账户管理、商品展示、订单处理等操作。 4. 页面模板目录(templates):存储用于动态生成网页的HTML模板文件。 5. 静态资源目录(static):存放层叠样式表、客户端脚本及图像等固定资源。 6. 依赖清单(requirements.txt):记录项目运行所需的所有第三方Python库及其版本号,便于环境重建。 7. 参数配置(config.py):集中设置数据库连接参数、缓存服务器地址等运行配置。 此外,项目还可能包含自动化测试用例、数据库结构迁移工具以及运行部署相关文档。通过构建此系统,开发者能够系统掌握Flask框架的实际运用,理解用户身份验证、访问控制、数据持久化、界面动态生成等网络应用关键技术,同时熟悉MySQL数据库运维与Redis缓存机制的应用方法。对于入门阶段的学习者而言,该系统可作为综合性的实践训练载体,有效促进Python网络编程技能的提升。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
在当代储能装置监控技术领域,精确测定锂离子电池的电荷存量(即荷电状态,SOC)是一项关键任务,它直接关系到电池运行的安全性、耐久性及整体效能。随着电动车辆产业的迅速扩张,业界对锂离子电池SOC测算的精确度与稳定性提出了更为严格的标准。为此,构建一套能够在多样化运行场景及温度条件下实现高精度SOC测算的技术方案具有显著的实际意义。 本文介绍一种结合Transformer架构与容积卡尔曼滤波(CKF)的混合式SOC测算系统。Transformer架构最初在语言处理领域获得突破性进展,其特有的注意力机制能够有效捕捉时间序列数据中的长期关联特征。在本应用中,该架构用于分析电池工作过程中采集的电压、电流与温度等时序数据,从而识别电池在不同放电区间的动态行为规律。 容积卡尔曼滤波作为一种适用于非线性系统的状态估计算法,在本系统中负责对Transformer提取的特征数据进行递归融合与实时推算,以持续更新电池的SOC值。该方法增强了系统在测量噪声干扰下的稳定性,确保了测算结果在不同环境条件下的可靠性。 本系统在多种标准驾驶循环(如BJDST、DST、FUDS、US06)及不同环境温度(0°C、25°C、45°C)下进行了验证测试,这些条件涵盖了电动车辆在实际使用中可能遇到的主要工况与气候范围。实验表明,该系统在低温、常温及高温环境中,面对差异化的负载变化,均能保持较高的测算准确性。 随附文档中提供了该系统的补充说明、实验数据及技术细节,核心代码与模型文件亦包含于对应目录中,可供进一步研究或工程部署使用。该融合架构不仅在方法层面具有创新性,同时展现了良好的工程适用性与测算精度,对推进电池管理技术的进步具有积极意义。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值