CodeForces - 450D Jzzhu and Cities (spfa)

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 
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];
vectore[maxn];

void init()
{
    for(int i=0; i)
        e[i].clear();
    for(int i=0; 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)
    {
        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)
        {
            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)
    {
        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

 

转载于:https://www.cnblogs.com/youchandaisuki/p/9086011.html

你可能感兴趣的:(ui)