Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路
2 seconds
256 megabytes
standard input
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.
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 ≤ n; ui ≠ 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 a single integer representing the maximum number of the train routes which can be closed.
5 5 3
1 2 1
2 3 2
1 3 3
3 4 4
1 5 5
3 5
4 5
5 5
2
2 2 3
1 2 2
2 1 3
2 1
2 2
2 3
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;
}
Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路的更多相关文章
- Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟
E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #257 (Div. 2) A题
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #257 (Div. 1)A~C(DIV.2-C~E)题解
今天老师(orz sansirowaltz)让我们做了很久之前的一场Codeforces Round #257 (Div. 1),这里给出A~C的题解,对应DIV2的C~E. A.Jzzhu and ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...
随机推荐
- php中实现快排与冒泡排序
快排 <?php function quicksort($str){ if(count($str)<=1) return $str;//如果个数不大于一,直接返回 $key=$str[0] ...
- Cocos2d-x 3.1.1 lua-tests 开篇
Cocos2d-x 3.1.1 lua-tests开篇 本篇博客打算从研究Cocos2d-x引擎提供的測试样例来写起,笔者针对Cocos2d-x 3.1.1这个版本号来介绍怎样来学习它给我们提供的 ...
- 解决AngularJS和Django模板标签冲突问题
原地址 Django和AngularJS在模板中使用同样的符号来引用变量,例如 {{variable_name}}. 有两种解决办法,各有利弊.一个修改AngularJS模板语法,另一个使用Djang ...
- Linux源文件夹结构呈现
1.arch文件夹: - 相关的存储在不同平台上的代码,每个平台都采用了不同的文件夹来区分. ******Alpha平台 ******Arm平台 ******Arv32平台 ******X86平台 2 ...
- IOS线程操作(3)
采用CGD更有效的比前两个(它被认为是如此,有兴趣的同学可以去试试). 这是推荐的方式来使用苹果的比较. GCD它是Grand Central Dispatch缩写,这是一组并行编程C介面. GCD是 ...
- filezilla安装
[alexus@wcmisdlin02 bin]$ ./filezilla ./filezilla: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not ...
- hdu4151(二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4151 题意:找出比n小的没有重复数字的总个数,例如12以内11不符合,1~10都符合. 分析:直接利用 ...
- 最锋利的Visual Studio Web开发工具扩展:Web Essentials详解
原文:最锋利的Visual Studio Web开发工具扩展:Web Essentials详解 Web Essentials是目前为止见过的最好用的VS扩展工具了,具体功能请待我一一道来. 首先,从E ...
- hdu1011(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs& ...
- prepareCall()运行存储过程
CallableStatement 对象为全部的 DBMS 提供了一种以标准形式调用已储存过程的方法.已储存过程储存在数据库中.对已储存过程的调用是 CallableStatement对象所含的内容. ...