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

首先我们把所有的边夹杂在一起。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. 阿超的烦恼 javaScript篇

    幼儿园级1-100随机数运算 实现目标 1.点击随机生成两个数并进项随机的四则运算. 2.答案暂时隐藏,等孩子做完题后点击答案处显示答案. 3.背景设置的卡通些,激发孩子阅读兴趣........... ...

  2. 「专题训练」Collecting Bugs(POJ-2096)

    题意与分析 题意大致是这样的:给定一个\(n\times s\)的矩阵,每次可以随机的在这个矩阵内给一个格子染色(染过色的仍然可能被选中),问每一行和每一列都有格子被染色的次数的期望. 这题如果从概率 ...

  3. 【赛后补题】Lucky Probability(CodeForces 110D)

    题意 给定两个\(P,Q\)的正整数区间(\(P,Q\)都符合\([L,R]\)这个区间,并且都\(\le 10^9\)),分别从其中随机选出一个数,选出的两个数作为一个新区间的左右端点.要求新区间内 ...

  4. 011 --Mysql中特定查询

    1.优化COUNT()查询 COUNT()可能是被大家误解最多的函数了,它有两种不同的作用,其一是统计某个列值的数量,其二是统计行数.统计列值时,要求列值是非空的,它不会统计NULL.如果确认括号中的 ...

  5. import 导入包的特别用法总结

    指定别名 可以为包指定一个别名,以便记忆或提高输入效率 如 import str "strings" 在使用的时候可以直接使用别名,如原先要写成strings.Contains,现 ...

  6. Mysql数据库的隔离级别

    Mysql数据库的隔离级别有四种 1.read umcommitted   读未提交(当前事务可以读取其他事务没提交的数据,会读取到脏数据) 2.read committed 读已提交(当前事务不能读 ...

  7. JVM新生代老年代详解

    1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...

  8. js备忘录5

    函数的全解析 原文链接: http://mp.weixin.qq.com/s?src=11&timestamp=1509672643&ver=491&signature=9fD ...

  9. java第三次实验

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1352       姓名:陈实  学号:20135224 成绩:             指导 ...

  10. 每日Scrum(10)

    今天我们小组整合了下我们所编辑的程序,然后在界面上进行了修改和少部分的完善,现在就等着下午的验收了 任务展板 燃尽图如下: