Submission #57311

# Submission time Handle Problem Language Result Execution time Memory
57311 2018-07-14T14:20:34 Z MatheusLealV Travelling Merchant (APIO17_merchant) C++17
12 / 100
37 ms 2168 KB
#include <bits/stdc++.h>
#define N 150
#define K 1050
#define f first
#define s second
using namespace std;
#define int long long
typedef long long ll;
typedef pair<ll, ll> pii;

ll inf = 2000000000000000000;

bool can[N][N];

ll n, m, k, dist[N][N], best_cost[N][N], B[N][K], S[N][K];

ll E[N][N], dp[N][N];

vector< pii > grafo[N];

void dfs(int x, int ini)
{
	for(auto v: grafo[x])
	{
		if(can[ini][v.f]) continue;

		can[ini][v.f] = 1;

		dfs(v.f, ini);
	}
}

ll find_ciclo()
{
	ll ans = 0;

	for(int i = 1; i <= n; i++)
	{
		can[i][i] = true;

		dfs(i, i);
	}

	for(int i = 1; i <= n; i++)
	{
		if(can[1][i] and can[i][1])
		{
			ll cima = best_cost[1][i];

			ll baixo = dist[i][1] + dist[1][i];

			if(baixo != 0) ans = max(ans, cima/baixo);
		}
	}

	return ans;
}

void dijkstra(int ini)
{
	priority_queue < pii, vector< pii >, greater < pii > > pq;

	for(int i = 1; i <= n; i++) dist[ini][i] = inf;

	dist[ini][ini] = 0;

	pq.push({0, ini});

	while(!pq.empty())
	{
		int x = pq.top().s;

		ll d = pq.top().f;

		pq.pop();

		if(d > dist[ini][x]) continue;

		for(auto v: grafo[x])
		{
			if(dist[ini][v.f] > dist[ini][x] + v.s)
			{
				dist[ini][v.f] = dist[ini][x] + v.s;

				pq.push({dist[ini][v.f], v.f});
			}
		}
	}
}

bool solve(ll x)
{	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			for(int q = 1; q <= k; q++)
			{
				if(B[i][q] == -1 or S[j][q] == -1) continue;

				ll C = -B[i][q] + S[j][q];

				best_cost[i][j] = max(best_cost[i][j], C);
			}

			//cout<<"CUSTO "<<i<<" "<<j<<" "<<best_cost[i][j]<<"\n";
		}
	}

	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			dp[i][j] = inf;

	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			if(dist[i][j] == inf) continue;

			E[i][j] = -best_cost[i][j] + x*dist[i][j];

			dp[i][j] = E[i][j];

			//cout<<i<<" "<<j<<" "<<dp[i][j]<<"\n";
		}

		dp[i][i] = inf;
	}

	for(int q = 1; q <= n; q++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				dp[i][j] = min(dp[i][j], dp[i][q] + dp[q][j]);

	for(int i = 1; i <= n; i++)
	{
		if(dp[i][i] <= 0)
		{
			//cout<<"CICLO NEGATIVO "<<i<<"\n";

			return true;
		}
	}

	return false;
}	

int32_t main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n>>m>>k;

	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= k; j++) cin>>B[i][j]>>S[i][j];

	for(int i = 1, a, b; i <= m; i++)
	{
		ll c;

		cin>>a>>b>>c;

		grafo[a].push_back({b, c});
	}

	for(int i = 1; i <= n; i++) dijkstra(i);

	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			for(int q = 1; q <= k; q++)
			{
				if(B[i][q] == -1 or S[j][q] == -1) continue;

				ll C = -B[i][q] + S[j][q];

				best_cost[i][j] = max(best_cost[i][j], C);
			}

			//cout<<"CUSTO "<<i<<" "<<j<<" "<<best_cost[i][j]<<"\n";
		}
	}

	cout<<find_ciclo()<<"\n";

	return 0;

	ll ini = 0LL, fim = 10000000000LL, mid, best;

	while(fim >= ini)
	{
		mid = (ini + fim)/2;

		if(solve(mid))
		{
			best = mid;

			ini = mid + 1;
		}

		else fim = mid - 1;
	}

	cout<<best<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 2168 KB Output is correct
2 Correct 4 ms 2168 KB Output is correct
3 Correct 4 ms 2168 KB Output is correct
4 Correct 4 ms 2168 KB Output is correct
5 Correct 3 ms 2168 KB Output is correct
6 Correct 3 ms 2168 KB Output is correct
7 Correct 3 ms 2168 KB Output is correct
8 Correct 3 ms 2168 KB Output is correct
9 Correct 3 ms 2168 KB Output is correct
10 Correct 3 ms 2168 KB Output is correct
11 Correct 3 ms 2168 KB Output is correct
12 Correct 3 ms 2168 KB Output is correct
13 Correct 3 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2168 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 2168 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2168 KB Output isn't correct
2 Halted 0 ms 0 KB -