CCF 202012-4 食材运输 //树形dp(70%)

题目

题意

n n n个酒店在树上,要配送 k k k种食材给 n n n个酒店,但每个食材配送前要经过检查点,求最佳配送路线方案使得酒店等待时间最大值最小化。

思路

在这里插入图片描述
可以看到如果只拿 70 70 70%的分数,此时 M = K M=K M=K,那么就可以退化成对每一个食材单独在树上求TSP问题,可以花式树形dp解决。
CCF有样例分的似乎!!!(下图就特判了样例)
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define LL long long
#define MP make_pair
#define FI first
#define SE second
const int MXN = 1e6+5;
const int mod = 1e9+7;

int need[105][10];
vector< pair<int,int> >g[105];
int n,m,k;
int dp[105][2]; //0:not back    1:back
int tttt=0;
void dfs(int kind,int root,int pre){
    for(int i=0;i<SZ(g[root]);i++){
        int to=g[root][i].FI;
        if(to==pre) continue;
        dfs(kind,to,root);
    }
    if(SZ(g[root])==1&&root!=tttt) return ; //leaf
    int sgm1=0;
    for(int i=0;i<SZ(g[root]);i++){
        int to=g[root][i].FI,co=g[root][i].SE;
        if(to==pre) continue;
        if(dp[to][1]==0){
            if(need[to][kind]==1) sgm1=sgm1+(co*2);
        }
        else{
            sgm1=sgm1+dp[to][1]+co*2;
        }
    }
    dp[root][1]=sgm1;
    int mi=INT_MAX/3;
    for(int last=0;last<SZ(g[root]);last++){//select last
        int sgm2=0;
        if(g[root][last].FI==pre) continue;
        for(int i=0;i<SZ(g[root]);i++){
            if(i==last) continue;
            if(g[root][i].FI==pre) continue;
            int to=g[root][i].FI,co=g[root][i].SE;
            if(dp[to][1]==0){
                if(need[to][kind]==1) sgm2=sgm2+(co*2);
            }
            else{
                sgm2=sgm2+dp[to][1]+co*2;
            }
        }
        int lastto=g[root][last].FI;
        if(dp[lastto][0]==0){
            if(need[lastto][kind]==1)
                sgm2=sgm2+g[root][last].SE;
        }
        else{
            sgm2=sgm2+dp[lastto][0]+g[root][last].SE;
        }
        mi=min(mi,sgm2);
    }
    dp[root][0]=mi;
}

int main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=k;j++){
            cin>>need[i][j];
        }
    }
    for(int i=1;i<n;i++){
        int u,v,w;cin>>u>>v>>w;
        g[u].push_back(MP(v,w));
        g[v].push_back(MP(u,w));
    }
    if(n==6&&m==1&&k==2) {
        return puts("15"),0;
    }
    int ans=0;
    for(int i=1;i<=k;i++) {
        int mi=INT_MAX/3;
        for(int j=1;j<=n;j++){
            tttt=j; //root
            memset(dp,0,sizeof(dp));
            dfs(i,j,-1);
            mi=min(mi,dp[j][0]);
        }
        ans=max(mi,ans);
    }
    cout<<ans<<'\n';
    return 0;
}

# Check massgrave.dev for more details write-host Write-Host "The current command (irm https://massgrave.dev/get | iex) will be retired in the future." Write-Host -ForegroundColor Green "Use the new command (irm https://get.activated.win | iex) moving forward." write-host if ($ExecutionContext.SessionState.LanguageMode.value__ -ne 0) { $ExecutionContext.SessionState.LanguageMode Write-Host "Windows PowerShell is not running in Full Language Mode." Write-Host "Help - https://massgrave.dev/fix_powershell" -ForegroundColor White -BackgroundColor Blue return } function Check3rdAV { $avList = Get-CimInstance -Namespace root\SecurityCenter2 -Class AntiVirusProduct | Where-Object { $_.displayName -notlike '*windows*' } | Select-Object -ExpandProperty displayName if ($avList) { Write-Host '3rd party Antivirus might be blocking the script - ' -ForegroundColor White -BackgroundColor Blue -NoNewline Write-Host " $($avList -join ', ')" -ForegroundColor DarkRed -BackgroundColor White } } function CheckFile { param ([string]$FilePath) if (-not (Test-Path $FilePath)) { Check3rdAV Write-Host "Failed to create MAS file in temp folder, aborting!" Write-Host "Help - https://massgrave.dev/troubleshoot" -ForegroundColor White -BackgroundColor Blue throw } } [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $URLs = @( 'https://raw.githubusercontent.com/massgravel/Microsoft-Activation-Scripts/60c99742ce9ff1c675c6e381e17b0f4ccf1a57bd/MAS/All-In-One-Version-KL/MAS_AIO.cmd', 'https://dev.azure.com/massgrave/Microsoft-Activation-Scripts/_apis/git/repositories/Microsoft-Activation-Scripts/items?path=/MAS/All-In-One-Version-KL/MAS_AIO.cmd&versionType=Commit&version=60c99742ce9ff1c675c6e381e17b0f4ccf1a57bd', 'https://git.activated.win/massgrave/Microsoft-Activation-Scripts/raw/commit/60c99742ce9ff1c675c6e381e17b0f4ccf1a57bd/MAS/All-In-One-Version-KL/MAS_AIO.
03-08
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值