Submission #132323

#TimeUsernameProblemLanguageResultExecution timeMemory
132323bogdan10bos007 (CEOI14_007)C++14
0 / 100
7 ms5112 KiB
/// 00:04 why?
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 2e5 + 5;

int N, M, S, D, A, B;
vector<int> edg[NMAX];

vector<int> BFS(int start)
{
	vector<int> d(N + 1, 1 << 30);
	d[start] = 0;
	queue<int> q;
	q.push(start);
	while(!q.empty())
	{
		int nod = q.front();
		q.pop();
		for(auto nxt: edg[nod])
			if(d[nxt] > d[nod] + 1)
			{
				d[nxt] = d[nod] + 1;
				q.push(nxt);
			}
	}
	return d;
}

int main()
{
	freopen("1.in", "r", stdin);
	scanf("%d%d", &N, &M);
	scanf("%d%d%d%d", &S, &D, &A, &B);
	for(int i = 1; i <= M; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		edg[x].push_back(y);
		edg[y].push_back(x);
	}

	auto dS = BFS(S);
	auto dD = BFS(D);

	if(dD[A] + 1 <= dS[A] || dD[B] + 1 <= dS[B])
	{
		printf("-1\n");
		exit(0);
	}

	int timp = -1;
	if(dD[A] - dS[A] != dD[B] - dS[B])
		timp = min(dD[A] - dS[A], dD[B] - dS[B]);
	else
	{
		auto dA = BFS(A);
		auto dB = BFS(B);

		int maxStepsS = 0;
		for(int i = 1; i <= N; i++)
			if(dA[i] == dB[i] && dA[i] + dS[i] == dS[A])
				maxStepsS = max(maxStepsS, dS[i]);

		int maxStepsD = 0;
				for(int i = 1; i <= N; i++)
					if(dA[i] == dB[i] && dA[i] + dD[i] == dD[A])
						maxStepsD = max(maxStepsD, dS[i]);
		timp = min(dD[A] - dS[A], dD[B] - dS[B]);
		if(maxStepsS < maxStepsD)	timp--;
	}
	printf("%d\n", timp);
	return 0;
}

Compilation message (stderr)

007.cpp: In function 'int main()':
007.cpp:33:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  freopen("1.in", "r", stdin);
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~
007.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
007.cpp:35:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &S, &D, &A, &B);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...