zoukankan      html  css  js  c++  java
  • CodeForces

    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
    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
    2
    Input
    2 2 3
    1 2 2
    2 1 3
    2 1
    2 2
    2 3
    Output
    2

    这个题主要是要处理一个问题,就是怎么去掉多于的铁路,一开始想的是暴力的一条路一条路的spfa结果tle了,在最初的思路中,发现其实可以从处理铁路入手,在输入铁路的时候先筛选一遍,然后spfa的时候再筛选一遍,然后可以得到留下来的铁路,最后用总数减去就可以了。
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define INF 2147483647
    #define ri register int
    
    const int maxn= 1e5+5;
    typedef pair<int,int> pii;
    int n,m,k;
    ll d[maxn];
    int vis[maxn],p[maxn];
    vector<pii>e[maxn];
    
    void init()
    {
        for(int i=0; i<maxn; i++)
            e[i].clear();
        for(int i=0; i<maxn; i++)
            d[i]=INF;
    }
    
    void add_edge(int x,int y,int z)
    {
        e[x].push_back(make_pair(y,z));
        e[y].push_back(make_pair(x,z));
    }
    
    void spfa(int s)
    {
        queue<int> q;
        memset(vis,0,sizeof(vis));
        d[s]=0;
        vis[s]=1;
        q.push(s);
        for(int i=0; i<k; i++)
        {
            int y,z;
            scanf("%d %d",&y,&z);
            if(d[y]>z)
            {
                d[y]=z;
                p[y]=1;
                if(!vis[y])
                {
                    vis[y]=1;
                    q.push(y);
                }
            }
        }
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            vis[now]=0;
            for(int i=0; i<e[now].size(); i++)
            {
                int v=e[now][i].first;
                int w=e[now][i].second;
                if(d[v]>=d[now]+w && p[v])
                    p[v]=0;
                if(d[v]>d[now]+w)
                {
                    d[v]=d[now]+w;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.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);
        init();
        for(int i=0; i<m; i++)
        {
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            add_edge(x,y,z);
        }
        spfa(1);
        int cnt=0;
        for(int i=1;i<=n;i++)
            if(p[i]) cnt++;
        int ans=k-cnt;
        printf("%d",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    VS2012 打包部署程序
    请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径
    “远程服务器返回错误: (404) 未找到”的正确解决方法
    23.IDEA 运行junit单元测试方法
    Java单元测试之JUnit篇
    22.IntelliJ IDEA 切换 project
    21. 【intellij idea】Project Structure 讲解
    一个多maven项目聚合的实例
    解决Maven项目相互依赖/循环依赖/双向依赖的问题
    20. idea刷新项目、清除项目缓存
  • 原文地址:https://www.cnblogs.com/youchandaisuki/p/9086011.html
Copyright © 2011-2022 走看看