CF449B Jzzhu and Cities (最短路)
CF449B CF450D
http://codeforces.com/contest/450/problem/D
http://codeforces.com/contest/449/problem/B
Codeforces Round #257 (Div. 2) D
Codeforces Round #257 (Div. 1) B
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 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. 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
Output
Input
Output
|
题意:有n个城市,1是首都。给出m条有权无向边(公路),k条由1连接到某个城市的有权无向边(铁路),求在保持首都到各个城市的最短路长度不变的情况下,最多能炸掉多少条铁路。
题解:首都到达同一个城市的铁路只保留最短的,然后进行最短路并统计某个顶点最短路的更新次数,最后只保留长度等于最短路且更新次数为1(只有这一种最短路)的铁路。
设一个c[i]记录i点的更新次数,初始c[首都]为1,其他为0。更新的时候dij和spfa不是小于才更新嘛,小于的时候就c[新点]=c[当前点],等于的时候就c[新点]+=c[当前点],这样c[i]就是最短路的更新次数(最短路的方案数)。
注意CF可是大家都能出数据的,有人出了个卡SPFA的数据,我都吓尿了。可以给SPFA加SLF优化过。有人用优先队列过的,因为还好没人出卡优先队列SPFA的数据…
代码:
- //#pragma comment(linker, "/STACK:102400000,102400000")
- #include<cstdio>
- #include<cmath>
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<map>
- #include<set>
- #include<stack>
- #include<queue>
- using namespace std;
- #define ll long long
- #define usll unsigned ll
- #define mz(array) memset(array, 0, sizeof(array))
- #define minf(array) memset(array, 0x3f, sizeof(array))
- #define REP(i,n) for(i=0;i<(n);i++)
- #define FOR(i,x,n) for(i=(x);i<=(n);i++)
- #define RD(x) scanf("%d",&x)
- #define RD2(x,y) scanf("%d%d",&x,&y)
- #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
- #define WN(x) prllf("%d\n",x);
- #define RE freopen("D.in","r",stdin)
- #define WE freopen("1biao.out","w",stdout)
- #define mp make_pair
- #define pb push_back
- const ll INF=1LL<<;
- const int maxn=;
- const int maxm=;
- struct edge {
- int v,next;
- ll w;
- } e[maxm];///边表
- int head[maxn],en;
- void add(int x,int y,ll z) {
- e[en].w=z;
- e[en].v=y;
- e[en].next=head[x];
- head[x]=en++;
- }
- int n,m,k;
- ll g[maxn];
- bool f[maxn];///入队标志
- int b[maxn], c[maxn];
- ll d[maxn];///b为循环队列,d为起点到各点的最短路长度
- void spfa() { ///0~n-1,共n个点,起点为st
- int i,k;
- int st=, l=, r=;
- memset(f,,sizeof(f));
- memset(b,,sizeof(b));
- for(i=; i<n; i++)
- d[i]=INF;
- b[]=st;
- f[st]=;
- d[st]=;
- c[st]=;
- while(l!=r) {
- k=b[l++];
- l%=n;
- for(i=head[k]; i!=-; i=e[i].next)
- if (d[k]+e[i].w < d[e[i].v]) {
- d[e[i].v]=d[k] + e[i].w;
- c[e[i].v]=c[k];
- if (!f[e[i].v]) {
- if(d[e[i].v]>d[b[l]]) {///SLF优化,这题卡没优化的SPFA……
- b[r++]=e[i].v;
- r%=n;
- } else {
- l--;
- if(l==-)l=n-;
- b[l]=e[i].v;
- }
- f[e[i].v]=;
- }
- } else if(d[k]+e[i].w == d[e[i].v])
- c[e[i].v]+=c[k];
- f[k]=;
- }
- }
- void init() {
- memset(head,-,sizeof(head));
- en=;
- }
- int main() {
- int i,x,y;
- ll z;
- while(scanf("%d%d%d",&n,&m,&k)!=EOF) {
- init();
- REP(i,m) {
- scanf("%d%d%I64d",&x,&y,&z);
- x--;
- y--;
- add(x,y,z);
- add(y,x,z);
- }
- REP(i,n) g[i]=INF;
- REP(i,k) {
- scanf("%d%I64d",&x,&z);
- x--;
- if(z<g[x]) g[x]=z;
- }
- REP(i,n)
- if(g[i]!=INF) {
- add(,i,g[i]);
- add(i,,g[i]);
- }
- memset(c,,sizeof(c));
- spfa();
- int remain=;
- REP(i,n)
- if(g[i]!=INF && c[i]== && d[i]==g[i])
- remain++;
- printf("%d\n",k-remain);
- }
- return ;
- }
CF449B Jzzhu and Cities (最短路)的更多相关文章
- CF449B Jzzhu and Cities 迪杰斯特拉最短路算法
CF449B Jzzhu and Cities 其实这一道题并不是很难,只是一个最短路而已,请继续看我的题解吧~(^▽^) AC代码: #include<bits/stdc++.h> #d ...
- Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路
D. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)
D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...
- Codeforces 449 B. Jzzhu and Cities
堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...
- D. Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 ...
- codeforces 449B Jzzhu and Cities (Dij+堆优化)
输入一个无向图<V,E> V<=1e5, E<=3e5 现在另外给k条边(u=1,v=s[k],w=y[k]) 问在不影响从结点1出发到所有结点的最短路的前提下,最多可以 ...
- Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA
题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路 分析:先求一次最短路,铁路 ...
- Jzzhu and Cities
CF #257 div2D:http://codeforces.com/contest/450/problem/D 题意:给你n个城市,m条无向有权边.另外还有k条边,每条边从起到到i.求可以删除这k ...
随机推荐
- CentOS加载U盘
概述: 把CentOS设置成了启动进入命令行,结果不知道在哪儿找U盘了,于是搜集了一些命令. 1. 查看分区信息,以确定那个是U盘 使用root执行fdisk -l,确定U盘是sdb1 2. 挂载U盘 ...
- C# 字符串处理
1.比较字符串 String 类提供了一系列的方法用于字符串的比较,如CompareTo 和 Equals方法等. ① CompareTo : 如果参数的值与此实例相等,则返回0:如果此实例大于参数 ...
- “K米” 软件产品评测
第一部分 调研,评测 评测: 第一次上手体验:KTV相信很多人都有去过,大部分包厢只有哦一个点歌台,相信很多人都会烦恼于一堆人挤在小小的点歌台前点歌的样子,还有些人不太好意思跑到点歌台点歌,常常是碰到 ...
- Objective-C学习笔记之NSData、NSDate
NSData和NSMutableData存储的是二进制数据,在文件操作,网络,以及核心图形图像中使用较广泛.NSData创建后不可以再修改,NSMutableData可以再次修 NSString *s ...
- Objective-C复合
正所谓复合,便是定义的这个类中的成员是另外类的实例方法. 也就是把其他对象作为自身的题部分,以提升自身的功能, 就相当于C语言中的函数嵌套.下面是一段代码(多个文件放在一块了): /***Comput ...
- npm run-script
package.json "scripts": { "start": "electron .", "package": ...
- JQuery遍历方法$.each输出函数
each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSON 等等在javaScript开发过程中使用$ ...
- Wget下载终极用法和15个详细的例子
Wget下载终极用法和15个详细的例子 备注:wget 不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...
- 按照网上方法js删除指定cookie,却怎么也删除不了,解决如下
网上方法: 查找原因说是没有指定Path,记得系统里以前也没指定还是可以的,就查了一下现在的系统Path,猜测是系统Path由以前的/改为/E7-Planning 就改了前端删除方法 测试一下OK了, ...
- Spring MVC学习笔记——给Controller和视图传值
一.给Controller传值,值将显示在控制台 1.第一种:使用@RequestParam,改HelloController.java //RequestMapping表示用哪一个url来对应 @R ...