Submission #8345129


Source Code Expand

#include "bits/stdc++.h"
using namespace std;

#define ll long long 
#define ii pair<ll,ll>
const int MAXN = 1e5 + 5;
const ll MOD = 1e9 + 7, INF = 1e18;

int n, m, s, t;
ii fromS[MAXN], fromT[MAXN];
vector<ii> adj[MAXN];
ll ans;

ll add(ll a, ll b){ return (a+b+MOD)%MOD; }
ll mul(ll a, ll b){ return (a*b)%MOD; }

void dijkstra(ii d[], int start){
	for(int i = 1; i <= n; ++i){
		d[i] = ii(INF, 0);
	}
	priority_queue<ii> pq;
	pq.push(ii(0, start));
	while(!pq.empty()){
		ii top = pq.top(); pq.pop();
		ll curDist = -top.first, pos = top.second;
		if(d[pos].first <= curDist)
			continue;
		d[pos] = ii(curDist, 0);
		for(ii i : adj[pos]){
			pq.push(ii(-d[pos].first-i.second, i.first));
		}
	}

	vector<int> dist = {};
	for(int i = 1; i <= n; ++i) dist.push_back(i);
	sort(dist.begin(), dist.end(), [d](int a, int b){ return d[a].first < d[b].first; });
	d[start].second = 1;
	for(int i = 1; i < n; ++i){
		int pos = dist[i];
		for(ii j : adj[pos]){
			if(d[j.first].first+j.second == d[pos].first){
				d[pos].second = add(d[pos].second, d[j.first].second);
			}
		}
	}
}

int main(){
	scanf("%d %d %d %d", &n, &m, &s, &t);
	for(int i = 0; i < m; ++i){
		ll u, v, w;
		scanf("%lld %lld %lld", &u, &v, &w);
		adj[u].push_back(ii(v, w));
		adj[v].push_back(ii(u, w));
	}

	dijkstra(fromS, s);
	dijkstra(fromT, t);
	assert(fromS[t].second == fromT[s].second && fromS[t].first == fromT[s].first);

	// Get all paths
	ll ans = mul(fromS[t].second, fromS[t].second);
	// Minus meet at vertex
	for(int i = 1; i <= n; ++i){
		// printf("(%lld, %lld) (%lld, %lld)\n", fromS[i].first, fromS[i].second, fromT[i].first, fromT[i].second);
		if(fromS[i].first == fromT[i].first)
		if(fromS[i].first+fromT[i].first == fromS[t].first){
			ll tmp = mul(fromS[i].second, fromT[i].second);
			tmp = mul(tmp, tmp);
			// printf("Del %lld %d\n", tmp, i);
			ans = add(ans, -tmp);
		}
	}
	// Minus meet at edge
	for(int i = 1; i <= n; ++i){
		for(ii j : adj[i]){
			if(fromS[i].first != fromT[i].first && fromS[j.first].first != fromT[j.first].first)
			if(fromS[i].first + fromT[j.first].first + j.second == fromS[t].first)
			if(2*fromS[i].first < fromS[t].first && 2*fromS[j.first].first > fromS[t].first){
				ll tmp = mul(fromS[i].second, fromT[j.first].second);
				tmp = mul(tmp, tmp);
				ans = add(ans, -tmp);
			}
		}
	}
	printf("%lld\n", ans);
}

Submission Info

Submission Time
Task E - Avoiding Collision
User IgoRamli
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2426 Byte
Status AC
Exec Time 278 ms
Memory 23780 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:49:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d %d", &n, &m, &s, &t);
                                      ^
./Main.cpp:52:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld %lld", &u, &v, &w);
                                      ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 22
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 271 ms 22076 KB
02.txt AC 276 ms 23780 KB
03.txt AC 260 ms 17616 KB
04.txt AC 253 ms 17268 KB
05.txt AC 278 ms 22432 KB
06.txt AC 186 ms 14708 KB
07.txt AC 272 ms 19164 KB
08.txt AC 96 ms 11888 KB
09.txt AC 247 ms 17532 KB
10.txt AC 265 ms 17428 KB
11.txt AC 246 ms 17788 KB
12.txt AC 243 ms 17336 KB
13.txt AC 271 ms 19164 KB
14.txt AC 264 ms 19160 KB
15.txt AC 267 ms 19584 KB
16.txt AC 257 ms 19460 KB
17.txt AC 257 ms 19584 KB
18.txt AC 260 ms 19460 KB
sample01.txt AC 3 ms 4352 KB
sample02.txt AC 3 ms 4352 KB
sample03.txt AC 3 ms 4352 KB
sample04.txt AC 3 ms 4352 KB