Description

题库链接

给出一张 \(N\) 个节点, \(M\) 条边的无向图,给出起点 \(S\) 和终点 \(T\) 。询问两个人分别从 \(S\) 和 \(T\) 出发,走最短路不相遇的方案数。

\(1 \leq N \leq 100,000,1 \leq M \leq 200,000\)

Solution

记最短路长度为 \(D\) ,从 \(S\) 出发走过的路程为 \(d_1\) ,从 \(T\) 出发的走过的路程为 \(d_2\) 。值得注意的是走最短路相遇只会出现两种情况:

  1. 在某个节点相遇,此时 \(d_1=d_2=\frac{D}{2}\) ;
  2. 在某条边上相遇,记这条边为 \(e\) ,边权为 \(c\) ,连接 \(u,v\) 两个节点。此时 \({d_1}_u+{d_2}_v+c=D\) ,且 \({d_1}_u < \left\lfloor\frac{D}{2}\right\rfloor,{d_2}_v < \left\lfloor\frac{D}{2}\right\rfloor\)

那么我们跑一遍最短路计数,用总方案数减去上述不合法的情况即可。

Code

//It is made by Awson on 2018.2.2
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 100000;
const int yzd = 1e9+7;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); } int n, m, s, t, u, v, c;
struct tt {int to, next, cost; }edge[(N<<2)+5];
int path[N+5], top;
int ans1[N+5], ans2[N+5], vis[N+5], in[N+5];
LL dist1[N+5], dist2[N+5];
queue<int>Q;
vector<int>to[N+5]; void add(int u, int v, int c) {
edge[++top].to = v, edge[top].cost = c, edge[top].next = path[u], path[u] = top;
}
void SPFA(int s, LL *dist) {
dist[s] = 0; Q.push(s); vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop(); vis[u] = 0;
for (int i = path[u]; i; i = edge[i].next)
if (dist[edge[i].to] > dist[u]+edge[i].cost) {
dist[edge[i].to] = dist[u]+edge[i].cost;
if (!vis[edge[i].to]) {
vis[edge[i].to] = 1; Q.push(edge[i].to);
}
}
}
}
void topsort(int s, LL *dist, int *ans) {
for (int u = 1; u <= n; u++) {
to[u].clear();
for (int i = path[u]; i; i = edge[i].next)
if (dist[edge[i].to] == dist[u]+edge[i].cost) to[u].push_back(edge[i].to), ++in[edge[i].to];
}
ans[s] = 1; Q.push(s);
while (!Q.empty()) {
int u = Q.front(), size = to[u].size(); Q.pop();
for (int i = 0; i < size; i++) {
if (--in[to[u][i]] == 0) Q.push(to[u][i]); (ans[to[u][i]] += ans[u]) %= yzd;
}
}
}
void work() {
read(n), read(m), read(s), read(t);
for (int i = 1; i <= m; i++) {
read(u), read(v), read(c); add(u, v, c); add(v, u, c);
}
memset(dist1, 127/3, sizeof(dist1));
SPFA(s, dist1); topsort(s, dist1, ans1);
memset(dist2, 127/3, sizeof(dist2));
SPFA(t, dist2); topsort(t, dist2, ans2);
int ans = 1ll*ans1[t]*ans1[t]%yzd;
for (int u = 1; u <= n; u++) {
if (dist1[u] == dist2[u] && dist2[u]*2 == dist1[t]) (ans -= 1ll*ans1[u]*ans1[u]%yzd*ans2[u]%yzd*ans2[u]%yzd) %= yzd;
for (int i = path[u]; i; i = edge[i].next)
if (dist1[u]+edge[i].cost+dist2[edge[i].to] == dist1[t] && dist1[u]*2 < dist1[t] && dist2[edge[i].to]*2 < dist1[t])
(ans -= 1ll*ans1[u]*ans1[u]%yzd*ans2[edge[i].to]%yzd*ans2[edge[i].to]%yzd) %= yzd;
}
writeln((ans+yzd)%yzd);
}
int main() {
work();
return 0;
}

