• AcWing 第 2 场周赛


    比赛链接:Here

    AcWing 3626. 三元一次方程

    暴力即可

    void solve() {
        int n;
        cin >> n;
        for (int i = 0; i <= n / 3; ++i)
            for (int j = 0; j <= n / 5; ++j)
                for (int k = 0; k <= n / 7; ++k)
                    if (i * 3 + j * 5 + k * 7 == n) {
                        cout << i << " " << j << " " << k << "
    ";
                        return ;
                    }
        cout << -1 << "
    ";
    }
    

    AcWing 3627. 最大差值

    把次k最大值累加到最大值即可

    void solve() {
        int n, k; cin >> n >> k;
        vector<ll>a(n);
        for (ll &x : a)cin >> x;
        sort(a.begin(), a.end());
        int i = n -  2;
        while (k and i >= 0) {
            if (a[i] == 0) {i--; continue;}
            a[n - 1] += a[i];
            i--, k--;
        }
        cout << a[n - 1] << "
    ";
    }
    

    AcWing 3628. 边的删减

    本题给定一个无向连通加权图,从第 1 个点出发到第 i 个点的最短距离记为 di

    接着我们需要进行操作,删掉图中的一些边,最多保留原图中的 k 条边

    删完边后,如果点 i 到点 1 的距离任然是原图的中最短路长度,则我们称该点为优秀点

    我们需要找出一种删边方案,使得最终图中有尽可能多的 优秀点

    分析
    对于初始图,我们需要得到每个点 i 到起始点 1 的最短路径长度,这毫无疑问会让我们想到最短路算法

    因此,一开始我们需要挑选一个最短路算法来求每个点到起点的最短路径

    接着看下一个要求,我们要找出一个保留小于等于 k 条边的方案,使得构成最短路的点尽可能多

    为了保证我们保留下来的边是有价值的,因此我们保留的边一定是更新出最短路的边

    (Dijkstra)算法 的贪心思想是:每次更新到起点距离最短的点的最短路长度

    利用该性质,我们知道:
    (Dijkstra)算法 对于每轮更新出最短路的点来说,最后一次更新他距离的边一定是 有价值的

    这些有价值的边,连成的点就构成了一个最短路树

    因此我们直接用 Dijkstra 求一遍最短路的同时,把每个点最后一次被更新最短距离的边都存下来

    于是被保留下来边,一定是有价值的边

    从前往后(不从前往后保留,就不在一个连通块里,那这样的边也是没价值的),把这些边保留下来即可

    注意:用 long long 存路径长度

    #define x first
    #define y second
    using LL = long long;
    typedef pair<LL, int> PII;
    const int N = 100010, M = 200010;
    int n, m, k;
    int h[N], e[M], w[M], id[M], ne[M], idx;
    LL dist[N];
    bool st[N];
    vector<int> ans;
    
    void add(int a, int b, int c, int d) { // 添加一条边a->b,边权为c
        e[idx] = b, w[idx] = c, id[idx] = d, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    void dijkstra() { // 求1号点到n号点的最短路距离
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        priority_queue<PII, vector<PII>, greater<PII>> heap;
        heap.push({0, 1});
    
        while (heap.size()) {
            auto t = heap.top();
            heap.pop();
    
            int ver = t.second, distance = t.first;
    
            if (st[ver]) continue;
            st[ver] = true;
    
            for (int i = h[ver]; i != -1; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[ver] + w[i]) {
                    dist[j] = dist[ver] + w[i];
                    heap.push({dist[j], j});
                }
            }
        }
    }
    
    void dfs(int u) {
        st[u] = true;
        for (int i = h[u]; ~i; i = ne[i]) {
            int j = e[i];
            if (!st[j] && dist[j] == dist[u] + w[i]) {
                if (ans.size() < k) ans.push_back(id[i]);
                dfs(j);
            }
        }
    }
    
    int main() {
        cin >> n >> m >> k;
        memset(h, -1, sizeof h);
        for (int i = 1; i <= m; i ++ ) {
            int a, b, c;
            cin >> a >> b >> c;
            add(a, b, c, i), add(b, a, c, i);
        }
    
        dijkstra();
    
        memset(st, 0, sizeof st);
        dfs(1);
    
        printf("%d
    ", ans.size());
        for (auto x : ans) printf("%d ", x);
        return 0;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    mysql 1
    mysql 创建库
    SQLServer2008数据库卸载图解
    同一个局域网中用Windows自己的远程桌面远程局域网中的其他PC
    WIN10怎么安装SQL server2000数据库
    怎样彻底卸载(删除)SQL server2000
    SQL Server 2000安装教程图解
    如何将网页保存为PDF文件
    【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(四)
    【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(五)
  • 原文地址:https://www.cnblogs.com/RioTian/p/14971342.html
Copyright © 2020-2023  润新知