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 ...
随机推荐
- Follow me to learn how to use mvc template
Introduction After having gone through many project: Project A Project B Project C I start to write ...
- UltraEdit打开UTF-8文件后显示中文乱码的问题
左图是UE,默认的编码格式GBK.右图可以看出可修改成UTF-8编码.按钮位置是UE底部状态栏,图3显示怎么开关状态栏. >>>>>>>>>> ...
- 公司mysql数据库设计与优化培训ppt
cnblogs无法上传附件. http://pan.baidu.com/s/1kVGqMn9
- 布局文件预览:Rendering Problems Exception raised during rendering: Unable to find the layout for Action Bar.的解决
在android studio或者eclipse中打开layout文件,发现不能预览布局,提示以下错误: Rendering Problems Exception raised during rend ...
- [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载、ID型别差异
[ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载.ID型别差异 原始码下载 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授 ...
- [原创]html5_PC游戏_图片俄罗斯方块
PC游戏_图片俄罗斯方块 以前的了,快一年了... 使用了离线canvas复制的方法,启动预览效果需要服务器支持 另外,AC娘图片可以自己做加载功能,这样游戏图片显示更顺畅 效果: --- 代码: h ...
- SharePoint 服务器端对象模型操作用户组(创建/添加/删除)
摘要:几个操作SharePoint用户组的方法,已经测试通过,但是没有提升权限,如果没有权限的人操作,需要提升权限(提权代码附后).大家需要的话,可以参考下,写在这里也给自己留个备份~~ //创建用户 ...
- Android多媒体录制--MediaRecorder视频录制
Android使用MediaRecorder类进行视频的录制. 需要注意,使用MediaRecorder 录音录像 的设置代码步骤一定要按照API指定的顺序来设置,否则报错 步骤为: 1.设置视频源, ...
- 提高eclipse使用效率(二) 提高Android开发效率的小技巧
XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...
- 可展开的列表组件——ExpandableListView深入解析
可展开的列表组件--ExpandableListView深入解析 一.知识点 1.ExpandableListView常用XML属性 2.ExpandableListView继承BaseExpanda ...