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

思路:这题仅仅要引入一个最短路条数,然后再遍历火车线,假设最短路与火车线长度相等,此时假设最短路条数是1的话,那说明这个最短路就是火车线,不能去掉,假设最短路条数大于1条,说明除了这条火车线外还有别的跟他相同短的路,能够去掉;假设火车线长度比最短路长的话,显然能够去掉。

这题刚開始看确实挺蛋疼的,比赛的时候也没啥想法。好久不做图论方面的了,所以有点不会了。

刚才敲spfa还又一次看了下这算法才会手敲,确实有点生了。

刚開始queue没过,T了。然后改成优先队列就过了。呵呵。没明确。不是dijkstra才得用优先队列么,这题为什么也要用。后面想想,应该是先后问题T了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1000000070000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int cnt,n,head[900005],vis[900005];
int repeat[900005];//记录反复的边
ll dist[900005],Map[900005];
struct node
{
int v,next,w;
} e[900005];
void add(int u,int v,int w)
{
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
void spfa()
{
int i,j;
priority_queue<int>q;
for(i=0; i<=n; i++) dist[i]=INF;
repeat[1]=1;
q.push(1);
vis[1]=1;
dist[1]=0;
while(!q.empty())
{
int u=q.top();
q.pop();
vis[u]=0;
for(i=head[u]; i!=-1; i=e[i].next)
{
if(dist[e[i].v]>dist[u]+e[i].w)
{
dist[e[i].v]=dist[u]+e[i].w;
repeat[e[i].v]=repeat[u];
if(!vis[e[i].v])
{
vis[e[i].v]=1;
q.push(e[i].v);
}
}
else if(dist[e[i].v]==dist[u]+e[i].w)
{
repeat[e[i].v]+=repeat[u];
if(repeat[e[i].v]>=2) repeat[e[i].v]=2;
}
}
}
}
int main()
{
int m,k,i,j,u,v,w,sum=0;//sum表示须要保留的火车线路
mem(head,-1);
cin>>n>>m>>k;
for(i=0;i<=n;i++)
Map[i]=INF;
for(i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(i=0; i<k; i++)
{
scanf("%d%d",&v,&w);
if(Map[v]>w) Map[v]=w;
}
for(i=1; i<=n; i++)
{
if(Map[i]!=INF)
{
add(1,i,Map[i]);
add(i,1,Map[i]);
sum++;
}
}
spfa();
for(i=1; i<=n; i++)
if(Map[i]!=INF)
{
if(dist[i]==Map[i]&&repeat[i]==2) sum--;
else if(dist[i]<Map[i]) sum--;
}
printf("%d\n",k-sum);//总的减去保留在最短路里面的
return 0;
}

最新文章

  1. css 表格
  2. Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
  3. paip.java c++得到当前类,方法名称以及行号
  4. android 图片全屏
  5. hdu-1800
  6. clinit和init(转载)
  7. JavaScript操作cookie基础分析
  8. PHP秒杀系统全方位设计(一)
  9. POST不同提交方式对应的Content-Type,及java服务器接收参数方式
  10. 还在问跨域?本文记录js跨域的多种实现实例
  11. JQ 放大镜
  12. [看图说话]在VMware Workstation 9中安装Mac OS X 10.8 Mountain Lion
  13. Junit4使用实验报告
  14. springboot系列一、springboot产生背景及介绍
  15. python零碎知识点
  16. php会话控制技术
  17. 【struts2】值栈(后篇)
  18. 持续集成之四:Jenkins+sonarqube
  19. Usage of “symmetrical” and “symmetric”
  20. JS: 防抖节流

热门文章

  1. codeforces 592B The Monster and the Squirrel
  2. Win8下在Vmware11中安装使用苹果系统OS X 10.10
  3. 跟我一起学extjs5(13--运行菜单命令在tabPanel中显示模块)
  4. c++windows内核编程笔记day12 硬盘逻辑分区管理、文件管理、内存管理
  5. 安卓开发28:自定义View类
  6. jQuery 顶部导航尾随滚动,固定浮动在顶部
  7. 自顶向下分析Binder【1】—— Binder实例篇
  8. cocos2dx 制作单机麻将(一)
  9. Java PreparedStatement
  10. HighChart学习-更新数据data Series与重绘