从S出发跑dij,从T出发跑dij,顺便最短路计数。

  令$F(x)$为$S$到$T$最短路经过$x$的方案数,显然这个是可以用$S$到$x$的方案数乘$T$到$x$的方案数来得到。

  然后第一个条件就变成了满足$F(A)+F(B)=F(T)$,这个只要用map存一下点的状态,每次查$F(T)-F(A)$就可以得到$B$的状态了。

  第二个条件实际上就是$A$无法到达$B$,怎么判断这个呢。

  按最短路正反拓扑排序两次,分别按两种拓扑序做$O(n*m/32)$的传递闭包,然后一个点两种(按拓扑序得到的能到达的点的状态的补集)的交集就是不能到达的点了。

  统计答案的时候找map里$F(T)-F(A)$的状态 & $A$两种(按拓扑序得到的能到达的点的状态的补集)的交集,用bitset::count求出有几个1就好了,记得判一下算重复的情况。

  第一次学会在map里开bitset...

  还有$S$不能到达$T$要输出$n*(n-1)/2$...= =

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<bitset>
  6. #include<queue>
  7. #include<map>
  8. #define ll long long
  9. using namespace std;
  10. const int maxn=;
  11. const ll inf=1e15;
  12. struct tjm{int too, dis, pre;}e[maxn<<];
  13. struct poi{int x; ll dis;};
  14. priority_queue<poi>q;
  15. bool operator<(poi a, poi b){return a.dis>b.dis;}
  16. map<ll,bitset<maxn> >mp;
  17. int n, m, s, t, x, y, z, tot, cnt, top;
  18. int p[maxn], last[maxn], ru[maxn], pos[maxn], st[maxn];
  19. ll ans, dist[][maxn], f[][maxn];
  20. bitset<maxn>g[][maxn];
  21. bool v[maxn];
  22. inline void read(int &k)
  23. {
  24. int f=; k=; char c=getchar();
  25. while(c<'' || c>'') c=='-' && (f=-), c=getchar();
  26. while(c<='' && c>='') k=k*+c-'', c=getchar();
  27. k*=f;
  28. }
  29. inline void add(int x, int y, int z){e[++tot]=(tjm){y, z, last[x]}; last[x]=tot;}
  30. inline void dijkstra(int x, int ty)
  31. {
  32. for(int i=;i<=n;i++) dist[ty][i]=inf;
  33. dist[ty][x]=; f[ty][x]=; q.push((poi){x, });
  34. while(!q.empty())
  35. {
  36. poi now=q.top(); q.pop();
  37. if(now.dis!=dist[ty][now.x]) continue;
  38. for(int i=last[now.x], too;i;i=e[i].pre)
  39. if(dist[ty][too=e[i].too]>dist[ty][now.x]+e[i].dis)
  40. {
  41. f[ty][too]=f[ty][x];
  42. dist[ty][too]=min(inf, dist[ty][now.x]+e[i].dis);
  43. q.push((poi){too, dist[ty][too]});
  44. }
  45. else if(dist[ty][too]==dist[ty][now.x]+e[i].dis) f[ty][too]+=f[ty][x];
  46. }
  47. }
  48. inline bool check(int x, int y, int dis, int ty){return v[y] && dist[ty][x]+dis==dist[ty][y];}
  49. inline void topsort(int ty)
  50. {
  51. memset(ru, , sizeof(ru)); top=;
  52. for(int i=;i<=cnt;i++) for(int j=last[p[i]], too;j;j=e[j].pre)
  53. if(check(p[i], too=e[j].too, e[j].dis, ty)) ru[too]++;
  54. for(int i=;i<=cnt;i++) if(!ru[p[i]]) st[++top]=p[i], pos[p[i]]=top;
  55. for(int i=;i<=top;i++)
  56. for(int j=last[st[i]], too;j;j=e[j].pre)
  57. if(check(st[i], too=e[j].too, e[j].dis, ty))
  58. {
  59. ru[too]--;
  60. if(!ru[too]) st[++top]=too, pos[too]=top;
  61. }
  62. for(int i=;i<=cnt;i++) g[ty][p[i]][p[i]-]=;
  63. for(int i=top;i;i--)
  64. for(int j=last[st[i]], too;j;j=e[j].pre)
  65. if(check(st[i], too=e[j].too, e[j].dis, ty) && pos[st[i]]<pos[too]) g[ty][st[i]]|=g[ty][too];
  66. }
  67. int main()
  68. {
  69. read(n); read(m); read(s); read(t);
  70. for(int i=;i<=m;i++) read(x), read(y), read(z), add(x, y, z), add(y, x, z);
  71. dijkstra(s, ); if(dist[][t]==inf) return printf("%lld\n", 1ll*n*(n-)>>), ;
  72. dijkstra(t, );
  73. for(int i=;i<=n;i++) if(dist[][i]+dist[][i]==dist[][t]) p[++cnt]=i, v[i]=;
  74. for(int i=;i<=cnt;i++) mp[f[][p[i]]*f[][p[i]]]|=<<(p[i]-);
  75. topsort(); topsort();
  76. for(int i=;i<=cnt;i++) ans+=(((mp[f[][t]-f[][p[i]]*f[][p[i]]])>>(i-))&(~g[][p[i]]>>(i-))&(~g[][p[i]]>>(i-))).count();
  77. ll tmp=; for(int i=;i<=cnt;i++) if(f[][p[i]]*f[][p[i]]==f[][t]) tmp++; ans+=tmp*(n-cnt);
  78. printf("%lld\n", ans);
  79. }

