[Codeforces 449B] Jzzhu and Cities

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;
    
}

 

以上是关于[Codeforces 449B] Jzzhu and Cities的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 449D:Jzzhu and Numbers

Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

Codeforces 449D. Jzzhu and Numbers

CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数

Jzzhu and Numbers CodeForces - 449D (容斥,dp)