제출 #409541

#제출 시각아이디문제언어결과실행 시간메모리
409541CursedCodeTravelling Merchant (APIO17_merchant)C++14
21 / 100
135 ms2140 KiB
#include <bits/stdc++.h>
using namespace std;
 
long long n,m,k;
long long buy[105][1005],sell[105][1005];
long long adj[105][105],efficent[105][105];
long long profit[105][105];
const long long inf=10e15;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cin >> n >> m >> k;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= k;j++) cin >> buy[i][j] >> sell[i][j];
	}
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++) adj[i][j] = inf;
	}
	for(int i = 1;i <= m;i++){
		long long u,v,w;
		cin >> u >> v >> w;
		adj[u][v] = w;
	}
	for(int l = 1;l <= n;l++){
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= n;j++){
				adj[i][j] = min(adj[i][j] , adj[i][l] + adj[l][j]);
			}
		}
	}
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++){
			if(adj[i][j] >= inf){
				profit[i][j] = -inf;
				continue;
			}
			for(int cnt = 1;cnt <= k;cnt++){
				if(sell[j][cnt] == -1 || buy[i][cnt] == -1) continue;
				profit[i][j] = max(profit[i][j] , sell[j][cnt] - buy[i][cnt]);
			}
		}
	}
	long long lower = 0,higher = inf;
	while(lower<higher){
		long long mid = (lower + higher + 1) / 2;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= n;j++){
				if(adj[i][j] >= inf) efficent[i][j] = -inf;
				else efficent[i][j] = profit[i][j] - mid * adj[i][j];
			}
		}
		for(int l=1;l<=n;l++){
			for(int i = 1;i <= n;i++){
				for(int j = 1;j <= n;j++){
					if(adj[i][l] >= inf || adj[j][l] >= inf) continue;
					efficent[i][j] = max(efficent[i][j] , efficent[i][l] + efficent[l][j]);
				}
			}
		}
		bool check = false;
		for(int i = 1;i <= n;i++){
			if(efficent[i][i] >= 0){
				check = true;
				break;
			}
		}
		if(check) lower=mid;
		else higher=mid-1;
	}
	cout << lower << '\n';
 	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...