zoukankan      html  css  js  c++  java
  • D. Jzzhu and Cities

    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.

    Input

    The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).

    Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ nui ≠ vi; 1 ≤ xi ≤ 109).

    Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).

    It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.

    Output

    Output a single integer representing the maximum number of the train routes which can be closed.

    Examples
    input
    Copy
    5 5 3
    1 2 1
    2 3 2
    1 3 3
    3 4 4
    1 5 5
    3 5
    4 5
    5 5
    output
    Copy
    2
    input
    Copy
    2 2 3
    1 2 2
    2 1 3
    2 1
    2 2
    2 3
    output
    Copy
    2


    一次spfa
    考虑对每个点,每次被更新时,有两种情况,铁路和公路,被铁路更新标记为1(被认为不能删),被公路更新就标记回0.
    另一方面,当dis[v]==dis[u]+w时,如果已被标记为1,那还需要重新标记
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c)
    {
        return min(min(a, b), c);
    }
    template <class T> inline T max(T a, T b, T c)
    {
        return max(max(a, b), c);
    }
    template <class T> inline T min(T a, T b, T c, T d)
    {
        return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d)
    {
        return max(max(a, b), max(c, d));
    }
    #define scanf1(x) scanf("%d", &x)
    #define scanf2(x, y) scanf("%d%d", &x, &y)
    #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
    #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int N =1e6;
    const int M=200005;
    // name*******************************
    struct edge
    {
        int to,nxt;
        ll w;
    } e[N];
    int tot=1;
    int fst[N];
    ll dis[N];
    int ins[N];
    int vis[N];
    int pos[N];
    priority_queue<int>que;
    int sum=0;
    int n,m,k;
    // function******************************
    void add(int u,int v,int w)
    {
        e[++tot].to=v;
        e[tot].nxt=fst[u];
        fst[u]=tot;
        e[tot].w=w;
    }
    void spfa()
    {
        For(i,1,n)dis[i]=INF;
        que.push(1);
        ins[1]=1;
        dis[1]=0;
        while(!que.empty())
        {
            int u=que.top();
            que.pop();
            ins[u]=0;
            for(int p=fst[u]; p; p=e[p].nxt)
            {
                int v=e[p].to;
                ll w=e[p].w;
                if(dis[v]==dis[u]+w&&pos[v])
                    pos[v]=vis[p];
                if(dis[v]>dis[u]+w)
                {
                    dis[v]=dis[u]+w;
                    pos[v]=vis[p];
                    if(!ins[v])
                    {
                        ins[v]=1;
                        que.push(v);
                    }
                }
            }
        }
    }
    
    //***************************************
    int main()
    {
    //    ios::sync_with_stdio(0);
    //    cin.tie(0);
        // freopen("test.txt", "r", stdin);
        //  freopen("outout.txt","w",stdout);
        scanf("%d%d%d",&n,&m,&k);
        For(i,1,m)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        For(i,1,k)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(1,x,y);
            vis[tot]=1;
        }
        spfa();
        For(i,1,n)
        sum+=pos[i];
        cout<<k-sum;
    
        return 0;
    }






  • 相关阅读:
    hdoj 2586 How far away?(最近公共祖先)
    poj 1330 A-Nearest Common Ancestors
    心形图
    B1928 日期差值
    B1022 D进制的A+B
    B1009 说反话
    hihocoder 1498 签到
    51Nod 1082 与7无关的数
    51Nod 1015 水仙花数
    51Nod 1283 最小周长
  • 原文地址:https://www.cnblogs.com/planche/p/8903086.html
Copyright © 2011-2022 走看看