SUPER BALL

時間限制: 1 Sec  內存限制: 128 MB
提交: 54  解決: 14
[提交] [狀態] [討論版] [命題人:admin]

題目描述

In the distance future, there exist a company that makes ball. Technically they make spherical object regardless of use, but let’s just call it a ball. Their ball consist of many layers wrapped around each other. Maybe the first layer is made of metal, second made of rubber, third made of carbon nanotubes, the forth layer maybe a single-atom-thick graphene. Think of it like an onion. These layers are so advance that each type of layer have their own factory. Logistic becomes a headache because during the production of the ball, the ball needs to be transferred from one factory to another factory in order of their type of layer. For example, a ball's core (the first layer) is made in the metal factory, and then it is transferred to the rubber factory and then it is transferred to the carbon nanotubes factory and then to the graphene factory. 
Fortunately, some factory can make more than one type of layer, so in that case, the ball may not need to be transferred to another factory. Plus, there can be more than one factory that can make  a  particular  type  of  layer,  so  if  its  cheaper  to  use  that  factory,  the  company  can. 
Unfortunately, because this is the future, absolutely everything must be recycle. And because the ball are so advance, each layer of the ball can only be recycled by the factory that can make that layer. 
Because this is a company that wants to make money, they need to know how much a ball will cost and they want to minimize it. That includes the cost of making a layer, the cost of recycling a layer and the cost of transferring the ball from one factory to another. Thankfully, delivery to and from the client is handled by another company and they pick up the ball from any factory to the client and send it back for recycling to any factory for free. As you can imagine, when the manager receive an order of a ball made of 101 exotic layer, some of his brain cell commit suicide. So he turns to you to make his life easier. Given a description of the factories, and a description of a type of ball calculate the minimum cost to produce the ball. 

 

輸入

The  first  line  consist  of  two  number F (1 ≤ F ≤ 500) which  is  the  number  of  factory and L (1 ≤ L ≤ 500) which is the number of type of layer. Each factory is represented by an integer from 1 to F and each layer type is represented by an integer from 1 to L. 
The next 3F line represent the factory information, each factory is described with three line. 
The first line consist of F integers, C i (0 ≤ Ci  ≤ 103 ) where Ci  is the cost of transferring a ball from the current factory to the i'th factory. 
The second line consist of L integer, D i ( - 1 ≤ Di  ≤ 103 ) where Di  is the cost of producing the layer type i. 
The third line consist of L integer, R i ( - 1 ≤ Ri  ≤ 103 ) where R i  is the cost of recycling the layer type i. 
If Di  or Ri  is -1, that means the factory cannot produce or recycle the layer type. 
The next line represent the ball layer configuration. 
The line start with an integer N(1 ≤ N ≤ 500) which is the number of layer in the ball. The next N integer, Gi (1 ≤ Gi  ≤ L) represent the type of layer of the ball. 

 

輸出

Output a single integer R which is the minimum cost of producing the ball. 

 

樣例輸入

3 3
0 10 15
99 -1 -1
10 -1 -1
10 0 5
-1 10 10
-1 5 5
15 5 0
-1 1 -1
-1 20 -1
2 3 2

 

樣例輸出

26

 

提示

In the first example, the ball configuration is 3, 2. The cheapest way to make and recycle the ball is from factory 3, costing 1 for first layer, then, factory 2, with transfer cost of 5 and produce costing of 10 for the second layer. The for recycling, the second layer is recycled starting at factory 2, costing 5 for second layer and 5 for the first layer, again at the same factory (so no transfer cost).h  

題目大意:一個球有許多層,每一層需要一種材質,有一些工廠,工廠之間運送有花費,生產材料有花費,回收有花費,先按球層的順序一層層的生產材料,生產完後可以免費瞬移到任意一個工廠,再按反向的順序一層一層的回收材料。

#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int mapp[505][5][505];
int dp[505][505];
int z[505];
int main()
{
    int f,l;
    scanf("%d%d", &f, &l);
    for (int i = 0; i < f; ++i)
    {
        for (int j = 0; j < f; ++j)
            scanf("%d", &mapp[i][0][j]); //工廠間移動的花費
        for (int j = 0; j < l; ++j)
            scanf("%d", &mapp[i][1][j]); //生產材料的花費
        for (int j = 0; j < l; ++j)
            scanf("%d", &mapp[i][2][j]); //回收材料的花費
    }
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", &z[i]); //球的每一層的材料

    int ans = 0;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < f; ++j)
            dp[i][j] = inf; //初始化
    for (int i = 0; i < f; ++i)
        if (mapp[i][1][ z[n - 1] - 1] != -1)
            dp[n - 1][i] = mapp[i][1][ z[n - 1] - 1]; 
    for (int i = n - 2; i >= 0; --i)
    {
        for (int j = 0; j < f; ++j)
        {
            if (mapp[j][1][ z[i] - 1] == -1)
                continue;
            for (int k = 0; k < f; ++k)
                dp[i][j] = min(dp[i][j], dp[i + 1][k] + mapp[k][0][j] + mapp[j][1][ z[i] - 1]);
        }
    } //dp[i][j]代表處理到第i層,此時在第j個工廠的最小花費

    int minn = inf;
    for (int i = 0; i < f; ++i)
        minn = min(minn, dp[0][i]);
    ans = minn;
    minn = inf;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < f; ++j)
            dp[i][j] = inf;
    for (int i = 0; i < f; ++i)
        if (mapp[i][1][ z[0] - 1] != -1)
            dp[0][i] = mapp[i][2][ z[0] - 1];
    for (int i = 1; i < n; ++i)
    {
        for (int j = 0; j < f; ++j)
        {
            if (mapp[j][2][ z[i] - 1] == -1)
                continue;
            for (int k = 0; k < f; ++k)
                dp[i][j] = min(dp[i][j], dp[i - 1][k] + mapp[k][0][j] + mapp[j][2][ z[i] - 1]);
        }
    }
    for (int i = 0; i < f; ++i)
        minn = min(minn, dp[n - 1][i]);  //反過來再求一遍回收的最小花費
    ans += minn;
    printf("%d\n", ans);
    return 0;
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章