给一个无向图,外加一些特殊的连接原点的无向边。在不改变原点与所有点的最短路的情况下,最多可以删除多少条特殊边?

首先我们把所有的边夹杂在一起。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的更多相关文章

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

  2. Codeforces 665A. Buses Between Cities 模拟

    A. Buses Between Cities time limit per test: 1 second memory  limit per test: 256 megabytes input: s ...

  3. Educational Codeforces Round 12 A. Buses Between Cities 水题

    A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...

  4. Codeforces C. Jzzhu and Cities(dijkstra最短路)

    题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

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

  7. [Codeforces 864F]Cities Excursions

    Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...

  8. Codeforces 1101F Trucks and Cities dp (看题解)

    Trucks and Cities 一个很显然的做法就是二分然后对于每个车贪心取check, 这肯定会TLE, 感觉会给人一种贪心去写的误导... 感觉有这个误导之后很难往dp那个方向靠.. dp[ ...

  9. Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树

    题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...

随机推荐

  1. 可视化分析 web 访问日志

    内容目录 Python 基础 使用模块介绍 可视化组件 echarts 介绍 Web 访问日志 代码解读 讲师:KK 多语言混搭开发工程师,多年 PHP.Python 项目开发经验,曾就职 360.绿 ...

  2. python 利用urllib 获取办公区公网Ip

    import json,reimport urllib.requestdef GetLocalIP(): IPInfo = urllib.request.urlopen("http://ip ...

  3. C++将一个vector中的内容复制到另一个vector结尾

    在使用vector容器的时候,需要将一个vector中的内容复制到另一个vector结尾,如何实现呢? 使用vector的insert方法 template <class InputIterat ...

  4. 使用Xshell远程访问tensorboard

    在使用tensorflow时,由于本地资源的限制,一般在远程服务器上训练模型,而服务器没有图形界面,那么在训练过程中如何实时地访问tensorboard可视化数据呢? 如果服务器和本地电脑连接在同一个 ...

  5. Hacknet 玩后感

    这款游戏的主题是黑客模拟.玩家需要帮助雇主搞定各种乱七八糟的需求. 你需要使用各种工具和各种linux命令行来获取对方电脑的Root权限.然后就是各种增删改查... 解密部分设计的太过简单,导致玩的时 ...

  6. 在python脚本中设置环境变量,并运行相关应用

    1. 问题 在自动化应用的时候 ,有时候环境变量与运行需要不一致.这时候有两种选择: 改变节点环境变量,使得其和运行需求保持一致: 在自动化脚本中设置环境变量,其范围只在脚本运行环境中有效. 显然,当 ...

  7. Python 3 利用 Dlib 实现摄像头实时人脸检测和平铺显示

    1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...

  8. ovs源码阅读--netlink使用

    netlink netlink socket是一种用于用户态进程和内核态进程之间的通信机制.它通过为内核模块提供一组特殊的API,并为用户程序提供了一组标准的socket接口的方式,实现了全双工的通讯 ...

  9. Streamr助你掌控自己的数据(2)——三种整合数据至Streamr的典型场景

    博客说明 所有刊发内容均可转载但是需要注明出处. 三种整合数据至Streamr的典型场景 本系列文档主要介绍怎么通过Streamr管理自己的DATA,整个系列包括三篇教程文档,分别是:教你5分钟上传数 ...

  10. 【NLP】使用bert

    # 参考 https://blog.csdn.net/luoyexuge/article/details/84939755 小做改动 需要: github上下载bert的代码:https://gith ...