Codeforces 449B Jzzhu and Cities(SPFA优化)

Codeforces 449B Jzzhu and Cities(SPFA优化)

题意:

1mst1S.png

1msYp8.png

前置知识:

SPFA

讲解:https://blog.csdn.net/qq_35644234/article/details/61614581

code:

建图方法很细节(mark一手

题是spfa裸题,最后判断一下跑出来的最短路和铁路哪个近并且计数就过了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;
const long long INF=0x3f3f3f3f3f3f3f;
const int Maxn = 1e5+10;

int cnt;
vector <pair<int,int > > E[Maxn];
int n,m,k;
bool vis[Maxn];
long long dis[Maxn];
bool book[Maxn];
queue <int> q;

int main()
{
int u,v,w;
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&n,&m,&k);
for(int i=1; i<=n; i++)
dis[i]=INF;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&w);
E[u].push_back(make_pair(v,w));
E[v].push_back(make_pair(u,w));
}
q.push(1);
dis[1]=0;
vis[1]=1;
for(int i=1; i<=k; i++)
{
scanf("%d%d",&v,&w);
if(w<dis[v])
{
dis[v]=w;
vis[v]=1;
book[v]=1;
q.push(v);
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=0; i<E[u].size(); i++)
{
int v=E[u][i].first;
int w=E[u][i].second;
if(dis[v]>=dis[u]+w)
{
dis[v]=dis[u]+w;
book[v]=0;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
int ans = 0;
for(int i=1; i<=n; i++)
if(book[i]==1) ans++;
printf("%d",k-ans);
return 0;
}