Submission #106212

# Submission time Handle Problem Language Result Execution time Memory
106212 2019-04-17T12:27:22 Z polyfish Travelling Merchant (APIO17_merchant) C++14
33 / 100
97 ms 1864 KB
//Pantyhose(black) + glasses = infinity

#include <bits/stdc++.h>
using namespace std;
 
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"

const int MAX_N = 102;
const int MAX_K = 1002;
const int INF = 1e9;

struct edge {
	int u, v;
	double w;

	edge() {}

	edge(int u, int v, double w): u(u), v(v), w(w) {}
};

int n, m, k, b[MAX_N][MAX_K], s[MAX_N][MAX_K], c[MAX_N][MAX_N], mx[MAX_N][MAX_N];
vector<edge> g[MAX_N]; 
double d[MAX_N];

void read_input() {
	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];
	}
}

void floyd() {
	for (int i=1; i<=n; ++i) {
		for (int j=1; j<=n; ++j)
			c[i][j] = INF;
	}

	for (int i=1; i<=n; ++i)
		c[i][i] = 0;

	for (int i=1; i<=m; ++i) {
		int u, v;
		cin >> u >> v;
		cin >> c[u][v];
	}

	for (int k=1; k<=n; ++k) {
		for (int i=1; i<=n; ++i) {
			for (int j=1; j<=n; ++j)
				c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
		}
	}

	// debug(c[4][1]);
}

void init() {
	for (int i=1; i<=n; ++i) {
		for (int j=1; j<=n; ++j) {
			mx[i][j] = -INF;

			for (int t=1; t<=k; ++t) {
				if (b[i][t]!=-1 && s[j][t]!=-1)
					mx[i][j] = max(mx[i][j], -b[i][t] + s[j][t]);
			}
		}
	}

	// debug(mx[1][4]);
}

bool relax(edge e) {
	if (d[e.v]>d[e.u] + e.w) {
		d[e.v] = d[e.u] + e.w;
		return true;
	}

	return false;
}

bool check_negative_cycle(double x) {
	vector<edge> E;

	for (int i=1; i<=n; ++i) {
		for (int j=1; j<=n; ++j) {
			if (mx[i][j]!=-INF) {
				E.push_back(edge(i, j, x*c[i][j] - max(0, mx[i][j])));
				// if (i==4 && j==1)
				// 	debug(mx[i][j]);
			}
		}
	}

	memset(d, 0, sizeof(d));

	for (int i=0; i<=n; ++i) {
		if (i==n)
			return true;

		bool stop = true;

		for (auto e : E) {
			if (relax(e))
				stop = false;
		}

		if (stop)
			break;
	}

	return false;
}

double bisect() {
	double l = 0, r = INF;

	while (abs(l-r)>1e-2) {
		double mid = (l + r) / 2;

		if (check_negative_cycle(mid))
			l = mid;
		else
			r = mid;
	}

	return r;
}

int main() {
	#ifdef GLASSES_GIRL
		freopen(FILE_NAME".in", "r", stdin);
		freopen(FILE_NAME".out", "w", stdout);
	#endif
	ios::sync_with_stdio(0); cin.tie(0);

	read_input();
	floyd();
	init();
	// debug(check_negative_cycle(1.9));
	cout << int64_t(bisect());
}
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 1280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 896 KB Output is correct
2 Incorrect 3 ms 768 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 50 ms 1696 KB Output is correct
2 Correct 97 ms 1824 KB Output is correct
3 Correct 47 ms 1832 KB Output is correct
4 Correct 40 ms 1864 KB Output is correct
5 Correct 67 ms 1696 KB Output is correct
6 Correct 38 ms 1664 KB Output is correct
7 Correct 7 ms 896 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 17 ms 896 KB Output is correct
10 Correct 9 ms 896 KB Output is correct
11 Correct 10 ms 896 KB Output is correct
12 Correct 10 ms 1024 KB Output is correct
13 Correct 8 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 896 KB Output is correct
2 Incorrect 3 ms 768 KB Output isn't correct
3 Halted 0 ms 0 KB -