Jzzhu and Cities(CodeForces - 449B)

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

解题思路:此题最开始没理解清楚,原来火车的路径,也要加入最短路的计算中,最后再来判断可不可以去掉此火车线路。就是记录一下每个点的入度是多少。最后判断那些起点到一个点,可以乘坐火车的点,如果到该点的最短花费小于火车的花费,就是可以去掉的,如果是等于,入度为1不能去掉,如果去掉就是去掉了火车线路,入度大于1就是可以去掉的。不可能出现大于的情况。

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
#define inf 1e17
struct s{
   int to;
   long long  val;
   int flag;
   int next;
}edge[1000000];
int head[200010];
int indegree[200010];
int cnt;
void add(int u,int v,long long  w,int flag)
{
    edge[++cnt].to=v;
    edge[cnt].val=w;
    edge[cnt].flag=flag;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
long long dis[200010];
int book[200010];
int c[100010];
long long  d[100010];
struct s1{
    int x;
    long long  val;
    friend bool operator <(s1 u,s1 v)
    {
        return u.val>v.val;
    }
};
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    memset(indegree,0,sizeof(indegree));
    memset(head,-1,sizeof(head));
    cnt=0;
    memset(edge,0,sizeof(edge));
    for(int i=0;i<m;i++)
    {
        int x,y;
        long long z;
        scanf("%d%d%I64d",&x,&y,&z);
        add(x,y,z,0);
        add(y,x,z,0);
    }
    for(int i=0;i<k;i++)
    {
        scanf("%d%I64d",&c[i],&d[i]);
        add(1,c[i],d[i],1);
        add(c[i],1,d[i],1);
    }
    for(int i=0;i<=n;i++)
    {
        dis[i]=inf;
    }
    dis[1]=0;
    s1 tmp;
    tmp.x=1;
    tmp.val=0;
    priority_queue<s1>q;
    q.push(tmp);
    while(!q.empty())
    {
        s1 temp=q.top();
        q.pop();
        if(book[temp.x]==0)
        {
            book[temp.x]=1;
            for(int i=head[temp.x];i!=-1;i=edge[i].next)
            {
                if(dis[edge[i].to]==dis[temp.x]+edge[i].val)
                    indegree[edge[i].to]++;
                if(dis[edge[i].to]>dis[temp.x]+edge[i].val)
                {
                    indegree[edge[i].to]=1;
                    dis[edge[i].to]=dis[temp.x]+edge[i].val;
                    s1 tn;
                    tn.x=edge[i].to;
                    tn.val=dis[edge[i].to];
                    q.push(tn);
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<k;i++)
    {
        if(dis[c[i]]<d[i])
        {
            ans++;
        }
        else if(dis[c[i]]==d[i])
        {
            if(indegree[c[i]]>1)
            {
                ans++;
                indegree[c[i]]--;
            }
        }
    }
    printf("%d\n",ans);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值