Codeforces Round #257 (Div. 2)D(最短路Dijkstra+堆优化)

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

题意:给了n个点,m条边,k条专线(起点一定为1),都是双向的,要求的是最多可以删掉多少条专线,使得1结点到任何其它节点的最短路长度不变

思路:裸的最短路,只是在跑最短路的时候记录一下即可,如果 i 可以被更新,那么判断更新 i 的边是否为专线,是就+1,然后判断原来直达 i 的边是否为专线,是就-1,还有

            一个要判断的地方是,当前到 i 最短路的值和 i 以前的一样,那么就判断以前直达 i 的边是否为专线,且现在的边不是专线,不然专线替换专线没意义,是就将之替

            换为当前的边,同时答案-1,最后输出答案即可,我先用spfa写在第45组超时了,之后改成迪杰斯特拉堆优化就过了

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; #define maxn 100010 #define INF (0x7fffffffffffffff-((1<<31)-1)) typedef long long ll; struct node{ int v; int vis; int w; }; int n,m,k; int head[maxn]; node edge[maxn*8]; int next[maxn*8]; ll dp[maxn]; int vis[maxn]; int fa[maxn]; int ans; int d; typedef pair 
        
          pii; priority_queue 
         
           ,greater 
          
            >q; void add(int u,int v,int w,int vis) { edge[d].v=v; edge[d].w=w; edge[d].vis=vis; next[d]=head[u]; head[u]=d++; } void spfa() { memset(vis,0,sizeof(vis)); int i; for(i=1;i<=n;i++)dp[i]=INF; memset(fa,-1,sizeof(fa)); dp[1]=0; q.push(make_pair(0,1)); while(!q.empty()) { pii ff=q.top();q.pop(); int f=ff.second; if(vis[f])continue; vis[f]=1; for(i=head[f];i!=-1;i=next[i]){ int v=edge[i].v; int w=edge[i].w; int vi=edge[i].vis; if(dp[v]>dp[f]+w){ dp[v]=dp[f]+w; if(fa[v]!=-1&&edge[fa[v]].vis)ans--; fa[v]=i; if(vi)ans++; q.push(make_pair(dp[v],v)); } else if(dp[v]==dp[f]+w&&edge[fa[v]].vis&&!vi){ fa[v]=i; ans--; } } } } int main() { while(scanf("%d%d%d",&n,&m,&k)!=EOF) { int u,v,w,i; memset(head,-1,sizeof(head)); d=0; for(i=0;i 
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值