Codeforces 449B Jzzhu and Cities【最短路SPFA+思维+玄学优先队列】

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

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


题目大意:

有n个点,m条无向边,k条火车路。

火车路是从节点1直接通向节点的路。

问最多可以拆除多少条火车路。


思路:

1、首先,建图之后跑最短路 ,得到从节点1到其他个点的最短路。


2、然后将dis【i】<火车路花费的火车路拆除掉,因为显然这条火车路是不需要的,因为有更短的路径比火车路的路径更短


3、当dis【i】==火车路花费的时候,我们要记录节点i的最短路径入度,如果入度>1,那么就可以拆除当前火车路。因为这样就说明有两条及以上的路径能够从1到达节点i,那么这个火车路也是多余的。那么要如何记录节点i的最短路径入度呢?我们在松弛的时候加上两个语句:

if(dis【v】>dis【u】+w)in【v】=1;

if(dis【v】==dis【u】+w)in【v】++;

这样我们就能够统计下来节点i的最短路径条数。


4、玄学优先队列。我写的是SPFA,队列TLE on 45,栈TLE on 4,因为比较渣,堆写的很挫,写了发优先队列+Dij,也是TLE on45,最后百度了一下题解,竟然可以用优先队列水过数据.........................(难道优先队列真的能够优化算法?)


Ac代码;


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
ll head[1000000];
struct node
{
    ll from;
    ll to;
    ll next;
    ll w;
}e[1515151];
ll in[15151515];
ll go[1515151];
ll val[1515151];
ll dis[1515155];
ll vis[1515155];
ll n,m,k,cont;
void add(ll from,ll to,ll w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA(ll ss)
{
    priority_queue<ll>s;
    for(ll i=1;i<=n;i++)dis[i]=1000000000000000000;
    memset(vis,0,sizeof(vis));
    vis[ss]=1;
    dis[ss]=0;
    s.push(ss);
    while(!s.empty())
    {
        ll u=s.top();
        s.pop();
        vis[u]=0;
        for(ll i=head[u];i!=-1;i=e[i].next)
        {
            ll v=e[i].to;
            ll w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                in[v]=1;
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
            else if(dis[v]==dis[u]+w)in[v]++;
        }
    }
}
int main()
{
    while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
    {
        cont=0;
        memset(in,0,sizeof(in));
        memset(head,-1,sizeof(head));
        for(ll i=0;i<m;i++)
        {
            ll x,y,w;
            scanf("%I64d%I64d%I64d",&x,&y,&w);
            add(x,y,w);
            add(y,x,w);
        }
        for(ll i=0;i<k;i++)
        {
            scanf("%I64d%I64d",&go[i],&val[i]);
            add(1,go[i],val[i]);
            add(go[i],1,val[i]);
        }
        ll output=0;
        SPFA(1);
        for(ll i=0;i<k;i++)
        {
            if(dis[go[i]]<val[i])output++;
            else if(dis[go[i]]==val[i])
            {
                if(in[go[i]]>1)
                {
                    in[go[i]]--;
                    output++;
                }
            }
        }
        printf("%I64d\n",output);
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值