输入一个无向图<V,E>    V<=1e5, E<=3e5

现在另外给k条边(u=1,v=s[k],w=y[k])

问在不影响从结点1出发到所有结点的最短路的前提下,最多可以删除k条边的多少条

跑最短路的时候维护或者统计就好了

一开始用spfa.然后TLE 45...好久没写  Dij+堆优化   ...

p.s.优先队列默认大顶堆

Dij+堆优化   264ms

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <set>
  8. #include <string>
  9. using namespace std;
  10.  
  11. #define ll long long
  12. #define maxn 100010
  13. #define maxm 700010
  14.  
  15. int head[maxn];
  16. struct edge{
  17. int v,w,nxt;
  18. }e[maxm];
  19. int E;
  20. void init(){E=;memset(head,-,sizeof(head));}
  21. void addedge(int u,int v,int w){
  22. e[E].v=v,e[E].w=w,e[E].nxt=head[u];
  23. head[u]=E++;
  24. }
  25. priority_queue<pair<ll,int> >que;
  26. bool vis[maxn];
  27. //ll d[maxn];
  28. int main(){
  29. int n,m,k;
  30. while(~scanf("%d%d%d",&n,&m,&k)){
  31. init();
  32. for(int i=;i<m;++i){
  33. int u,v,w;
  34. scanf("%d%d%d",&u,&v,&w);
  35. addedge(u,v,w);
  36. addedge(v,u,w);
  37. }
  38. que.push(make_pair(0ll,));
  39. for(int i=;i<k;++i){
  40. int s,y;
  41. scanf("%d%d",&s,&y);
  42. que.push(make_pair(0ll-y,-s));
  43. }
  44. memset(vis,false,sizeof(vis));
  45. int ans=;
  46. while(!que.empty()){
  47. pair<ll,int> tmp = que.top();que.pop();
  48. ll dis = -tmp.first;
  49. int u = tmp.second;
  50. if(u<){
  51. u=-u;
  52. if(vis[u])++ans;
  53. }
  54. if(vis[u]) continue;
  55. vis[u]=true;
  56. //d[u] = dis;
  57. for(int i=head[u];i!=-;i=e[i].nxt)
  58. if(vis[e[i].v]==false)
  59. que.push(make_pair( -dis-e[i].w, e[i].v));
  60. }
  61. printf("%d\n",ans);
  62. }
  63. return ;
  64. }

spfa  TLE 45

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <set>
  8. #include <string>
  9. using namespace std;
  10.  
  11. #define ll long long
  12. #define maxn 100010
  13. #define maxm 700010
  14.  
  15. int head[maxn];
  16. struct edge{
  17. int v,w,nxt;
  18. int k;
  19. }e[maxm];
  20. int E;
  21. void init(){E=;memset(head,-,sizeof(head));}
  22. void addedge(int u,int v,int w,int kind){
  23. e[E].v=v,e[E].w=w,e[E].nxt=head[u];
  24. e[E].k = kind;
  25. head[u]=E++;
  26. }
  27. int q[maxn];
  28. bool inq[maxn];
  29. bool bus[maxn];
  30. ll dis[maxn];
  31. void spfa(){
  32. int hd=,tl=;
  33. q[tl++]=;
  34. memset(dis,0x3f,sizeof(dis));
  35. dis[]=;
  36. memset(inq,false,sizeof(inq));
  37. inq[]=true;
  38. memset(bus,false,sizeof(bus));
  39. while(hd!=tl){
  40. int u = q[hd++];
  41. if(hd==maxn)hd=;
  42. inq[u] = false;
  43. for(int i=head[u];i!=-;i=e[i].nxt){
  44. int v = e[i].v, w = e[i].w, k = e[i].k;
  45. if(dis[u]+w < dis[v]){
  46. dis[v] = dis[u]+w;
  47. bus[v] = false;
  48. if(k==) bus[v]=true;
  49. if(inq[v]==false){
  50. inq[v]=true;
  51. q[tl++]=v;
  52. if(tl==maxn)tl=;
  53. }
  54. }else if(dis[u]+w == dis[v]){
  55. if(k==) bus[v]=true;
  56. }
  57. }
  58. }
  59. }
  60. int si[],yi[];
  61. int main(){
  62. int n,m,k;
  63. while(~scanf("%d%d%d",&n,&m,&k)){
  64. init();
  65. for(int i=;i<m;++i){
  66. int u,v,w;
  67. scanf("%d%d%d",&u,&v,&w);
  68. addedge(u,v,w,);
  69. addedge(v,u,w,);
  70. }
  71. for(int i=;i<k;++i){
  72. scanf("%d%d",si+i,yi+i);
  73. addedge(,si[i],yi[i],);
  74. }
  75. spfa();
  76. int ans=;
  77. for(int i=;i<k;++i){
  78. int v = si[i];
  79. if(dis[v] < yi[i]) ++ans;
  80. else {
  81. if(bus[v]==false)bus[v]=true;
  82. else ++ans;
  83. }
  84. }
  85. printf("%d\n",ans);
  86. }
  87. return ;
  88. }

