Goods Transportation(最大运输量及最小成本)
题目
The transportation problem is to minimize the cost of transporting good from m source nodes to n destination nodes through arcs. Arcs are directed routes from source to destination which have no capacity limitation, but there is a cost associated with each arc for each unit of goods transported through. Each source has a supply amount, and each destination has a demand amount.
In this problem, the source nodes are numbered from 1 to m, and the destination nodes are numbered from 1 to n. There is one arc between each pair of the source node and the destination node. The cost of the arc originated from source node a (1 <= a <= m) to destination node b (1 <= b <= n) is a + b. The supply for each source nodes and the demand for each destination node are also given. You are going to calculate the maximal possible amount of goods that can be transported from source to destination, and also the minimized cost to achieve that goal.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.
For each case, the numbers of source and destination nodes, m (1 <= m <= 10,000) and n (1 <= n <= 10,000), are given in the first line. The next line contains m integers, which are the supply amounts for each source, and the next line n integers, which are the demand amounts of each destination. All supply and demand amounts are non-negative numbers less than or equal to 10,000.
Output
Results should be directed to standard output. Start each case with “Case #:” on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
For each test case print in one line the two integers - the maximal amount of goods that can be transported to destination, and the minimized cost for transporting those goods. Separate them with a single space.
Sample Input
1
3 4
2 5 6
4 3 1 5
Sample Output
Case 1:
13 63
从n个源节点运送货物到m个目的节点,线路的运输量没有限制,运输成本为源节点与目标节点的编号之和。第一行输入为用例数,每个用例的第一行为源节点个数和目的节点个数,第二行为源节点的供应量,第三行为目标节点的需求量。输出最大的运输量及最小的成本。
思路
最大运输量即为各源节点供应量之和与目的节点需求量之和的最小值。最小运输成本采用下面的方法来求。
我们先看最简单的一种情况,类似下面这个图,源节点编号为i,供应量为5,目的节点编号的j,j+1,需求量分别为2、3。
则最大的运输量为5,成本为2(i+j)+3(i+j+1) = 5i+2j+3(j+i)
也就是运输的成本可以拆分为源节点与其编号的积和目的节点与其编号的积的和。
因此我们可以先计算出所有源节点与其编号之积(供应成本)再加上目的节点与其编号之积(需求成本),若供应量有富余,则减去相应的供应成本;若需求量有富余,则减去相应的需求成本。
代码
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int t,m,n; //分别表示用例数、源节点数、目的节点数
int sdsum,ssum,dsum; //分别表示总运输成本、供应成本、需求成本
int s[10005],d[10005];
scanf("%d",&t);
for(int j=1;j<=t;j++)
{
scanf("%d%d",&m,&n);
sdsum = 0;
ssum = 0;
dsum = 0;
for(int i=1;i<=m;i++)
{
scanf("%d",&s[i]);
ssum += s[i];
sdsum += s[i]*i;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&d[i]);
dsum += d[i];
sdsum += d[i]*i;
}
int df = ssum-dsum;
if(df>0)
{
int tmp = m;
while(df>s[tmp])
{
sdsum -= s[tmp]*tmp;
df -= tmp;
tmp--;
}
sdsum -= df*tmp;
df = 0;
}
if(df<0)
{
int tmp = n;
while(-df>d[tmp])
{
sdsum -= d[tmp]*tmp;
df += tmp;
tmp--;
}
sdsum += df*tmp;
df = 0;
}
printf("Case%d:\n%d %d\n",j,min(ssum,dsum),sdsum);
if(j<t)
{
printf("\n");
}
}
return 0;
}
该问题描述了一个运输问题,旨在以最小成本从m个源节点向n个目标节点运输商品,其中成本是节点编号之和。输入包含多个测试用例,每个用例给出节点数量、供应量和需求量。输出最大运输量和最小成本。解决方案涉及计算源和目标节点编号的乘积以确定成本。
&spm=1001.2101.3001.5002&articleId=103541436&d=1&t=3&u=8fe77d3e3ceb4df7a737fe404a59135c)
1499

被折叠的 条评论
为什么被折叠?



