您的位置:首页 > 其它

CodeForces - 449B Jzzhu and Cities 最短路

2017-08-04 22:47 477 查看

CodeForces - 449B  Jzzhu and Cities

Jzzhu is the president of country A. There are n cities numbered from1 ton in his country. City1 is the capital of A. Also there arem roads connecting the cities. One can go from cityui tovi (and vise versa) usingthei-th road, the length of this road isxi. Finally, there arek train routes in the country. One can use thei-th train route togo from capital of the country to citysi (and vise versa), the length of this route isyi.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 everycity to the capital mustn't change.InputThe 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 integersui, vi, xi(1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109).Each of the next k lines contains two integerssi andyi(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.OutputOutput a single integer representing the maximum number of the train routes which can be closed.ExampleInput
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 2
2 1 3
2 1
2 2
2 3
Output
2
题意是一个国家有n个城市,m条路,k条铁路,城市标号1是首都,并且k条铁路都与首都相连,问在首都与各个城市最短距离不变的情况下,最多能去多少铁路。
思路:另设一个标记数组train,一开始全为0,然后把有铁路的终点标记为1,并且更新dis数组,注意可能有重复的铁路,然后用spfa求最短路,在松弛时多加一个条件,如果这个点有铁路连接,并且到这个点的最短距离能被松弛,则要去掉这条铁路,标记为0,最后k减去标记为1的就是要去掉的铁路数
开始用的是spfa队列优化,一直在T在45这个样例,搜的题解也有用这个的,为什么我的过不了,然后百度了一个spfa双队列优化,SLF:Small Label First 策略,设要加入的节点是j,队首元素为i,若dist(j)<dist(i),则将j插入队首,否则插入队尾。
#include<stdio.h>#include<queue>#include<algorithm>using namespace std;const long long  INF = 0x3f3f3f3f3f3f3f3f;const int maxn=100100;int N,M,K;struct edge{int v,w,next;}e[600200];int head[maxn],cnt;long long dis[maxn];bool vis[maxn],train[maxn];void add_edge(int u,int v,int w){e[cnt].v=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt++;}int spfa(){dis[1]=0;vis[1]=true;deque<int> q;q.push_back(1);for(int i=1;i<=K;i++){int v,x;scanf("%d%d",&v,&x);if(dis[v]>x){dis[v]=x;train[v]=true;if(!vis[v]){if(!q.empty()&&dis[v]<dis[q.front()])q.push_front(v);elseq.push_back(v);vis[v]=true;}}}while(!q.empty()){int temp=q.front();q.pop_front();vis[temp]=false;for(int i=head[temp];i!=-1;i=e[i].next){int to=e[i].v;if(dis[to]>=dis[temp]+e[i].w&&train[to]) train[to]=false;if(dis[to]>dis[temp]+e[i].w){dis[to]=dis[temp]+e[i].w;if(!vis[to]){if(!q.empty()&&dis[to]<dis[q.front()])q.push_front(to);elseq.push_back(to);vis[to]=true;}}}}int con=0;for(int i=1;i<=N;i++)if(train[i]) con++;return K-con;}int main(void){int u,v,w;scanf("%d%d%d",&N,&M,&K);cnt=0;for(int i=1;i<=N;i++){vis[i]=false;train[i]=false;dis[i]=INF;head[i]=-1;}for(int i=1;i<=M;i++){scanf("%d%d%d",&u,&v,&w);add_edge(u,v,w);add_edge(v,u,w);}printf("%d\n",spfa());return 0;}

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