POJ2516--Minimum Cost

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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1102
#define edge maxn*40
const int inf = 0x3f3f3f3f;
int first[maxn];
int vv[edge],ww[edge],nxt[edge],cst[edge];
int e;
int pre[maxn], pos[maxn];
int dis[maxn], que[maxn*10];
bool vis[maxn];
inline int min(int a,int b)
{
	return a>b?b:a;
}
void addEdge(int u, int v, int w, int cost)
{
	vv[e] = v;
	ww[e] = w;
    cst[e] = cost;
	nxt[e] = first[u];
	first[u] = e++;
	vv[e] = u;
	ww[e] = 0;
    cst[e] = -cost;
	nxt[e] = first[v];
	first[v] = e++;
}

bool spfa(int s, int t)
{
	int i;
    memset(pre, -1, sizeof(pre));
	memset(vis, 0, sizeof(vis));
    int head, tail; 
	head = tail = 0;
    for(i = 0; i < maxn; i++)
		dis[i] = inf;
    que[tail++] = s;////源点入队
	pre[s] = s;///源点的前缀是源点本身
	dis[s] = 0;
	vis[s] = 1;
    while(head != tail)
    {
        int u = que[head++];
		vis[u] = 0;//出队了??
		for(i = first[u]; i != -1; i = nxt[i])
		{
			int v = vv[i];
			if(ww[i] > 0 && dis[u] + cst[i] < dis[v])
			{
				dis[v] = dis[u] + cst[i];
				pre[v] = u;
				pos[v] = i;////这个我真心不知道用来干嘛的。。。
				if(!vis[v])////原来我写的spfa一直不是最优化的
				{
					vis[v] = 1;
					que[tail++] = v;
				}
			}
		}
    }
    return pre[t] != -1;
}

int MinCostFlow(int s, int t, int flow)////s到t的最小费用最大流。
{
	int i;
    int cost = 0;
    int nflow = 0;
    while(spfa(s, t))
    {
        int f = inf;
        for(i = t; i != s; i = pre[i])
            if (ww[pos[i]] < f) f = ww[pos[i]];
		f = min(flow - nflow,f);
        nflow += f; cost += dis[t] * f;
        for(i = t; i != s; i = pre[i])
        {
            ww[pos[i]] -= f;
            ww[pos[i] ^ 1] += f;
        }
		if(nflow == flow)break;
    }
	if(nflow == flow)
    return cost; // flow是最大流值
	return -1;
}
struct DZ
{
	int ned[58];
}dz[58];
struct SR
{
	int had[58];
}sr[58];
int main()
{
	//freopen("in.txt","r",stdin);
	int n,m,k;
	while(scanf("%d%d%d",&n,&m,&k)==3 &&(n||m||k))
	{
		//接下来就是N行,每行代表这个店主需要对应品种的数量,n个店主
		//每个店主都要满足要求,否则就不行。
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=k;j++)
			{
				scanf("%d",&dz[i].ned[j]);
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=k;j++)
			{
				scanf("%d",&sr[i].had[j]);
			}
		}
		//然后是K个矩阵,矩阵是用来存边的
		//我觉得应该一种一种物品来。
		bool flag = true;//用来判断能否实现目的
		int Ans = 0;
		for(int i=1;i<=k;i++)///枚举物品种
		{
			memset(first,-1,sizeof(first));
			e = 0;
			for(int j=1;j<=n;j++)
			{
				for(int o=1;o<=m;o++)
				{
					int cost;
					scanf("%d",&cost);
					addEdge(o,j+m,inf,cost);//各个要买东西的店主已经连好边了,接下来要连超级源点
				}
			}
			for(int j=1;j<=m;j++)
			{
				if(sr[j].had[i])
				{
					addEdge(0,j,sr[j].had[i],0);
				}
			}///////超级源点连好了
			////接下来要连超级汇点
			int sum = 0;
			for(int j=1;j<=n;j++)
			{
				if(dz[j].ned[i])
				{
					sum += dz[j].ned[i];
					addEdge(j+m,m+n+1,dz[j].ned[i],0);
				}
			}
			int ans = MinCostFlow(0,m+n+1,sum);
			if(ans == -1) flag = false;
			else Ans += ans;
		}
		if(flag) printf("%d\n",Ans);
		else printf("-1\n");
	}
	return 0;
}
					


### 关于 `pip` 安装过程中由 `urllib3` 引发的异常错误解决方案 在使用 `pip` 进行包管理可能会遇到由于网络连接超时或其他原因导致的 `urllib3` 异常。以下是针对此类问题的具体分析和解决方法。 #### 1. 超时错误 (`ReadTimeoutError`) 当执行命令如 `pip3 install jupyterlab` 或其他依赖下载操作,如果目标服务器响应时间过长,则可能出现如下错误: ``` pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. ``` 此错误表明客户端未能及接收到来自远程主机的数据流[^1]。可以尝试通过增加超时参数来缓解该问题: ```bash pip --default-timeout=100 install <package_name> ``` 这里设置了一个更大的默认超时(单位为秒),从而允许更长时间等待服务端响应[^4]。 #### 2. 清理旧版本并重新安装 `pip` 有本地环境中残留损坏或不兼容的组件也可能引起类似的异常行为。按照以下步骤清理环境后再试可能有效果: - 删除已有的站点包目录下的所有文件: ```bash rm -rf ~/.local/lib/python<version>/site-packages/* ``` - 下载官方脚本重新部署最新版 `pip` : ```bash curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3 - type pip3 hash -r pip3 ``` 这一步骤能够确保使用的工具链是最新的状态,减少因程序本身缺陷造成的冲突风险[^2]. #### 3. 使用镜像源加速获取资源 考虑到国外某些网站访问速度较慢甚至不可达的情况,在国内推荐切换至阿里云、清华大学开源软件镜像站等提供更快捷稳定的服务地址作为替代方案之一。修改配置方式如下所示: 编辑或者创建名为 `.pip/pip.conf`(Linux/Mac OS X) 的全局配置文档加入下面内容即可生效: ```ini [global] index-url = https://mirrors.aliyun.com/pypi/simple/ trusted-host = mirrors.aliyun.com ``` 对于 Windows 用户来说路径应改为 `%APPDATA%\Python\pip\pip.ini`. 另外还有临指定的方法适用于单次调用场景下无需永久更改设定的情形 : ```bash pip install some-package -i http://pypi.douban.com/simple --trusted-host pypi.douban.com ``` 以上措施均有助于改善因地理因素带来的延迟现象进而规避潜在隐患[^4]. #### 4. 更新或替换底层库 `urllib3` 既然问题是围绕着 `urllib3`, 那么单独升级它也是一个得考虑的方向 。运行下列指令完成更新过程: ```bash pip install --upgrade urllib3 ``` 当然也可以手动卸载再重装一遍确认效果如何变化 : ```bash pip uninstall urllib3 && pip install urllib3 ``` 得注意的是 , 如果项目里头绑定了特定版本号的话记得查阅对应说明文档调整策略适配需求[^3]. --- ### 总结 综上所述,面对 `pip` 和其内部实现所依托的 `urllib3` 出现的各种异常状况可以从多个角度切入排查处理。无论是优化网络条件还是修正自身软硬件设施都不可或缺。希望上述建议能帮助到您解决问题!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值