Submission #230554

#TimeUsernameProblemLanguageResultExecution timeMemory
230554alishahali1382Travelling Merchant (APIO17_merchant)C++14
12 / 100
97 ms2168 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define upd(x, y) x=min(x, y)

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=20000000000000010LL;
const int mod=1000000007;
const int MAXN=110, MAXK=1010;

ll n, m, k, u, v, x, y, t, a, b, ans;
ll G[MAXN][MAXN], val[MAXN][MAXN];
ll B[MAXN][MAXK], S[MAXN][MAXK];
ll G2[MAXN][MAXN], dist[MAXN];

inline ll _mul(ll x, ll y){
	if (INF/x<y+5) return INF;
	return x*y;
}

bool check(ll x){
	//memset(G2, 0, sizeof(G2));
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) G2[i][j]=_mul(x, G[i][j])-val[i][j];
	//cerr<<"G2:\n";for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) cerr<<G2[i][j]<<" \n"[j==n];cerr<<'\n';
	
	memset(dist, 63, sizeof(dist));
	dist[1]=0;
	for (int shit=1; shit<=n+1; shit++){
		for (int i=1; i<=n; i++) for (int j=1; j<=n; j++){
			ll w=dist[i]+G2[i][j];
			if (w<dist[j]){
				dist[j]=w;
				if (shit==n+1) return 1;
			}
		}
	}
	for (int i=2; i<=n; i++) if (dist[i]+G2[i][1]<=0) return 1;
	return 0;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	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];
	
	memset(G, 63, sizeof(G));
	for (int i=1; i<=n; i++) G[i][i]=0;
	while (m--){
		cin>>u>>v>>x;
		upd(G[u][v], x);
	}
	for (int x=1; x<=n; x++) for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) upd(G[i][j], G[i][x]+G[x][j]);
	for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) for (int x=1; x<=k; x++)
		if (B[i][x]!=-1 && S[j][x]!=-1) val[i][j]=max(val[i][j], S[j][x]-B[i][x]);
	
	//cerr<<"val:\n";for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) cerr<<val[i][j]<<" \n"[j==n];cerr<<'\n';
	/*
	debug(check(2))
	return 0;
	*/
	
	int dwn=0, up=1000000000;
	while (up-dwn>1){
		int mid=(dwn+up)>>1;
		if (check(mid)) dwn=mid;
		else up=mid;
	}
	if (up==1000000000) dwn=-1;
	cout<<dwn<<'\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...