QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203682#4370. Road TimesPetroTarnavskyiWA 2ms3860kbC++174.7kb2023-10-06 19:24:052023-10-06 19:24:06

Judging History

你现在查看的是最新测评结果

  • [2023-10-06 19:24:06]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3860kb
  • [2023-10-06 19:24:05]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const db EPS = 1e-9;
const db LINF = 1.1e18;

const int INF = 1.01e9;

struct Simplex
{
	void pivot(int l, int e)
	{
		assert(0 <= l && l < m);
		assert(0 <= e && e < n);
		assert(abs(a[l][e]) > EPS);
		b[l] /= a[l][e];
		FOR(j, 0, n)
			if (j != e)
				a[l][j] /= a[l][e];
		a[l][e] = 1 / a[l][e];
		FOR(i, 0, m)
		{
			if (i != l)
			{
				b[i] -= a[i][e] * b[l];
				FOR(j, 0, n)
					if (j != e)
						a[i][j] -= a[i][e] * a[l][j];
				a[i][e] *= -a[l][e];
			}
		}
		v += c[e] * b[l];
		FOR(j, 0, n)
			if (j != e)
				c[j] -= c[e] * a[l][j];
		c[e] *= -a[l][e];
		swap(nonBasic[e], basic[l]);
	}
	void findOptimal()
	{
		vector<db> delta(m);
		while (true)
		{
			int e = -1;
			FOR(j, 0, n)
				if (c[j] > EPS && (e == -1 || nonBasic[j] < nonBasic[e]))
					e = j;
			if (e == -1)
				break;
			FOR(i, 0, m)
				delta[i] = a[i][e] > EPS ? b[i] / a[i][e] : LINF;
			int l = min_element(ALL(delta)) - delta.begin();
			if (delta[l] == LINF)
			{
				// unbounded
				assert(false);
			}
			pivot(l, e);
		}
	}
	void initializeSimplex(const vector<vector<db>>& _a, const vector<db>& _b, const vector<db>& _c)
	{
		m = SZ(_b);
		n = SZ(_c);
		nonBasic.resize(n);
		iota(ALL(nonBasic), 0);
		basic.resize(m);
		iota(ALL(basic), n);
		a = _a;
		b = _b;
		c = _c;
		v = 0;
		int k = min_element(ALL(b)) - b.begin();
		if (b[k] > -EPS)
			return;
		nonBasic.PB(n);
		iota(ALL(basic), n + 1);
		FOR(i, 0, m)
			a[i].PB(-1);
		c.assign(n, 0);
		c.PB(-1);
		n++;
		pivot(k, n - 1);
		findOptimal();
		if (v < -EPS)
		{
			// infeasible
			assert(false);
		}
		int l = find(ALL(basic), n - 1) - basic.begin();
		if (l != m)
		{
			int e = -1;
			while (abs(a[l][e]) < EPS)
				e++;
			pivot(l, e);
		}
		n--;
		int p = find(ALL(nonBasic), n) - nonBasic.begin();
		assert(p < n + 1);
		nonBasic.erase(nonBasic.begin() + p);
		FOR(i, 0, m)
			a[i].erase(a[i].begin() + p);
		c.assign(n, 0);
		FOR(j, 0, n)
		{
			if (nonBasic[j] < n)
				c[j] = _c[nonBasic[j]];
			else
				nonBasic[j]--;
		}
		FOR(i, 0, m)
		{
			if (basic[i] < n)
			{
				v += _c[basic[i]] * b[i];
				FOR(j, 0, n)
					c[j] -= _c[basic[i]] * a[i][j];
			}
			else
				basic[i]--;
		}
	}
	pair<vector<db>, db> simplex(const vector<vector<db>>& _a, const vector<db>& _b, const vector<db>& _c)
	{
		initializeSimplex(_a, _b, _c);
		assert(SZ(a) == m);
		FOR(i, 0, m)
			assert(SZ(a[i]) == n);
		assert(SZ(b) == m);
		assert(SZ(c) == n);
		assert(SZ(nonBasic) == n);
		assert(SZ(basic) == m);
		findOptimal();
		vector<db> x(n);
		FOR(i, 0, m)
			if (basic[i] < n)
				x[basic[i]] = b[i];
		return {x, v};
	}
private:
	int m, n;
	VI nonBasic, basic;
	vector<vector<db>> a;
	vector<db> b;
	vector<db> c;
	db v;
} simplex;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(2);
	int n;
	cin >> n;
	vector<VI> g(n, VI(n)), dist(n, VI(n)), lastEdge(n, VI(n));
	vector<pair<int, int>> edges;
	FOR(i, 0, n)
	{
		FOR(j, 0, n)
		{
			cin >> g[i][j];
			if (i == j)
				assert(g[i][j] == 0);
			else if (g[i][j] == -1)
				dist[i][j] = INF;
			else
			{
				dist[i][j] = g[i][j];
				lastEdge[i][j] = SZ(edges);
				edges.emplace_back(i, j);
			}
		}
	}
	FOR(k, 0, n)
		FOR(i, 0, n)
			FOR(j, 0, n)
			{
				if (dist[i][k] + dist[k][j] < dist[i][j])
				{
					dist[i][j] = dist[i][k] + dist[k][j];
					lastEdge[i][j] = lastEdge[k][j];
				}
			}
	int m = SZ(edges);
	vector<vector<db>> a;
	vector<db> b;
	FOR(i, 0, m)
	{
		auto [u, v] = edges[i];
		vector<db> ai(m);
		ai[i] = -1;
		a.PB(ai);
		b.PB(-g[u][v]);
		ai[i] = 1;
		a.PB(ai);
		b.PB(2 * g[u][v]);
	}
	int r;
	cin >> r;
	while (r--)
	{
		int s, d, t;
		cin >> s >> d >> t;
		vector<db> ai(m);
		while (s != d)
		{
			int e = lastEdge[s][d];
			ai[e] = 1;
			d = edges[e].F;
		}
		a.PB(ai);
		b.PB(t);
		FOR(i, 0, m)
			ai[i] *= -1;
		a.PB(ai);
		b.PB(-t);
	}
	int q;
	cin >> q;
	while (q--)
	{
		int s, d;
		cin >> s >> d;
		vector<db> c(m);
		cout << s << " " << d;
		while (s != d)
		{
			int e = lastEdge[s][d];
			c[e] = -1;
			d = edges[e].F;
		}
		cout << " " << -simplex.simplex(a, b, c).S;
		FOR(i, 0, m)
			c[i] *= -1;
		cout << " " << simplex.simplex(a, b, c).S << "\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3652kb

input:

3
0 50 -1
55 0 40
-1 40 0
1
0 2 120
3
0 1
1 2
1 0

output:

0 1 50.00 80.00
1 2 40.00 70.00
1 0 55.00 110.00

result:

ok 12 numbers

Test #2:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

3
0 50 -1
55 0 40
-1 40 0
1
0 2 90
3
0 1
1 2
1 0

output:

0 1 50.00 50.00
1 2 40.00 40.00
1 0 55.00 110.00

result:

ok 12 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3828kb

input:

3
0 50 -1
55 0 40
-1 40 0
1
0 2 180
3
0 1
1 2
1 0

output:

0 1 100.00 100.00
1 2 80.00 80.00
1 0 55.00 110.00

result:

ok 12 numbers

Test #4:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

6
0 960 -1 -1 -1 -1
-1 0 -1 -1 1000 -1
-1 1000 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 1000 0 970
-1 -1 -1 -1 -1 0
3
0 3 5900
2 3 5800
2 5 5700
6
0 1
2 1
1 4
4 3
4 5
0 5

output:

0 1 1900.00 1920.00
2 1 1800.00 1820.00
1 4 1980.00 2000.00
4 3 1980.00 2000.00
4 5 1880.00 1900.00
0 5 5800.00 5800.00

result:

ok 24 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

6
0 960 -1 -1 -1 -1
-1 0 -1 -1 1000 -1
-1 1000 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 1000 0 940
-1 -1 -1 -1 -1 0
3
0 3 5900
2 3 5800
2 5 5700
6
0 1
2 1
1 4
4 3
4 5
0 5

output:

0 1 1920.00 1920.00
2 1 1820.00 1820.00
1 4 2000.00 2000.00
4 3 1980.00 1980.00
4 5 1880.00 1880.00
0 5 5800.00 5800.00

result:

ok 24 numbers

Test #6:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

6
0 950 -1 -1 -1 -1
-1 0 -1 -1 1000 -1
-1 1000 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 1000 0 970
-1 -1 -1 -1 -1 0
3
0 3 5900
2 3 5800
2 5 5700
6
0 1
2 1
1 4
4 3
4 5
0 5

output:

0 1 1900.00 1900.00
2 1 1800.00 1800.00
1 4 2000.00 2000.00
4 3 2000.00 2000.00
4 5 1900.00 1900.00
0 5 5800.00 5800.00

result:

ok 24 numbers

Test #7:

score: 0
Accepted
time: 2ms
memory: 3676kb

input:

10
0 123 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 234 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 345 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 456 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 567 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 678 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 890 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 901 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 555
666 -1 -1 -1 -1 -1 -1 -1 -1...

output:

0 0 -0.00 0.00
0 1 216.00 246.00
0 2 450.00 714.00
0 3 1084.00 1114.00
0 4 1540.00 1570.00
0 5 2674.00 2704.00
0 6 3408.00 3438.00
0 7 4298.00 4358.00
0 8 5199.00 5542.00
0 9 5754.00 6097.00
1 0 6487.00 6517.00
1 1 -0.00 0.00
1 2 234.00 468.00
1 3 838.00 868.00
1 4 1324.00 1324.00
1 5 2428.00 2458.0...

result:

ok 400 numbers

Test #8:

score: 0
Accepted
time: 2ms
memory: 3860kb

input:

10
0 123 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 234 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 345 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 456 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 567 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 678 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 890 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 901 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 555
666 -1 -1 -1 -1 -1 -1 -1 -1...

output:

0 0 -0.00 0.00
0 1 216.00 246.00
0 2 580.00 640.00
0 3 1084.00 1114.00
0 4 1540.00 1570.00
0 5 2674.00 2704.00
0 6 3408.00 3438.00
0 7 4298.00 4358.00
0 8 5199.00 5542.00
0 9 5754.00 6097.00
1 0 6487.00 6517.00
1 1 -0.00 0.00
1 2 364.00 394.00
1 3 838.00 868.00
1 4 1324.00 1324.00
1 5 2428.00 2458.0...

result:

ok 400 numbers

Test #9:

score: 0
Accepted
time: 2ms
memory: 3688kb

input:

10
0 123 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 234 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 345 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 456 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 567 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 678 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 890 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 901 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 555
666 -1 -1 -1 -1 -1 -1 -1 -1...

output:

0 0 -0.00 0.00
0 1 245.00 246.00
0 2 609.00 640.00
0 3 1084.00 1114.00
0 4 1569.00 1570.00
0 5 2703.00 2704.00
0 6 3437.00 3438.00
0 7 4327.00 4358.00
0 8 5228.00 5541.00
0 9 5783.00 6097.00
1 0 6516.00 6517.00
1 1 -0.00 0.00
1 2 364.00 394.00
1 3 838.00 868.00
1 4 1324.00 1324.00
1 5 2457.00 2458.0...

result:

ok 400 numbers

Test #10:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

3
0 10 -1
-1 0 10
10 -1 0
3
0 2 21
1 0 21
2 1 21
3
0 1
1 2
2 0

output:

0 1 10.50 10.50
1 2 10.50 10.50
2 0 10.50 10.50

result:

ok 12 numbers

Test #11:

score: -100
Wrong Answer
time: 1ms
memory: 3724kb

input:

8
0 10 -1 -1 -1 -1 -1 -1
-1 0 10 -1 -1 -1 -1 -1
-1 -1 0 10 -1 -1 -1 -1
-1 -1 -1 0 10 -1 -1 -1
-1 -1 -1 -1 0 10 -1 -1
-1 -1 -1 -1 -1 0 10 -1
-1 -1 -1 -1 -1 -1 0 10
10 -1 -1 -1 -1 -1 -1 0
8
0 7 71
1 0 71
2 1 71
3 2 71
4 3 71
5 4 71
6 5 71
7 6 71
8
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 0

output:

0 1 10.14 10.14
1 2 10.14 10.14
2 3 10.14 10.14
3 4 10.14 10.14
4 5 10.14 10.14
5 6 10.14 10.14
6 7 10.14 10.14
7 0 10.14 10.14

result:

wrong answer 3rd numbers differ - expected: '10.1428571', found: '10.1400000', error = '0.0002817'