제출 #1061015

#제출 시각아이디문제언어결과실행 시간메모리
1061015Faisal_Saqib전선 연결 (IOI17_wiring)C++17
7 / 100
654 ms262144 KiB
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e16;
long long min_total_length(std::vector<int> RR, std::vector<int> BB)
{
    vector<ll> r,b;
	for(auto i:RR)r.push_back(i);
	for(auto i:BB)b.push_back(i);
    ll n=r.size();
    ll m=b.size();
    map<ll,map<ll,ll>> dp;
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            dp[i][j]=inf;
        }
    }
    dp[0][0]=0;
    priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>>pq;
    pq.push({0,{0,0}});
    while(pq.size()>0)
    {
        auto it=pq.top();
        pq.pop();
        int i=it.second.first;
        int j=it.second.second;
        if(i==n and j==m)break;
        if(i==n or j==m)continue;
        // cout<<"At "<<i<<' '<<j<<' '<<dp[i][j]<<endl;
        ll nc=dp[i][j]+abs(r[i]-b[j]);
        if(nc<dp[i+1][j+1])
        {
            dp[i+1][j+1]=nc;
            pq.push({nc,{i+1,j+1}});
        }
        if(nc<dp[i+1][j])
        {
            dp[i+1][j]=nc;
            pq.push({nc,{i+1,j}});
        }
        if(nc<dp[i][j+1])
        {
            dp[i][j+1]=nc;
            pq.push({nc,{i,j+1}});
        }
    }
    return dp[n][m];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...