题目地址:点击打开链接
题意:1为首都,已存在了m条路, 现在有k条铁路要建,均从首都出发,连接另一点,距离为w。现在想要节省经费,取消一些铁轨,问说最多能取消多少条铁路,要求每座城市与首都的最短距离不变。
思路:先把铁路能到的地方距离设为铁路长并做上标记,然后做一遍spfa,铁路到的点的
距离被更新,那么去除标记。最后统计剩余标记数,就为一定要保留的铁路数。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, k, book[maxn], vis[maxn];
ll dis[maxn];
struct node
{
int v, w;
node(){}
node(int vv, int ww): v(vv), w(ww) {}
};
vector<node> g[maxn];
int main(void)
{
while(cin >> n >> m >> k)
{
for(int i = 0; i < maxn; i++)
g[i].clear(), dis[i] = INF, book[i] = 0;
for(int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].push_back(node(v, w));
g[v].push_back(node(u, w));
}
dis[1] = 0;
queue<int> q;
q.push(1);
book[1] = 1;
for(int i = 1; i <= k; i++)
{
int v, w;
scanf("%d%d", &v, &w);
if(w < dis[v])
{
dis[v] = w;
vis[v] = 1;
if(!book[v])
book[v] = 1, q.push(v);
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
book[u] = 0;
for(int i = 0; i < g[u].size(); i++)
{
int v = g[u][i].v;
int w = g[u][i].w;
if(dis[u]+w <= dis[v])
{
vis[v] = 0;
dis[v] = dis[u]+w;
if(!book[v])
{
book[v] = 1;
q.push(v);
}
}
}
}
int cnt = 0;
for(int i = 1; i <= n; i++)
if(vis[i]) cnt++;
printf("%d\n", k-cnt);
}
return 0;
}
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 m roads 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