Codeforces Round #257 (Div. 2) D Jzzhu and Cities 最短路

在国家A中,总统Jzzhu需要优化城市的连接并关闭部分列车路线,确保从每个城市到首都的最短路径不发生变化。通过SPFA算法解决此问题。
部署运行你感兴趣的模型镜像
D. Jzzhu and Cities
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 mroads 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.

Sample test(s)
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


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;



const int maxn=1111111;

int n,m,k;

int vis[maxn],pre[maxn];
pair<int,int> a[maxn];
vector< pair<int,int> > e[maxn];
long long dis[maxn];
queue <int> Q;

void spfa(int s)
{
    dis[s]=0;
    vis[s]=1;
    Q.push(s);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=0;i<e[u].size();i++)
        {
            int v=e[u][i].first,w=e[u][i].second;
            if(dis[v]>=dis[u]+w)
            {
                pre[v]=u;
                dis[v]=dis[u]+w;
                if (!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }

        }
    }
}

int main()
{
   
     

    scanf("%d%d%d",&n,&m,&k);

    int u,v,w;
    int ans=0;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        e[u].push_back(make_pair(v,w));
        e[v].push_back(make_pair(u,w));
    }

    for(int i=0;i<k;i++)
        scanf("%d%d",&a[i].second,&a[i].first);

    sort(a,a+k);

//    for(int i=0;i<k;i++)
//     cout<<a[i].second<<" "<<a[i].first<<endl;

    memset(vis,0,sizeof(vis));
    memset(pre,0,sizeof(pre));

   memset(dis,0x3f,sizeof(dis));
    for(int i=0;i<k;i++)
    {
        if(pre[a[i].second])
        {
            ans++;
            a[i].second=0;
        }
        else
        {
            Q.push(a[i].second);
            vis[a[i].second]=1;
            dis[a[i].second]=a[i].first;
            pre[a[i].second]=-1;
        }
    }

    //cout<<ans<<endl;

    spfa(1);

    for(int i=0;i<k;i++)
    {
        if(a[i].second!=0 && pre[a[i].second]!=-1)
            ans++;
    }

    cout<<ans<<endl;

    return 0;
}


您可能感兴趣的与本文相关的镜像

ComfyUI

ComfyUI

AI应用
ComfyUI

ComfyUI是一款易于上手的工作流设计工具,具有以下特点:基于工作流节点设计,可视化工作流搭建,快速切换工作流,对显存占用小,速度快,支持多种插件,如ADetailer、Controlnet和AnimateDIFF等

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值