52242023-04-23 01:24:40SleepyOverlordBarátnők (50 pont)cpp17Hibás válasz 36/50206ms18788 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

const LL INF = 1e18;
const int MOD  = 1e9 + 7;

int n, m, source, dest;
vector <VPII> g;
VLL dist, dist2, cnt, cnt2;
stack<int> order;
VB was;

void dijkstra(int start, VLL &d)
{
	set < pair <LL, int > > pq;
	d.RS(n + 1, INF);
	d[start] = 0;
	pq.INS({0, start});
	while (!pq.empty())
	{
		auto [dnode, node] = *pq.BG;
		pq.ER(pq.BG);
		for (auto [next, cost] : g[node])
			if (d[next] > dnode + cost)
			{
				pq.ER({d[next], next});
				d[next] = dnode + cost;
				pq.INS({d[next], next});
			}
	}
}

void df(int node, VLL &d)
{
	was[node] = true;
	for (auto [next, cost] : g[node])
		if (!was[next] && d[node] + cost == d[next]) df(next, d);
	order.push(node);
}

void topsort(int start, VLL &d, VLL &c)
{
	was.CL, was.RS(n + 1);
	FOR(i, 1, n)
		if (!was[i]) df(i, d);
	c.RS(n + 1);
	c[order.top()] = 1;
	while (!order.empty())
	{
		int node = order.top();
		order.pop();
		for (auto [next, cost] : g[node])
			if (d[node] + cost == d[next]) c[next] = (c[next] + c[node]) % MOD;
	}
}

int main()
{
	cin >> n >> m;
	g.RS(n + 1);
	FOR(i, 1, m)
	{
		int x, y, z;
		cin >> x >> y >> z;
		g[x].PB({y, z});
		g[y].PB({x, z});
	}
	cin >> source >> dest;
	dijkstra(source, dist);
	dijkstra(dest, dist2);
	topsort(source, dist, cnt);
	topsort(dest, dist2, cnt2);
	LL half = dist[dest] / 2;
	VLL p;
	FOR(i, 1, n)
	{
		if (dist[i] == half && dist2[i] == half) 
		{
			p.PB(cnt[i] * cnt2[i] % MOD);
		}
		for (auto [next, cost] : g[i])
		{
			if (dist[i] + dist2[next] + cost != dist[dest]) continue;
			if (dist[i] + cost > dist2[next] && dist2[next] + cost > dist[i])
			{
				p.PB(cnt[i] * cnt2[next] % MOD);
			}
		}
	}

	LL sum = 0;
	for (int x : p) sum = (sum + x) % MOD;
	LL sol = 0;
	for (LL x : p) sol = (sol + x * (sum - x)) % MOD;
	cout << sol;

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base36/50
1Elfogadva0/03ms1872 KiB
2Elfogadva0/0202ms14580 KiB
3Elfogadva2/24ms2572 KiB
4Elfogadva2/24ms2784 KiB
5Elfogadva2/23ms2936 KiB
6Hibás válasz0/24ms2984 KiB
7Hibás válasz0/24ms3008 KiB
8Elfogadva1/194ms12628 KiB
9Elfogadva2/2159ms17500 KiB
10Hibás válasz0/2165ms15064 KiB
11Hibás válasz0/2158ms15152 KiB
12Elfogadva2/2165ms17264 KiB
13Elfogadva2/2162ms15604 KiB
14Hibás válasz0/2152ms16104 KiB
15Hibás válasz0/2150ms16764 KiB
16Elfogadva1/1200ms16208 KiB
17Elfogadva1/1206ms16736 KiB
18Elfogadva1/197ms13828 KiB
19Elfogadva2/2203ms17068 KiB
20Elfogadva2/2188ms16820 KiB
21Elfogadva2/2202ms16820 KiB
22Elfogadva2/2192ms16728 KiB
23Elfogadva2/2204ms18012 KiB
24Elfogadva2/2172ms16592 KiB
25Elfogadva2/2158ms13648 KiB
26Elfogadva2/2181ms17032 KiB
27Hibás válasz0/2203ms17320 KiB
28Elfogadva2/2199ms18788 KiB
29Elfogadva2/2193ms16424 KiB