zoukankan      html  css  js  c++  java
  • [Codeforces 449B] Jzzhu and Cities

    [题目链接]

             https://codeforces.com/contest/449/problem/B

    [算法]

             最短路

             时间复杂度 : O(N ^ 2)

    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e5 + 10;
    const int INF = 2e9;
    
    int n , m , k;
    int mark[MAXN];
    long long dist[MAXN];
    bool inq[MAXN];
    vector< pair<int,int> > G[MAXN];
    
    template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
    template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
    template <typename T> inline void read(T &x)
    {
        T f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    
    int main()
    {
            
            read(n); read(m); read(k);
            for (int i = 1; i <= m; i++)
            {
                    int u , v , w;
                    read(u); read(v); read(w);
                    G[u].push_back(make_pair(v,w));
                    G[v].push_back(make_pair(u,w));
            }
            queue< int > q;
            for (int i = 1; i <= n; i++)
            {
                    dist[i] = INF;
                    mark[i] = 0;
                    inq[i] = false;
            }
            dist[1] = 0;
            q.push(1);
            inq[1] = true;
            for (int i = 1; i <= k; i++)
            {
                    int u , w;
                    read(u); read(w);
                    mark[u] = 1;
                    if (w < dist[u])
                    {
                            dist[u] = w;
                            if (!inq[u])
                            {
                                    inq[u] = true;
                                    q.push(u);        
                            }        
                    }        
            }
            while (!q.empty())
            {
                    int u = q.front();
                    q.pop();
                    inq[u] = false;        
                    for (unsigned i = 0; i < G[u].size(); i++)
                    {
                            int v = G[u][i].first , w = G[u][i].second;
                            if (dist[u] + w <= dist[v] && mark[v]) mark[v] = 0;
                            if (dist[u] + w < dist[v])
                            {
                                    dist[v] = dist[u] + w;
                                    if (!inq[v])
                                    {
                                            inq[v] = true;
                                            q.push(v);
                                    }
                            }
                    }
            }
            for (int i = 1; i <= n; i++) k -= mark[i];
            printf("%d
    ",k);
            
            return 0;
        
    }
  • 相关阅读:
    记周日一次故障意外
    每周一坑-【3月第1周】
    关于计划任务的一个小需求-优化篇
    400篇博客的一个里程碑
    关于计划任务的一个小需求-实现篇
    关于计划任务的一个小需求
    微服务优雅停机研究
    NSUInteger设为负数
    Mac上运行第一个Flutter项目
    Vue filtersfilter、computed、methods、watch对比
  • 原文地址:https://www.cnblogs.com/evenbao/p/9736553.html
Copyright © 2011-2022 走看看