codeforces 449B Jzzhu and Cities (Dij+堆优化)的更多相关文章

  1. [Codeforces 449B] Jzzhu and Cities

    [题目链接] https://codeforces.com/contest/449/problem/B [算法] 最短路 时间复杂度 : O(N ^ 2) [代码] #include<bits/ ...

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

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

  3. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  4. dij+堆优化

    写这个dij+堆优化的原因是有些地方卡SPFA,只能搞这个: 香甜的奶油: #include<iostream> #include<cstdio> #include<cs ...

  5. 迪杰斯特拉算法(Dijkstra) (基础dij+堆优化) BY:优少

    首先来一段百度百科压压惊... 迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最 ...

  6. Codeforces 450D Jzzhu and Cities [heap优化dij]

    #include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...

  7. Codeforces449A Jzzhu and Chocolate && 449B Jzzhu and Cities

    CF挂0了,简直碉堡了.两道题都是正确的思路但是写残了.写个解题报告记录一下心路历程. A题问的是 一个n*m的方块的矩形上切k刀,最小的那一块最大可以是多少.不难发现如果纵向切k1刀,横向切k2刀, ...

  8. dijkstra最短路算法(堆优化)

    这个算法不能处理负边情况,有负边,请转到Floyd算法或SPFA算法(SPFA不能处理负环,但能判断负环) SPFA(SLF优化):https://www.cnblogs.com/yifan0305/ ...

  9. Codeforces 449 B. Jzzhu and Cities

    堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...

随机推荐

  1. 关于shell脚本时遇value too great for base (error token is "08")

    今天在书写一个定时cp脚本时遇到了一个问题,value too great for base (error token is "08") 在网上查看到原来是以0开头的数字 系统会默 ...

  2. 前端入门级之如何从零开始前端(估计要被人鄙视成LOW货了)入门篇

    <!------------------------------------------------------基本说明开始----------------------------------- ...

  3. XMAL语法系列之-(2)---WPF控件继承图

    WPF控件继承图 1 FrameworkElement 1.1 Panel(面板类元素) 1.1.1 Canvas 1.1.2 DockPanel 1.1.3 Grid 1.1.4 TabPanel ...

  4. WCF--提示:"未找到终结点。"

    刚开始调用WCF的时候一直报错... ““System.ServiceModel.EndpointNotFoundException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进 ...

  5. EF方便的添加一条信息...

    //刚开始通过EF添加数据都是这样的...↓ var db = new DBEntities() T_User t_userinfo = new T_User() { Type = "typ ...

  6. POJ 2155 Matrix

    二维树状数组....                          Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  7. PHP函数preg_replace() 正则替换所有符合条件的字符串

    PHP preg_replace() 正则替换,与JavaScript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. preg_replace (正则表达式 ...

  8. java执行顺序

    本文讨论Java中(静态)变量.(静态)代码块的执行顺序 首先创建3个类: 1.Foo类,用于打印变量 public class Foo { public Foo(String word) { Sys ...

  9. 【转】phpcms授课学习

    来自:http://blog.csdn.net/yanhui_wei/article/category/1220735 <?php 思路: 一.目前在企业中使用比较多的cms内容管理有如下几种: ...

  10. Linux学习:用yum安装php,httpd,mysql

    见鸟哥的linux私房菜电子版832页.