「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!(dij+bitset)的更多相关文章

  1. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!

    n<=50000,m<=50000的图,给s和t,问有多少点对$(a,b)$满足 嗯. 不会. 首先最短路DAG造出来,然后两个条件转述一下:条件一,$N_a$表示从s到t经过a的路径,$ ...

  2. LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset

    题目传送门 https://loj.ac/problem/6252 https://lydsy.com/JudgeOnline/problem.php?id=5109 题解 首先跑最短路,只保留 \( ...

  3. loj #6250. 「CodePlus 2017 11 月赛」找爸爸

    #6250. 「CodePlus 2017 11 月赛」找爸爸 题目描述 小 A 最近一直在找自己的爸爸,用什么办法呢,就是 DNA 比对. 小 A 有一套自己的 DNA 序列比较方法,其最终目标是最 ...

  4. [LOJ 6249]「CodePlus 2017 11 月赛」汀博尔

    Description 有 n 棵树,初始时每棵树的高度为 H_i,第 i 棵树每月都会长高 A_i.现在有个木料长度总量为 S 的订单,客户要求每块木料的长度不能小于 L,而且木料必须是整棵树(即不 ...

  5. [LOJ 6248]「CodePlus 2017 11 月赛」晨跑

    Description “无体育,不清华”.“每天锻炼一小时,健康工作五十年,幸福生活一辈子” 在清华,体育运动绝对是同学们生活中不可或缺的一部分.为了响应学校的号召,模范好学生王队长决定坚持晨跑.不 ...

  6. 「CodePlus 2017 11 月赛」Yazid 的新生舞会(树状数组/线段树)

    学习了新姿势..(一直看不懂大爷的代码卡了好久T T 首先数字范围那么小可以考虑枚举众数来计算答案,设当前枚举到$x$,$s_i$为前$i$个数中$x$的出现次数,则满足$2*s_r-r > 2 ...

  7. 「CodePlus 2017 11 月赛」可做题

    这种题先二进制拆位,显然改的位置只有每一段确定的数的开头和结尾,只需要对于每一个可决策位置都尝试一下填1和0,然后取min即可. #include<iostream> #include&l ...

  8. 「CodePlus 2017 11 月赛」Yazid 的新生舞会

    n<=500000的数字,问有多少个区间的众数出现次数严格大于区间长度的一半. 这么说来一个区间就一个众数了,所以第一反应是枚举数字,对下标进行处理.然后没有第二反应.很好. 在枚举一个数字的时 ...

  9. [LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞

    [LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞 试题描述 到河北省 见斯大林 / 在月光下 你的背影 / 让我们一起跳舞吧 うそだよ~ 河北省怎么可能有 Stalin. ...

随机推荐

  1. centos7.6 安装 openvpn--2.4.7

    openvpn-server端 搭建 1,软件版本 Centos - 7.x easy-rsa - 3.0.3 OpenVPN - 2.4.7 2,安装 建议安装启用epel源,采用yum的方式安装o ...

  2. windows下在virtualbox中的Fuel Openstack 9.0 安装过程

    一.材料: 1.软件: virtualbox xshell(或putty,winscp) bootstrap.zip(580MB) mirrors(3.01GB) MirantisOpenStack- ...

  3. eclipse以MapReduce本地模式运行程序

    1.准备好所需的文件winutils.exe.hadoop-eclipse-plugin-2.7.3.jar.hadoop-common-2.2.0-bin-master.zip 下载路径:http: ...

  4. python3 拼接字符串的7种方法

    1.直接通过(+)操作符拼接 1 2 >>> 'Hello' + ' ' + 'World' + '!' 'Hello World!' 使用这种方式进行字符串连接的操作效率低下,因为 ...

  5. scrum立会报告+燃尽图(第二周第二次)

    此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2247 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...

  6. My Sql数据库设置环境变量和字符集

    一.踩坑背景 之前开发中一直用的是sql  server 数据库,最近接到公司一个老的项目,用的my sql数据库做的,功能做了一大部分,现在客户要求对原程序和数据库进行服务器的迁移工作.产品经理给出 ...

  7. Leetcode题库——28.实现strStr()

    @author: ZZQ @software: PyCharm @file: strStr.py @time: 2018/11/6 20:04 要求:给定一个 haystack 字符串和一个 need ...

  8. SQL之联合查询学习笔记

    定义: 联合查询可合并多个相似的选择查询的结果集.等同于将一个表追加到另一个表,从而实现将两个表的查询组合到一起,使用谓词为UNION或UNION ALL. 语法格式 UNION 可以将两个或两个以上 ...

  9. T4模板_T4基本结构

    T4文本模板由 指令块.文本块.控制块 组成. 一. 指令块(MSDN文本模板指令) 指令块以@开头,基本的指令块包括<#@ template #> .<#@ parameter# ...

  10. Maven解读:强大的依赖体系

    Github地址:https://github.com/zwjlpeng/Maven_Detail Maven最大的好处就是能够很方便的管理项目对第三方Jar包的依赖,只需在Pom文件中添加几行配置文 ...