[AtCoder arc090E]Avoiding Collision的更多相关文章

  1. AtCoder ARC 090 E / AtCoder 3883: Avoiding Collision

    题目传送门:ARC090E. 题意简述: 给定一张有 \(N\) 个点 \(M\) 条边的无向图.每条边有相应的边权,边权是正整数. 小 A 要从结点 \(S\) 走到结点 \(T\) ,而小 B 则 ...

  2. 【AtCoder】ARC090

    C - Candies 前一枚举一个i,求第一行的前i个和第二行从第n个到第i个 代码 #include <bits/stdc++.h> #define fi first #define ...

  3. AtCoder Regular Contest 090 C D E F

    C - Candies 题意 求左上角走到右下角最大的数字和. 思路 直接\(dp\) Code #include <bits/stdc++.h> #define maxn 110 usi ...

  4. Extjs5.0中的新特性

    We are excited that Ext JS 5 is now available! Ext JS 5 introduces many new and exciting improvement ...

  5. 【论文阅读】Socially aware motion planning with deep reinforcement learning-annotated

    目录 摘要部分: I. Introduction 介绍 II. Background 背景 A. Collision Avoidance with DRL B. Characterization of ...

  6. 【论文阅读】DSDNet Deep Structured self-Driving Network

    前言引用 [2] DSDNet Deep Structured self-Driving Network Wenyuan Zeng, Shenlong Wang, Renjie Liao, Yun C ...

  7. 【题解】Atcoder ARC#90 E-Avoiding Collision

    自己做出来固然开心,但是越发感觉到自己写题的确是很慢很慢了……往往有很多的细节反反复复的考虑才能确定,还要加油呀~ 这道题目的突破口在于正难则反.直接求有多少不相交的不好求,我们转而求出所有相交的.我 ...

  8. track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain

    https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html The TRACE method is used to invoke a remote, ...

  9. Collider Collision 区别

    Collision 中带有碰撞的信息,例如:速度和撞击到的点 示例 void OnCollisionEnter2D(Collision2D coll) { foreach(ContactPoint c ...

随机推荐

  1. hackme.inndy.tw - pyyy - Writeup

    hackme.inndy.tw - pyyy - Writeup 0x01 反编译 1.第一次尝试的时候我直接在线反编译,部分结果如下. for (i, f) in enumerate(F): n = ...

  2. 听翁恺老师mooc笔记(2)-第一个程序--&运算符

    使用devC++写hello world 第一步:文件-新建-源代码.然后输入"输出hello world"程序: 注意:写程序时关闭中文输入法,否则语句输入的分号可能会被识别为错 ...

  3. 2017-2018-1 20155306 mypwd的实现

    2017-2018-1 20155306 mypwd的实现 一.pwd的使用 功能: Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 命令格式:pwd [选项] 命 ...

  4. MyGod--Beta版本前期报告

    下一阶段需要改进完善的功能 1.完善购买功能,商品购买后,将生成申请订单,卖家将收到提醒.卖家在完成订单后,可以选择完成订单,商品将下架. 2.完善搜索功能,将界面中的搜索功能添加进去(简单考虑只搜索 ...

  5. Linux下C编写基本的多线程socket服务器

    不想多说什么,会搜这些东西的都是想看代码的吧. 一开始不熟悉多线程的时候还在想怎么来控制一个线程的结束,后来发现原来有pthread_exit()函数可以直接在线程函数内部调用结束这个线程. 开始还想 ...

  6. 关于tomcat部署应用的三种方式

    关于tomcat部署应用虽然不是一个经常的操作,因为一旦选择了一种部署方式,我们其他的应用就会不经大脑的使用这种既定模式, 如果不使用这种部署方式,但是对于其他的部署方式不是很清楚的话,很容易抓瞎,所 ...

  7. hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken

    环境:weblogic10.3.5,hibernate3,GGTS(groovy/grails tools suite):出现这问题是因为该项目是从weblogic8.1.6下移植到weblogic1 ...

  8. 记一次SQL调优/优化(SQL tuning)——性能大幅提升千倍以上

    好久不写东西了,一直忙于各种杂事儿,恰巧昨天有个用户研发问到我一个SQL调优的问题,说性能太差,希望我能给调优下,最近有些懒,可能和最近太忙有关系,本来打算问问现在的情况,如果差不多就不调了,那哥们儿 ...

  9. 偶遇vue-awesome-swiper的坑

    最近用vue重构一个移动端的项目,碰到了不少坑,今天拿移动端最著名的轮播插件swiper为例来说,由于这个项目没用UI库,纯手写的样式,沿用老的插件,自然而然的选择了vue-awesome-swipe ...

  10. (转载) Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)

    1 . 查看当天日期 select current_date(); 2. 查看当天时间 select current_time(); 3.查看当天时间日期 select current_timesta ...