이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
template<class T> bool ckmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}
template<class T> bool ckmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
template<int SZ> struct dijkstra {
vector<pair<int, ll>> adj[SZ];
bool vis[SZ];
ll d[SZ];
void addEdge(int u, int v, ll l) {
adj[u].pb(mp(v, l));
}
ll dist(int v) {
return d[v];
}
void build(int u) {
M00(i, SZ) vis[i] = 0;
M00(i, SZ) d[i] = 1e17;
d[u] = 0;
while(1) {
pair<ll, int> t = mp(1e17, -1);
M00(i, SZ) if(!vis[i]) ckmin(t, mp(d[i], i));
if(t.s == -1 || vis[t.s]) return;
vis[t.s] = 1;
for(auto& v: adj[t.s]) if(!vis[v.f]) {
ckmin(d[v.f], d[t.s] + v.s);
}
}
}
};
const int MAX_N = 110;
const int MAX_K = 1010;
int n, m, k;
ll buy[MAX_N][MAX_K];
ll sell[MAX_N][MAX_K];
ll dist[MAX_N][MAX_N];
ll profit[MAX_N][MAX_N];
pair<ll,ll> dp[MAX_N][MAX_N][MAX_N]; // dp[u][v][s] : max efficiency u -> v, using s steps
dijkstra<MAX_N> dijk;
bool better(pair<ll,ll> p1, pair<ll,ll> p2) { // is p1 better efficiency?
// p1.f / p1.s > p2.f / p2.s
ld p1f = p1.f; ld p1s = p1.s;
ld p2f = p2.f; ld p2s = p2.s;
return p1f/p1s > p2f/p2s;
}
pair<ll,ll> sum(pair<ll,ll> p1, pair<ll,ll> p2) {
return mp(p1.f + p2.f, p1.s + p2.s);
}
int main() { FAST
cin >> n >> m >> k;
M00(i, n) M00(j, k) cin >> buy[i][j] >> sell[i][j];
M00(i, m) {
int u, v; ll d;
cin >> u >> v >> d;
u--; v--;
dijk.addEdge(u, v, d);
}
M00(i, n) {
dijk.build(i);
M00(j, n) {
dist[i][j] = dijk.dist(j);
ll val = 0;
M00(ii, k) if(buy[i][ii] != -1 && sell[j][ii] != -1) ckmax(val, -buy[i][ii]+sell[j][ii]);
if(i != j) profit[i][j] = val;
}
}
M00(i, n) {
M00(a, n) M00(b, n) dp[i][a][b] = mp(-1e17, 1);
dp[i][i][0] = mp(0, 0);
MOO(s, 1, n) {
M00(j, n) {
M00(p, n) if(p != j && dp[i][p][s-1].f != -1e17 && dist[p][j] != 1e17) {
pair<ll,ll> cand = sum(dp[i][p][s-1], mp(profit[p][j], dist[p][j]));
if(better(cand, dp[i][j][s])) dp[i][j][s] = cand;
}
}
}
}
pair<ll,ll> res = mp(-1e17, 1);
M00(i, n) {
M00(j, n) if(j != i && dist[j][i] != 1e17) {
M00(s, n) if(dp[i][j][s].f != -1e17) {
pair<ll,ll> cand = sum(dp[i][j][s], mp(profit[j][i], dist[j][i]));
if(better(cand, res)) {
res = cand;
}
}
}
}
cout << max(0LL,res.f/res.s) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |