首页 > 其他分享 >Oct. Training 4

Oct. Training 4

时间:2022-10-30 09:58:37浏览次数:71  
标签:Training const int ll back push Oct dis

L - Airports

https://codeforces.com/gym/100959

题意

给定n个点,第i个点为(\(x_i, y_i\)),对于曼哈顿距离小于D的两个点可以建一条边,问最大的D使得整个图联通。

思路

这就相当于求曼哈顿最大生成树。
我们可以开八个set来维护 两组\(x_i + y_i\) \(-x_i - y_i\) \(x_i - y_i\) \(y_i - x_i\) 分别代表选和未选的点集
因为两点之间的曼哈顿距离是\(max(x_1 + y_1 - x_2 - y_2, x_1 + x_2 + y_1 - y_2, x_1 + x_2 + y_1 + y_2, x_1 - x_2 + y_1 + y_2)\)
所以'++'的set一定和'--'的set匹配,'+-'的set 一定和'-+'的set匹配。
后面每次比较四种组合方式求最大。

#include<bits/stdc++.h>
#include<array>
#define ll long long
#define all(a) a.begin(),a.end()
using namespace std;

const int N = 2e5 + 5;
const int M = 1e6 + 1;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
#define int ll

int n;
int vis[N], date[N][5];
multiset<pair<int, int>>st1[5], st2[5];

void solve() {
    cin >> n;
    for (int i = 1, u, v; i <= n; i++) {
        cin >> u >> v;
        st1[1].insert({ u + v, i }), date[i][1] = u + v;
        st1[2].insert({ -u + v, i }), date[i][2] = -u + v;
        st1[3].insert({ u - v, i }), date[i][3] = u - v;
        st1[4].insert({ -u - v, i}), date[i][4] = -u - v;
    }

    int mx = -INF, p = 0;
    for (int i = 1; i <= 4; i++) {
        auto t = st1[i].end(); t--;
        if (mx < (*t).first) {
            mx = (*t).first;
            p = (*t).second;
        }
    }

    for (int i = 1; i <= 4; i++) {
        auto it = st1[i].find({ date[p][i], p });
        st2[i].insert(*it);
        st1[i].erase(it);  
    }

    int num = 1;
    ll anss = INF;
    while (1) {
        if (st1[1].empty()) break;
        int ans = -INF, s1 = 0, s2 = 0, id = 0;
        for (int i = 1; i <= 4; i++) {

            if (st1[i].empty() || st2[4 - i + 1].empty()) continue;

            auto t1 = st1[i].end();
            t1--;
            while (vis[(*t1).second]) st1[i].erase(t1), t1--;
            auto t2 = st2[4 - i + 1].end();

            int dis = (*t1).first + (*(--t2)).first;
            if (dis > ans) {
                ans = dis;
                id = (*t1).second;
            }
        }

        anss = min(ans, anss);
        vis[id] = 1;
        for (int i = 1; i <= 4; i++) {
            auto it = st1[i].find({ date[id][i], id });
            st2[i].insert(*it); 
            st1[i].erase(it);
        }
    }
    
    cout << anss << '\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int _t = 1;
    //cin >> _t;
    while (_t--) {
        solve();
    }

    return 0;
}

D - Minimum Path

https://codeforces.com/problemset/problem/1473/E

题意

n个点m条边 每条边有一个边权 \(w_i\) 一条从u到v的路径的价值是这条路径上每条边权和减去最大边权加上最小边权。
求1到剩余n-1个点的路径最小价值

思路

对于一条路径的价值最小边权会被加两次 最大边权会被置零。
那么我们可以建四个图 第一张图为原图,1图和2图建 2 * w 边, 1图和3图建0的边, 而后2图和4图建 0 边, 3图和4图建2 * w的边, 1图和4图案原边建立。
然后对原图上的点跑最短路即可。
这样保证了一定会将一个最小值边加两次最大值边不加,且最优。

#include<bits/stdc++.h>
#include<array>
#define ll long long
#define all(a) a.begin(),a.end()
using namespace std;

const int N = 2e5 + 5;
const int M = 1e6 + 1;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
#define int ll
int n, m;

vector<pair<int, int>>g[N*4];
int dis[N * 4], vis[N * 4];

void dij(int s) {
    for (int i = 1; i <= n * 4; i++) dis[i] = INF, vis[i] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;
    dis[s] = 0;
    q.push({ dis[s], s });

    while (q.size()) {
        auto [w, x] = q.top();
        q.pop();
        if (vis[x]) continue;
        vis[x] = 1;
        for (auto [to, w] : g[x]) {
            if (dis[to] > dis[x] + w) {
                dis[to] = dis[x] + w;
                q.push({ dis[to], to });
            }
        }
    }
}

void solve() {
    cin >> n >> m;
    for (int i = 1, u, v, w; i <= m; i++) {
        cin >> u >> v >> w;
        g[u].push_back({ v, w });
        g[v].push_back({ u, w });

        g[u + n].push_back({ v + n, w });
        g[v + n].push_back({ u + n, w });

        g[u + 2 * n].push_back({ v + 2 * n, w });
        g[v + 2 * n].push_back({ u + 2 * n, w });

        g[u + 3 * n].push_back({ v + 3 * n, w });
        g[v + 3 * n].push_back({ u + 3 * n, w });

        g[u].push_back({ v + n, w * 2 });
        g[v].push_back({ u + n, w * 2 });

        g[u].push_back({ v + 2 * n, 0 });
        g[v].push_back({ u + 2 * n, 0 });

        g[u + n].push_back({ v + 3 * n, 0 });
        g[v + n].push_back({ u + n * 3, 0 });

        g[u + n * 2].push_back({ v + 3 * n, w * 2 });
        g[v + 2 * n].push_back({ u + n * 3, w * 2 });

        g[u].push_back({ v + 3 * n, w});
        g[v].push_back({ u + n * 3, w});
    }

    dij(1);

    for (int i = 2; i <= n; i++)
        cout << dis[i + 3 * n] << " \n"[i == n];
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int _t = 1;
    //cin >> _t;
    while (_t--) {
        solve();
    }

    return 0;
}

标签:Training,const,int,ll,back,push,Oct,dis
From: https://www.cnblogs.com/yaqu-qxyq/p/16840526.html

相关文章

  • 修改Octave编辑器为vim
    Octave的默认编辑器一般是emacs,该编辑器虽然强大,但是对于新手来说并不友好,并且我在macOS中使用时,发现Octave中的emacs的快捷键有失效的现象,因此为了避免麻烦,自己修改了......
  • 中文美句_Oct.28
    "我爱在黎明前起床,在山顶牧场上召唤太阳,云雀的歌声便是我异想天开的翅膀,朝露便是我晨起的浴缸。   ......
  • Basil: A Fast and Byzantine-Resilient Approach for Decentralized Training 阅读笔
    Basil:AFastandByzantine-ResilientApproachforDecentralizedTraining阅读笔记ProblemStatementDecentralizedSystemModel所有训练数据样本存储在分布式节......
  • Oct 24 2022 学习日志
    Dijkstra用pair实现$edge$(struct)建立edge数组$E$来记录每个点的出边$pair<int,int>$(struct)用来给优先队列服务,$first$为$dis[u]$,$second$为$u$初始化:$dis[u]=......
  • 补题记录——Oct. Training 1-图论、集合哈希
    H-BoboniuWalksonGraphhttps://codeforces.com/problemset/problem/1394/B题意给n个点m条有向边,么个点的出度不超过k(k<=9),每条边都有一个边权在(\(1<=w<=m\))且每条边......
  • Windows 10, version 22H2 (released Oct 2022) 简体中文版、英文版下载
    请访问原文链接:https://sysin.org/blog/windows-10/,查看最新版。原创作品,转载请保留出处。Windows10版本信息2022/10/19从Windows10版本21H2开始,Windows10版......
  • 英语每日一句_18/Oct
    "Everyonehastalent.Whatisrareisthecouragetofollowthetalenttothedarkplacewhereitleads."人人都有天赋。罕见的是甘愿跟随天赋,尝尽人间甘苦的勇气......
  • 使用istioctl 快速部署Istio
    环境介绍k8s集群:v1.25.2istio版本:1.15.2下载Istio方法一#curl-Lhttps://istio.io/downloadIstio|ISTIO_VERSION=1.15.2TARGET_ARCH=x86_64sh-%Total%......
  • winioctl.h(10326): [C4668] 没有将“_WIN32_WINNT_WIN10_TH2”定义为预处理器宏,用
    一般为Windows中的宏和UE4冲突所致在模块的xxx.Build.cs里面添加这个:bEnableUndefinedIdentifierWarnings=false;转自:https://blog.csdn.net/boonti/article/detail......
  • BUPT 2022 Autumn Training #5
    题目链接:https://vjudge.net/contest/519146B-BestRelayTeam水题。D-DistinctiveCharacter题意给n(n<=100000)个长为k(k<=20)的01串,定义两个01串的相似度为相......