Submission #6365209
Source Code Expand
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
from heapq import *
"""
とりあえず何通りあるかを数える:各頂点を通る時点での場合の数
最後に衝突する方法を除外する:頂点での衝突、辺での衝突
辺での衝突:距離を見れば分かる
"""
MOD = 10 ** 9 + 7
N,M = map(int,input().split())
S,T = map(int,input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
u,v,d = map(int,input().split())
graph[u].append((v,d))
graph[v].append((u,d))
def dijkstra(S):
# 距離、パスの個数を返す
INF = 10 ** 15
dist = [INF] * (N+1)
cnt_path = [0] * (N+1)
cnt_path[S] = 1
q = [(0,S)] # 距離、場所
while q:
d,x = heappop(q)
if dist[x] < d:
continue
dist[x] = d
for y,dy in graph[x]:
if d + dy > dist[y]:
continue
if d + dy == dist[y]:
cnt_path[y] += cnt_path[x]
continue
cnt_path[y] = cnt_path[x]
dist[y] = d + dy
heappush(q,(d+dy,y))
return dist, cnt_path
dist_S, cnt_path_S = dijkstra(S)
dist_T, cnt_path_T = dijkstra(T)
D = dist_S[T]
# 最短路の組
answer = cnt_path_S[T] * cnt_path_T[S]
# 頂点での衝突
for x in range(1,N+1):
if dist_S[x] == dist_T[x]:
t = cnt_path_S[x] * cnt_path_T[x] % MOD
answer -= t * t % MOD
# 辺での衝突。S側がx、T側がy
for x in range(1,N+1):
for y,d in graph[x]:
dx = dist_S[x]
dy = dist_T[y]
if dx + dy + d == D and dx + d > dy and dy + d > dx:
t = cnt_path_S[x] * cnt_path_T[y] % MOD
answer -= t * t % MOD
answer %= MOD
print(answer)
Submission Info
| Submission Time | |
|---|---|
| Task | E - Avoiding Collision |
| User | maspy |
| Language | Python (3.4.3) |
| Score | 700 |
| Code Size | 1812 Byte |
| Status | AC |
| Exec Time | 1910 ms |
| Memory | 86392 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 700 / 700 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample01.txt, sample02.txt, sample03.txt, sample04.txt |
| All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, sample01.txt, sample02.txt, sample03.txt, sample04.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01.txt | AC | 1661 ms | 80648 KiB |
| 02.txt | AC | 1869 ms | 76404 KiB |
| 03.txt | AC | 1799 ms | 81180 KiB |
| 04.txt | AC | 1798 ms | 83032 KiB |
| 05.txt | AC | 1837 ms | 76204 KiB |
| 06.txt | AC | 1403 ms | 65180 KiB |
| 07.txt | AC | 1825 ms | 79064 KiB |
| 08.txt | AC | 805 ms | 48164 KiB |
| 09.txt | AC | 1757 ms | 83028 KiB |
| 10.txt | AC | 1774 ms | 86392 KiB |
| 11.txt | AC | 1816 ms | 73760 KiB |
| 12.txt | AC | 1910 ms | 73236 KiB |
| 13.txt | AC | 1736 ms | 79424 KiB |
| 14.txt | AC | 1776 ms | 79420 KiB |
| 15.txt | AC | 1824 ms | 75608 KiB |
| 16.txt | AC | 1826 ms | 76892 KiB |
| 17.txt | AC | 1834 ms | 75560 KiB |
| 18.txt | AC | 1873 ms | 76884 KiB |
| sample01.txt | AC | 18 ms | 3188 KiB |
| sample02.txt | AC | 18 ms | 3188 KiB |
| sample03.txt | AC | 18 ms | 3192 KiB |
| sample04.txt | AC | 18 ms | 3188 KiB |