您的位置:首页 > 其它

CodeForces 449B - Jzzhu and Cities(最短路)

2017-01-18 22:29 375 查看
B. 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 ≤ 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

Output a single integer representing the maximum number of the train routes which can be closed.

Examples

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 22 1 3
2 1
2 22 3


output
2


/*
题目链接:http://codeforces.com/problemset/problem/450/D

题目大意: 给定一个无向图,其中保证从起点1开始到其他的点 i 有路相连。
边都为双向的。 有n条普通边,k条点1与其他点相连的特殊边。
现在问,最多能删除多少条特殊的边,使得这个无向图从起点1出发到其他任意点的最短路径不变。

-----------------------
有一个我的理解
就是 最短路,联通,然后 当前点所连的、通向起点的那条边 只需要1条就行了,尽量让这条边为普通边
类似的题目 AIZU 2249   https://vjudge.net/problem/Aizu-2249 -----------------
先来一遍最短路 在这过程中  看代码
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<LL, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;

struct edge{
int v, w, mark;
edge(){}
edge(int v_, int w_, int mark_):v(v_), w(w_), mark(mark_){}
};
vector<edge>G[maxn];
LL dis[maxn];
int flag[maxn];
int vis[maxn];
int n, m, k, cnt;

void dijkstra(int s)
{
priority_queue<P, vector<P>, greater<P> >q;
cnt = 0; // cnt记录所需要用到的特殊边数
memset(flag, 0, sizeof(flag)); // flag[i]记录点i之前所用的边是否为特殊边
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
dis[i] = INF;
dis[s] = 0;
q.push(P(dis[s], s));
while (!q.empty()){
P cur = q.top(); q.pop();
LL w = cur.first;
int u = cur.second;
if (vis[u]) continue;
vis[u] = 1;
if (dis[u] < w) continue;
for (int i = 0; i < G[u].size(); i++){
edge e = G[u][i];
if (dis[e.v] > dis[u] + e.w){
dis[e.v] = dis[u] + e.w;
q.push(P(dis[e.v], e.v));
if (e.mark){ // 如果当前边为特殊边
if (!flag[e.v]){ // 在这之前所用的边不为特殊边
cnt++;   // 就要用到这条特殊边了 加 1
flag[e.v] = 1; // 标记当前点用了特殊边了
}
//					else 的话 就说明在这之前所用的边已经为特殊边了  cnt不变
}
else if (flag[e.v]){ // 当前边为普通边 但是之前用了特殊边
flag[e.v] = 0;  // 就减掉
cnt--;
}
}
else if (dis[e.v] == dis[u] + e.w){// 如果相等的话
if (!flag[e.v]) continue; // 尽量用普通边 不用特殊边
if (e.mark) continue;
flag[e.v] = 0;
cnt--;
}
}
}
}
int main(void)
{
//	freopen("in.txt","r", stdin);
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= m; i++){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
G[u].push_back(edge(v, w, 0)); // 普通边标记为0
G[v].push_back(edge(u, w, 0));
}
for (int i = 1; i <= k; i++){
int v, w;
scanf("%d %d", &v, &w);
G[1].push_back(edge(v, w, 1)); // 特殊边标记为1
G[v].push_back(edge(1, w, 1));
}
dijkstra(1);
cout << k - cnt << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: