Jzzhu and Cities(CodeForces

来源:互联网 发布:centos开启mysql服务器 编辑:程序博客网 时间:2024/05/17 06:01
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 31 2 12 3 21 3 33 4 41 5 53 54 55 5
output
2
input
2 2 31 2 22 1 32 12 22 3
output
2

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

#include<stdio.h>#include<queue>#include<algorithm>#include<string.h>using namespace std;#define inf 1e17struct 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);}


原创粉丝点击