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

首先我们把所有的边夹杂在一起。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. asp.net core结合NLog搭建ELK实时日志分析平台

    0.整体架构 整体架构目录:ASP.NET Core分布式项目实战-目录 一.介绍ELK 1.说明(此篇ELK采用rpm的方式安装在服务器上)-牛刀小试 承接上一篇文章的内容准备部署ELK来展示asp ...

  2. Oracle安装到Maven本地仓库

    1.由于Maven的特性,并且之前的IDE环境已帮我们集成了Maven.而现在我们需要手动安装MVN本地仓库到电脑. 将mvn绿色安装包bin路径配置到系统环境变量Path中 验证命令: mvn –v ...

  3. Winfrom Panel Scroll End 的实现

    场景:在一个panel里面有非常多的自定义绘制的控件,在拖拉滚动条的时候,控件的画面上有残影 不知道大家遇到过这种情况没,一直做web的winform经验太少,有更好的解决办法请贡献 首先放出我的解决 ...

  4. 记录Centos7搭建ftp服务器以及遇到的各种坑

    前言 今天被经理要求搭建ftp服务器,然后就去网上搜索了一下教程.搭建成功后(遇到的坑不少)特此记录一下.因为是为了记录一下整个操作流程以防以后使用所以比较啰嗦. 目录 1.安装vsftpd 2.创建 ...

  5. c语言数字图像处理(五):空间滤波

    空间滤波原理 使用大小为m*n的滤波器对大小为M*N的图像进行线性空间滤波,将滤波器模板乘以图像中对应灰度值,相加得模板中心灰度值 a = (m-1)/2, b = (n-1)/2 若f(x+s, y ...

  6. [Unity Shader] 坐标变换与法线变换及Unity5新增加的内置函数

    学习第六章Unity内置函数时,由于之前使用mul矩阵乘法时的顺序与书中不一致,导致使用内置函数时出现光照效果不一样,因此引出以下两个问题: 1 什么时候使用3x3矩阵,什么时候使用4x4矩阵? 2 ...

  7. LeetCode 193. Valid Phone Numbers

    分析 难度 易 来源 https://leetcode.com/problems/valid-phone-numbers/ 题目 Given a text file file.txt that con ...

  8. 【Python3.6】python打包成exe

    D:\python_test>pip3 install pyinstaller…………D:\python_test>pyinstaller -F -w ui.py INFO: PyInst ...

  9. 高可用OpenStack(Queen版)集群-4.keystone集群

    参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...

  10. tf导出pb文件,以及如何使用pb文件

    先罗列出来代码,有时间再解释 from tensorflow.python.framework import graph_util import tensorflow as tf def export ...