zoukankan      html  css  js  c++  java
  • CodeForces-449B(单源最短路,思维)

    链接:

    https://vjudge.net/problem/CodeForces-449B

    题意:

    Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.

    Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.

    思路:

    先求到每个点的最短路,同时记录到每个点的最短路有几条.
    最后比较每条火车道,是否可以删除.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 1e6+10;
    const long long INF = 1e15;
    
    struct Edge
    {
        int to;
        LL v;
    };
    
    struct HeapNode
    {
        int to;
        LL dis;
        bool  operator < (const HeapNode& that) const
        {
            return this->dis > that.dis;
        }
    };
    
    vector<Edge> G[MAXN];
    int In[MAXN], Vis[MAXN];
    LL Dis[MAXN];
    int To[MAXN], Va[MAXN];
    int n, m, k;
    
    void Dij()
    {
        memset(Vis, 0, sizeof(Vis));
        memset(In, 0, sizeof(In));
        In[1] = 1;
        for (int i = 1;i <= n;i++)
            Dis[i] = INF;
        Dis[1] = 0;
        priority_queue<HeapNode> que;
        que.push(HeapNode{1, 0LL});
        while (!que.empty())
        {
            HeapNode node = que.top();
            que.pop();
            if (Vis[node.to])
                continue;
            Vis[node.to] = 1;
            for (int i = 0;i < G[node.to].size();i++)
            {
                int ne = G[node.to][i].to;
                if (Vis[ne])
                    continue;
                LL va = G[node.to][i].v;
                if (Dis[ne] == Dis[node.to]+va)
                    In[ne]++;
                if (Dis[ne] > Dis[node.to]+va)
                {
                    In[ne]=1;
                    Dis[ne] = Dis[node.to]+va;
                }
                que.push(HeapNode{ne, Dis[ne]});
            }
        }
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int u, v, w;
        cin >> n >> m >> k;
        for (int i = 1;i <= m;i++)
        {
            cin >> u >> v >> w;
            G[u].push_back(Edge{v, w});
            G[v].push_back(Edge{u, w});
        }
        for (int i = 1;i <= k;i++)
        {
            cin >> v >> w;
            To[i] = v, Va[i] = w;
            G[1].push_back(Edge{v, w});
        }
        Dij();
    //    for (int i = 1;i <= n;i++)
    //        cout << Dis[i] << ' ' ;
    //    cout << endl;
        int num = 0;
        for (int i = 1;i <= k;i++)
        {
            if (Dis[To[i]] < Va[i])
                num++;
            else if (Dis[To[i]] == Va[i] && In[To[i]] > 1)
                num++, In[To[i]]--;
        }
        cout << num << endl;
    
        return 0;
    }
    
  • 相关阅读:
    Window安装Redis并设置为开机启动
    大佬为你揭秘微信支付的系统架构,你想知道的都在这里了
    想要设计自己的微服务?看这篇文章就对了
    破局人工智能:构建AI,与腾讯云一起探索语音应用场景
    巧用机器学习定位云服务器故障
    重磅发布 | 黑镜调查:深渊背后的真相之「DDoS 威胁与黑灰产业调查报告」
    5分钟内看懂机器学习和深度学习的区别
    如何防范和应对Redis勒索,腾讯云教你出招
    Redis勒索事件爆发,如何避免从删库到跑路?
    作为一个编程新手,我再也不怕Flink迷了我的眼!
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11367121.html
Copyright © 2011-2022 走看看