Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

Posted xxrlz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid相关的知识,希望对你有一定的参考价值。

  • 题意: 二维平面上n个点,每个点可以建厂,也可以与其他点连边,建厂花费为(c_i),与j连边花费为((k_i+k_j)*dis(i,j)),dis为两点之间的欧式距离,求让每个点都通电的最小花费与方案
  • 思路: 维护使这个点通电的花费的优先队列,一开始先把建厂放进去,然后每次拿出最小花费的点i,再用i去更新与i建路的花费(有点像最小生成树).
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;

const int N = 1e4+10;
pii pos[N]; 
ll c[N],k[N],vis[N],cnt;

struct node{
    ll type,val,id;    // 0: 建厂, !0:建路
    bool operator <(const node oth)const {
        if(val == oth.val) return type > oth.type;
        return val > oth.val;
    }   // 最小花费,如果花费相同,则优先建路
};
vector<pii> road; // 建路
vector<int> chang; // 建厂
int dis(int i,int j){
    return abs(pos[i].first - pos[j].first) + abs(pos[i].second - pos[j].second);
}
int main(){
    priority_queue<node> que;
    int n;
    ll ans = 0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d%d",&pos[i].first,&pos[i].second);
    }
    for(int i=1;i<=n;++i)   scanf("%d",&k[i]);
    for(int i=1;i<=n;++i)   scanf("%d",&c[i]),que.push((node){0,k[i],i});
    while(cnt<n && que.size()){
        node tp = que.top();    que.pop();
        if(vis[tp.id])  continue;
        ans+=tp.val;    vis[tp.id] = 1;
        if(tp.type==0){
            chang.push_back(tp.id);
        }else{
            road.push_back((pii){tp.id,tp.type});   
        }
        for(int i=1;i<=n;++i){
            ll val = dis(i,tp.id);
            if(!vis[i] && val<=k[i])
                que.push((node){tp.id,val*1LL*(c[i]+c[tp.id]),i});
        }
    }
    printf("%lld
%d
",ans,chang.size());
    for(auto ch:chang){
        printf("%d ",ch);
    }
    printf("
%d
",road.size());
    for(auto ro:road){
        printf("%d %d
",ro.first,ro.second);
    }

    return 0;
}

(n^2log(n))水过

以上是关于Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

codeforces Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid

Codeforces Round #597 (Div. 2) A. Good ol' Numbers Coloring

Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp

Codeforces Round #597 (Div. 2)D(最小生成树)

Codeforces Round #597 (Div. 2) C dp