CF449B-- Jzzhu and Cities
来源:互联网 发布:淘宝成功原因 编辑:程序博客网 时间:2024/05/16 08:59
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
最短路、、、、写短路了
思路:堆优化的DIJK。每次拿权最小(权相等优先拿到达这个点用的是公路边的)
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <map>#include <set>#include <cmath>#include <queue>using namespace std;#define LL long long int#define lson id<<1,l,mid#define rson id<<1|1,mid+1,r#define maxn 200080bool vis[maxn];LL dis[maxn];struct Edge{ int v;LL cost; bool istrain; Edge(){} Edge(int vv,LL cc,bool a) { v = vv; cost = cc; istrain = a; } bool operator < (const Edge & a)const { if(cost == a.cost) return istrain; else return cost > a.cost; }};vector <Edge> ans[maxn];priority_queue <Edge> q;int Dijkstra(int n){ while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); dis[1] = 0; int nown = 0,sum = 0; q.push(Edge(1,0,0)); while(nown < n && !q.empty()) {L: Edge edge = q.top(); q.pop(); while(vis[edge.v] && !q.empty()) goto L; if(!vis[edge.v]) { int nowv = edge.v; vis[nowv] = 1; //cout << nowv << " " << edge.istrain << endl; if(edge.istrain) sum++; nown++; for(int i = 0;i < ans[nowv].size();i++) { if(!vis[ans[nowv][i].v] && (dis[ans[nowv][i].v] >= ans[nowv][i].cost+dis[nowv])) { dis[ans[nowv][i].v] = ans[nowv][i].cost+dis[nowv]; edge.v = ans[nowv][i].v; edge.cost = dis[ans[nowv][i].v]; if(ans[nowv][i].istrain) edge.istrain = 1; else edge.istrain = 0; q.push(edge); } } } } return sum;}int main(){ //freopen("in.txt","r",stdin); int n,m,k; while(scanf("%d%d%d",&n,&m,&k)==3) { for(int i = 1;i <= n;i++) ans[i].clear(); for(int i = 1;i <= m;i++) { int u,v,x; scanf("%d%d%d",&u,&v,&x); ans[u].push_back(Edge(v,x,0)); ans[v].push_back(Edge(u,x,0)); } for(int i = 1;i <= k;i++) { int s,y; scanf("%d%d",&s,&y); ans[1].push_back(Edge(s,y,1)); ans[s].push_back(Edge(1,y,1)); } int sum = Dijkstra(n); printf("%d\n",k-sum); } return 0;}
- CF449B-- Jzzhu and Cities
- codeforces Jzzhu and Cities
- Jzzhu and Cities CodeForces
- D. Jzzhu and Cities
- Jzzhu and Cities CodeForces
- Jzzhu and Cities ----CodeForces
- Jzzhu and Cities(CodeForces
- Jzzhu and Cities CodeForces
- Jzzhu and Cities CodeForces
- D Jzzhu and Cities --SPFA
- Codeforces 449 B. Jzzhu and Cities
- CodeForces 449 B. Jzzhu and Cities
- Jzzhu and Cities( cf450D) 最短路
- Codeforces 449B - Jzzhu and Cities / 450D - Jzzhu and Cities
- 【Codeforces】449B Jzzhu and Cities 最短路
- 【最短路】 codeforces 449B Jzzhu and Cities
- CF 449B - Jzzhu and Cities(最短路)
- Codeforces Round #257 (Div. 2) D. Jzzhu and Cities
- js,jquery的差别
- kinect环境配置--C++
- wikioi 1052 大顶堆
- hdu4848
- 海誓山盟都是一瞬间,我只想用真心来爱你
- CF449B-- Jzzhu and Cities
- The type java.lang.Object cannot be resolved
- mt7681 sdk GPIO操作实例讲解
- oracle在linux下开机自启动
- html5/css3-7月21日
- 学生信息管理系统之优化处理
- 去掉UILabel的黑边
- 区间DP基础篇之 POJ1159——Palindrome
- hihoCoder #1014 : Trie树 使用递归建树的方法