Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 12:01:20
原创
1688人浏览过

英特尔AI工具
英特尔AI工具

英特尔AI与机器学习解决方案

英特尔AI工具 70
查看详情 英特尔AI工具

D. 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.

Sample test(s)

input

5 5 31 2 12 3 21 3 33 4 41 5 53 54 55 5
登录后复制

output

input

2 2 31 2 22 1 32 12 22 3
登录后复制

output


思路:这题只要引入一个最短路条数,然后再遍历火车线,如果最短路与火车线长度相等,此时如果最短路条数是1的话,那说明这个最短路就是火车线,不能去掉,如果最短路条数大于1条,说明除了这条火车线外还有别的跟他同样短的路,可以去掉;如果火车线长度比最短路长的话,显然可以去掉。

这题刚开始看确实挺蛋疼的,比赛的时候也没啥想法。好久不做图论方面的了,所以有点不会了。

刚才敲spfa还重新看了下这算法才会手敲,确实有点生了。

刚开始queue没过,T了。然后改成优先队列就过了。呵呵。没明白。不是dijkstra才得用优先队列么,这题为什么也要用。后面想想,应该是先后问题T了。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<bitset>#define mem(a,b) memset(a,b,sizeof(a))#define INF 1000000070000using namespace std;typedef long long ll;typedef unsigned long long ull;int cnt,n,head[900005],vis[900005];int repeat[900005];//记录重复的边ll dist[900005],Map[900005];struct node{    int v,next,w;} e[900005];void add(int u,int v,int w){    e[cnt].v=v;    e[cnt].w=w;    e[cnt].next=head[u];    head[u]=cnt++;}void spfa(){    int i,j;    priority_queue<int>q;    for(i=0; i<=n; i++) dist[i]=INF;    repeat[1]=1;    q.push(1);    vis[1]=1;    dist[1]=0;    while(!q.empty())    {        int u=q.top();        q.pop();        vis[u]=0;        for(i=head[u]; i!=-1; i=e[i].next)        {            if(dist[e[i].v]>dist[u]+e[i].w)            {                dist[e[i].v]=dist[u]+e[i].w;                repeat[e[i].v]=repeat[u];                if(!vis[e[i].v])                {                    vis[e[i].v]=1;                    q.push(e[i].v);                }            }            else if(dist[e[i].v]==dist[u]+e[i].w)            {                repeat[e[i].v]+=repeat[u];                if(repeat[e[i].v]>=2) repeat[e[i].v]=2;            }        }    }}int main(){    int m,k,i,j,u,v,w,sum=0;//sum表示需要保留的火车线路    mem(head,-1);    cin>>n>>m>>k;    for(i=0;i<=n;i++)        Map[i]=INF;    for(i=0; i<m; i++)    {        scanf("%d%d%d",&u,&v,&w);        add(u,v,w);        add(v,u,w);    }    for(i=0; i<k; i++)    {        scanf("%d%d",&v,&w);        if(Map[v]>w) Map[v]=w;    }    for(i=1; i<=n; i++)    {        if(Map[i]!=INF)        {            add(1,i,Map[i]);            add(i,1,Map[i]);            sum++;        }    }    spfa();    for(i=1; i<=n; i++)        if(Map[i]!=INF)        {            if(dist[i]==Map[i]&&repeat[i]==2) sum--;            else if(dist[i]<Map[i]) sum--;        }    printf("%d\n",k-sum);//总的减去保留在最短路里面的    return 0;}
登录后复制



HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号