D Jzzhu and Cities --SPFA
来源:互联网 发布:ubuntu如何连接宽带 编辑:程序博客网 时间:2024/06/05 15:01
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 31 2 12 3 21 3 33 4 41 5 53 54 55 5
2
2 2 31 2 22 1 32 12 22 3
2
意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路
分析:先求一次最短路,铁路的权值大于该点最短距离的显然可以删去,否则将该条边加入图中,再求最短路,记录每个点的前一个点,然后又枚举铁路,已经删去的就不用处理了,如果铁路权值大于该点最短距离又可以删去,权值相等时,该点的前一个点如果不为1,则这个点可以由其他路到达,这条铁路又可以删去。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <queue>#define lll __int64using namespace std;#define N 100006struct Edge{ int v; lll w; Edge(int _v,lll _w) { v = _v; w = _w; } Edge(){}};struct Train{ int v; lll w;}T[N];vector<Edge> G[N];lll dis[N];int n,m;int head[N],tot,pre[N];int cut[N],vis[N];queue<int> que;void SPFA(int s){ while(!que.empty()) que.pop(); memset(vis,0,sizeof(vis)); vis[s] = 1; que.push(s); for(int i=0;i<=n;i++) dis[i] = 1000000000000000LL; dis[s] = 0; while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = 0; for(int i=0;i<G[u].size();i++) { int v = G[u][i].v; lll w = G[u][i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if(!vis[v]) { vis[v] = 1; que.push(v); } } } }}void SPFA2(){ while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = 0; for(int i=0;i<G[u].size();i++) { int v = G[u][i].v; lll w = G[u][i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; pre[v] = u; if(!vis[v]) { que.push(v); vis[v] = 1; } } else if(dis[v] == dis[u] + w && pre[v] < u) pre[v] = u; } }}int main(){ int i,j,k; int u,v,y; lll w; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { tot = 0; for(i=0;i<=n;i++) G[i].clear(); memset(head,-1,sizeof(head)); memset(cut,0,sizeof(cut)); memset(pre,-1,sizeof(pre)); for(i=0;i<m;i++) { scanf("%d%d%I64d",&u,&v,&w); G[u].push_back(Edge(v,w)); G[v].push_back(Edge(u,w)); } for(i=0;i<k;i++) { scanf("%d%I64d",&y,&w); T[i].v = y; T[i].w = w; } SPFA(1); int cnt = 0; for(i=0;i<k;i++) { int v = T[i].v; lll w = T[i].w; if(dis[v] <= w) { cut[i] = 1; cnt++; } else { G[1].push_back(Edge(v,w)); G[v].push_back(Edge(1,w)); pre[v] = 1; dis[v] = w; que.push(v); vis[v] = 1; } } SPFA2(); for(i=0;i<k;i++) { if(cut[i]) continue; int v = T[i].v; lll w = T[i].w; if((dis[v] == w && pre[v] != 1) || dis[v] < w) cnt++; } printf("%d\n",cnt); } return 0;}
- D Jzzhu and Cities --SPFA
- D. Jzzhu and Cities
- 文章标题 CoderForces 450D : Jzzhu and Cities(最短路---spfa)
- Codeforces 449B - Jzzhu and Cities / 450D - Jzzhu and Cities
- CF449B-- Jzzhu and Cities
- codeforces Jzzhu and Cities
- Jzzhu and Cities CodeForces
- Jzzhu and Cities CodeForces
- Jzzhu and Cities ----CodeForces
- Jzzhu and Cities(CodeForces
- Jzzhu and Cities CodeForces
- Jzzhu and Cities CodeForces
- Codeforces Round #257 (Div. 2) D. Jzzhu and Cities
- CODEFORCES 450D Jzzhu and Cities <最短路径>
- CodeForces 450D - Jzzhu and Cities (dij + heap )
- Codeforces Round #257 (Div. 1)B题Jzzhu and Cities(spfa+slf优化)
- Codeforces 449B Jzzhu and Cities【最短路SPFA+思维+玄学优先队列】
- Codeforces 449B Jzzhu and Cities (最短路/SPFA/priority_queue<int>Q)
- 编程算法 - 多重部分和问题 代码(C)
- 数组队列习题
- KMP算法
- 辛星教你在html中设置字符集
- hdu 1863 畅通工程(图论:最小生成树+并查集)
- D Jzzhu and Cities --SPFA
- 仿指引式界面的实现原理,例ShowcaseView
- mybatis的简单使用笔记
- HELPER 2416初探
- 2014-07-20 日志
- 音频概念(收藏)
- 程序员如何自我优化
- Hibernate在企业中的实战作用
- ubuntu安装kali工具包