#38206: C++詳解


11131039@stu.tshs.tp.edu.tw (二孝25林孟希)

學校 : 不指定學校
編號 : 201083
來源 : [125.228.248.38]
最後登入時間 :
2024-04-17 14:50:03
f606. 2. 流量 -- 2021年1月APCS | From: [125.228.248.38] | 發表日期 : 2023-11-03 14:03

#include <bits/stdc++.h>

using namespace std;

int citycost(int u,int f,int v)//計算費用函式(題意)
{
    if(u==v)
    {
        return f;
    }
    else
    {
        if(f<=1000)
        {
            return f*3;
        }
        else if(f>1000)
        {
            return 3000+(f-1000)*2;
        }
    }
}

int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    int q[n][m];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>q[i][j];
        }
    }
    int c[k][n];
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>c[i][j];
        }
    }
    int cost[k]={0},citytotalcost[m][m]={0};//cost:紀錄每個方案費用,citytotalcost紀錄該方案中每個城市要傳的總流量
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<m;j++)
        {
            for(int r=0;r<m;r++)
            {
                citytotalcost[j][r]=0;//先設為0
            }
        }
        for(int j=0;j<m;j++)//第j個城市
        {
            for(int r=0;r<m;r++)//要傳到r的總流量
            {
                for(int a=0;a<n;a++)//遍歷伺服器編號
                {
                    for(int b=0;b<m;b++)//尋找q中要傳到b==r的流量
                    {
                        if(c[i][a]==j && b==r)//該編號為a基地台位在c[i][a]==j
                        {
                            citytotalcost[j][r]+=q[a][b];//編號為a要傳到b==r
                        }
                    }
                }
            }
        }
        /*for(int j=0;j<m;j++)
        {
            for(int r=0;r<m;r++)
            {
                cout<<citytotalcost[j][r]<<" ";
            }
            cout<<endl;
        }
        */
        for(int j=0;j<m;j++)
        {
            for(int r=0;r<m;r++)
            {
                cost[i]+=citycost(j,citytotalcost[j][r],r);//加總所有費用
            }
        }
    }
    sort(cost,cost+k);
    /*for(int i=0;i<k;i++)
    {
        cout<<cost[i]<<" ";
    }
    */
    cout<<cost[0];
    return 0;
}

 
ZeroJudge Forum