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.保证 ...
随机推荐
- 约束3:default约束
默认值约束(Default约束)的作用是在执行insert命令时,如果命令没有显式给指定的列赋值,那么把默认约束值插入到该列中:如果在Insert命令中显式为指定的列赋值,那么将该列插入用户显式指定的 ...
- mysql自动化测试第一个例子
################################################################################ # This test verifie ...
- python 中的特殊方法,纠正自己笨笨的记忆
1. __new__ 和 __init__ 的区别 python 2.x 老式类(默认继承type) class A: pass 老式类中没有__new__类方法(也就是说定义也不会执行,它不是老式类 ...
- flask的继承和包含
为了方便使用重复的页面,我们也可以使用继承模板.还有包含模板,一般使用包含,俩个都不是很好理解,我只是用完的理解简单介绍一下,他们的用法打不相同,却又有类似之处 我们访问页面的时候在最上边会有导航的信 ...
- 最安全的聊天工具——Cryptocat
关于Cryptocat Cryptocat 是啥?Cryptocat,俗称 "加密猫",是一款非常注重安全的聊天软件. 美国前中情局员工斯诺登在躲避美国政府追捕过程中,就是使用 C ...
- CentOS 6.7 安装配置 nagios
一.简介 Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警,第一时间 ...
- TIME_WAIT 你好!
[root@vm-10-124-66-212 ~]# netstat -an|awk -F ' ' '{print $NF}'|sort |uniq -c |sort -rn|more 5552 TI ...
- PSP Daily软件beta版本——基于NABCD评论,及改进建议
1.根据(不限于)NABCD评论作品的选题: 此软件的用户人群较为明确,即:用户(软件工程课上学生)记录例行报告.写每周PSP表格和统计的需求.潜在用户还有未来该课堂的学生和需要用PSP方法记录任务完 ...
- Alpha版本测试文档
概述 本次测试主要是为了测试是否有导致崩溃的bug,验证是否符合软件基本需求. 测试环境 硬件测试:安卓系统手机,安卓平板. 测试人员 赖彦谕,金哉仁. 实际进度 2015/11/6 – 2015/1 ...
- 20162319 实验二 Java面对对象程序设计 实验报告
实验二 Java面向对象程序设计 实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验 ...