码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 450D Jzzhu and Cities (spfa)

时间:2018-05-25 01:34:29      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:air   output   must   condition   one   bit   技术分享   contains   turn   

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

 

CodeForces - 450D Jzzhu and Cities (spfa)

标签:air   output   must   condition   one   bit   技术分享   contains   turn   

原文地址:https://www.cnblogs.com/youchandaisuki/p/9086011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!