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\n%d\n",ans,chang.size());
    for(auto ch:chang){
        printf("%d ",ch);
    }
    printf("\n%d\n",road.size());
    for(auto ro:road){
        printf("%d %d\n",ro.first,ro.second);
    }

    return 0;
}

\(n^2log(n)\)水过

原文地址:https://www.cnblogs.com/xxrlz/p/11781282.html

时间: 2024-10-09 00:00:37

Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid的相关文章

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

#include<bits/stdc++.h> using namespace std ; int n; struct City { int id; long long x,y; //坐标 long long cc,kk; //自建的花费,连线的花费 bool self;//是否建站 int fa;//连线的站 bool operator < (const City & a)const { return cc<a.cc; } } c[2005]; int main() {

Codeforces Round #597 (Div. 2) A. Good ol&#39; Numbers Coloring

链接: https://codeforces.com/contest/1245/problem/A 题意: Consider the set of all nonnegative integers: 0,1,2,-. Given two integers a and b (1≤a,b≤104). We paint all the numbers in increasing number first we paint 0, then we paint 1, then 2 and so on. Ea

Codeforces Round #597 (Div. 2) B. Restricted RPS

链接: https://codeforces.com/contest/1245/problem/B 题意: Let n be a positive integer. Let a,b,c be nonnegative integers such that a+b+c=n. Alice and Bob are gonna play rock-paper-scissors n times. Alice knows the sequences of hands that Bob will play. H

题解Codeforces Round #597 (Div. 2)

A:送分,裸的gcd. 1 #include<stdio.h> 2 #define il inline 3 #define it register int 4 int T,a,b,d; 5 il void gcd(int a,int b){ 6 if(!b){d=a;return;} 7 gcd(b,a%b); 8 } 9 il void fr(int &num){ 10 num=0;char c=getchar();int p=1; 11 while(c<'0'||c>'

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

E. Hyakugoku and Ladders Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she w

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

/*每个点自己建立一座发电站相当于向超级源点连一条长度为c[i]的边,连电线即为(k[i]+k[j])*两点间曼哈顿距离,跑最小生成树(prim适用于稠密图,kruscal适用于稀疏图)*/ #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int fa[2007];int x[2007],y[2007];int c[2007],k[2007];long long m[2007][2007];vecto

Codeforces Round #589 (Div. 2) B——B. Filling the Grid

Suppose there is a h×wh×w grid consisting of empty or full cells. Let's make some definitions: riri is the number of consecutive full cells connected to the left side in the ii-th row (1≤i≤h1≤i≤h). In particular, ri=0ri=0 if the leftmost cell of the 

Shichikuji and Power Grid

D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由此建立最小生成树,即可得出答案. 代码: // Created by CAD on 2019/11/2. #include <bits/stdc++.h> #define ll long long #define pii pair<int,int> using namespace st

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i