Codeforces 449B_Jzzhu and Cities
给一个无向图,外加一些特殊的连接原点的无向边。在不改变原点与所有点的最短路的情况下,最多可以删除多少条特殊边?
首先我们把所有的边夹杂在一起。spfa跑出与所有点的最短路。
接下来我们通过一次bfs来判断哪些特殊的边是可以删除的。这里面的原理跟迪杰斯特拉算法差不多。
首先把原点加入队列,所有的特殊边按照长度排序,然后一直沿着非特殊边和最短的路径增广,把满足最短路条件的点都拉到队列里面来,直到队列元素为空,然后判断特殊边的最短的那一条边是否关键边,是的话就把它所连接的那个点拉到队列里面来。程序一直进行直到队列元素空且所有的特殊边都进行过判断为止。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1111111
typedef long long ll;
using namespace std; struct EG{
ll v,w;
}E[]; ll inf=~0U>>;
ll to[maxn],next[maxn],c[maxn],first[maxn],edge;
ll U[maxn],V[maxn],W[maxn],d[maxn];
ll Q[maxn],bot,top;
bool iq[maxn];
ll n,m,k,tk,ans=; bool cmp(EG e1,EG e2)
{
return e1.w<e2.w;
} void addedge(ll uu,ll vv,ll ww)
{
edge++;
to[edge]=vv,c[edge]=ww,next[edge]=first[uu],first[uu]=edge;
edge++;
to[edge]=uu,c[edge]=ww,next[edge]=first[vv],first[vv]=edge;
} void _init()
{
scanf("%I64d%I64d%I64d",&n,&m,&k);
for (ll i=; i<=n; i++) first[i]=-,d[i]=inf,iq[i]=false;
edge=-;
for (ll i=; i<=m; i++) scanf("%I64d%I64d%I64d",&U[i],&V[i],&W[i]),addedge(U[i],V[i],W[i]);
tk=edge;
for (ll i=; i<=k; i++)
{
scanf("%I64d%I64d",&E[i].v,&E[i].w);
addedge(,E[i].v,E[i].w);
}
} void SPFA()
{
Q[bot=top=]=,d[]=,iq[]=true;
while (bot<=top)
{
ll cur=Q[bot++];
iq[cur]=false;
for (ll i=first[cur]; i!=-; i=next[i])
if (d[cur]+c[i]<d[to[i]])
{
d[to[i]]=d[cur]+c[i];
if (!iq[to[i]]) Q[++top]=to[i],iq[to[i]]=true;
}
}
} void bfs()
{
ll topeg=;
sort(E+,E++k,cmp);
for (ll i=; i<=n; i++) iq[i]=false;
Q[bot=top=]=,iq[]=true;
while (bot<=top || topeg<k)
{
if (bot<=top)
{
ll cur=Q[bot++];
for (ll i=first[cur]; i!=-; i=next[i])
if (i<=tk && d[cur]+c[i]==d[to[i]] && !iq[to[i]])
iq[to[i]]=true,Q[++top]=to[i];
}
else
{
topeg++;
if (iq[E[topeg].v] || d[E[topeg].v]<E[topeg].w) ans++;
else iq[E[topeg].v]=true,Q[++top]=E[topeg].v;
}
}
} int main()
{
inf*=inf;
_init();
SPFA();
bfs();
printf("%I64d\n",ans);
return ;
}
Codeforces 449B_Jzzhu and Cities的更多相关文章
- Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities
http://codeforces.com/contest/962/problem/E E. Byteland, Berland and Disputed Cities time limit per ...
- Codeforces 665A. Buses Between Cities 模拟
A. Buses Between Cities time limit per test: 1 second memory limit per test: 256 megabytes input: s ...
- Educational Codeforces Round 12 A. Buses Between Cities 水题
A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...
- Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
- 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 864F]Cities Excursions
Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...
- Codeforces 1101F Trucks and Cities dp (看题解)
Trucks and Cities 一个很显然的做法就是二分然后对于每个车贪心取check, 这肯定会TLE, 感觉会给人一种贪心去写的误导... 感觉有这个误导之后很难往dp那个方向靠.. dp[ ...
- Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树
题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...
随机推荐
- Win7搭建FTP服务器
“控制面板” -> “程序和功能” -> “打开或关闭Windows 功能”: 1.展开“Internet 信息服务” 2.勾选“Internet Information Services ...
- c++引用与指针的关系
目录 1.引用的概念及用法 2.引用做参数 3.引用做返回值 4.汇编层看引用的特性 5.引用和指针的区别和联系: 更多内容请移步专栏:https://blog.csdn.net/column/det ...
- Unity学习笔记(3):一些常用API和应用场景
Mathf.Lerp(float a,float b,float t)插值函数,当a < b时往a中插入t,以此来实现颜色,声音等渐变效果. GameObject.FindWithTag(str ...
- static笔记
目录 1. static概括 2. static特点 1. 被static修饰的成员变量属于类,不属于这个类的某个对象. 2.被static修饰的成员可以并且建议通过类名直接访问 3. static注 ...
- mysql的安装教程-【linux】
先卸载系统自带的mysql,停止mysql:service mysql stop 1.查找以前是否装有mysql命令:rpm -qa|grep -i mysql可以看到mysql的几个包:qt-mys ...
- AI入门课程资源
企业 kaggle https://www.kaggle.com/learn/overview Google 介绍 https://developers.google.cn/machine-lea ...
- QT中的小细节
一 . QT4和QT5的区别(信号和槽):1. QT4: connect(button,SIGNAL(pressed()),this,SLOT(close())); /** * 优点 :写法简单 ...
- 10.openldap备份与恢复
备份方式 一.使用slapcat指令备份 使用slapcat备份后的数据 经过相关无用条目处理,即可实现数据上的条目备份 备份指令如下 #备份 #slapcat -v -l openldap-back ...
- idea最常使用的快捷键
撤销 反撤销 : Ctrl+Z / Ctrl+Shift+Z 删除一行 : Ctrl+Y 跳到实现类 : Ctrl+Alt+B 重命名文件: shift+F6 控制台放大缩小: ctrl+shif ...
- mysql/mybatis之合并两个表的查询结果
下面这段sql是把两个表中各自符合条件的count值相加,返回结果是两个之和 SELECT sum(result) FROM ( SELECT COUNT(*) result FROM TEST_A ...