Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA
题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路
分析:先求一次最短路,铁路的权值大于该点最短距离的显然可以删去,否则将该条边加入图中,再求最短路,记录每个点的前一个点,然后又枚举铁路,已经删去的就不用处理了,如果铁路权值大于该点最短距离又可以删去,权值相等时,该点的前一个点如果不为1,则这个点可以由其他路到达,这条铁路又可以删去。
由于本题中边比较多,最多可以有8x10^5条,所以用邻接链表会超时,最好用vector来存储边。
Time:296 ms Memory:30968 KB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#define lll __int64
using namespace std;
#define N 100006 struct Edge
{
int v;
lll w;
Edge(int _v,lll _w)
{
v = _v;
w = _w;
}
Edge(){}
}; struct Train
{
int v;
lll w;
}T[N]; vector<Edge> G[N];
lll dis[N];
int n,m;
int head[N],tot,pre[N];
int cut[N],vis[N];
queue<int> que; void SPFA(int s)
{
while(!que.empty())
que.pop();
memset(vis,,sizeof(vis));
vis[s] = ;
que.push(s);
for(int i=;i<=n;i++)
dis[i] = 1000000000000000LL;
dis[s] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].v;
lll w = G[u][i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!vis[v])
{
vis[v] = ;
que.push(v);
}
}
}
}
} void SPFA2()
{
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].v;
lll w = G[u][i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
pre[v] = u;
if(!vis[v])
{
que.push(v);
vis[v] = ;
}
}
else if(dis[v] == dis[u] + w && pre[v] < u)
pre[v] = u;
}
}
} int main()
{
int i,j,k;
int u,v,y;
lll w;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
tot = ;
for(i=;i<=n;i++)
G[i].clear();
memset(head,-,sizeof(head));
memset(cut,,sizeof(cut));
memset(pre,-,sizeof(pre));
for(i=;i<m;i++)
{
scanf("%d%d%I64d",&u,&v,&w);
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
for(i=;i<k;i++)
{
scanf("%d%I64d",&y,&w);
T[i].v = y;
T[i].w = w;
}
SPFA();
int cnt = ;
for(i=;i<k;i++)
{
int v = T[i].v;
lll w = T[i].w;
if(dis[v] <= w)
{
cut[i] = ;
cnt++;
}
else
{
G[].push_back(Edge(v,w));
G[v].push_back(Edge(,w));
pre[v] = ;
dis[v] = w;
que.push(v);
vis[v] = ;
}
}
SPFA2();
for(i=;i<k;i++)
{
if(cut[i])
continue;
int v = T[i].v;
lll w = T[i].w;
if((dis[v] == w && pre[v] != ) || dis[v] < w)
cnt++;
}
printf("%d\n",cnt);
}
return ;
}
Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA的更多相关文章
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)
题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- Codeforces Round #257 (Div. 2) B Jzzhu and Sequences
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...
- Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate
C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- Android性能优化之一:ViewStub
ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...
- ASP.NET数据绑定技术
1.DataBinder.Eval()方法 DataBinder.Eval()方法是ASP.NET框架支持的一个静态方法,用来计算Late_Bound(后期绑定)数据绑定表达式,并随时将结果转换为字符 ...
- 关于HTML的编码问题
平时我在写html文件时,很容易忘掉这个文件的编码类型,<meta charset=”utf-8”> 的语句,因为编辑器默认设置了一个编码,所以在我没有写编码格式设置语句的情况下,效果依然 ...
- oracle10g 统计信息查看、收集
1. 统计信息查看 1.1 单个表的全局统计信息.统计效果查看 2. 统计信息分析(收集) 2.1 分析工具选择 2.2 分析前做index重建 2.3 分析某数据表,可以在PL/SQL的comm ...
- An unexpected error has occurred" error appears when you try to create a SharePoint Enterprise Search Center on a Site Collection
The Enterprise Search Center requires that the Publishing feature be enabled. To enable the Publishi ...
- VC连接mysql数据库错误:libmysql.lib : fatal error LNK1113: invalid machine 解决方法
VC连接MySQL的配置过程在上一篇博文中,不过当你设置好,以为万事大吉的时候,运行却出现这个错误:libmysql.lib : fatal error LNK1113: invalid machin ...
- IOS 杂笔- 6(KVC-KVO)
kvc: 键值编码的基本概念 1:键值编码是一个用于间接访问对象属性的机制,使用该机制不需要调用存取方法和变量实例就可以访问对象属性. 2:键值编码方法在Objective-C非正式协(类别)NSKe ...
- Swift学习--闭包的简单使用(三)
一.Swift中闭包的简单使用 override func viewDidLoad() { super.viewDidLoad() /** 闭包和OC中的Block非常相似 OC中的block类似于匿 ...
- android 定制自己的日志工具
最理想的情况是能够控制日志的打印,当程序处于开发阶段就让日志打印出来,当程序上线之后就把日志屏蔽掉. 例如打印一行WARN级别的日志就可以写成这样: LogUtil.w("TAG" ...
- python 读写、创建 文件
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...