Submission #58534

# Submission time Handle Problem Language Result Execution time Memory
58534 2018-07-18T05:53:26 Z ksun48 Wild Boar (JOI18_wild_boar) C++14
47 / 100
18000 ms 50492 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const LL MAXD = (1LL << 60LL);
int n, m, t, l;
vector<int> a, b;
vector<LL> c;
typedef pair<LL, pair<int,int> > Path;
typedef vector<Path> OptPaths;
int unused = 3000;

Path new_unused(){
	Path x = {MAXD, {unused,unused}};
	unused ++;
	return x;
}

Path alt_path(pair<int,int> avoid, OptPaths paths){
	for(Path x : paths){
		if(x.second.first == avoid.first) continue;
		if(x.second.second == avoid.second) continue;
		return x;
	}
	return new_unused();
}

OptPaths process(OptPaths cand){
	sort(cand.begin(), cand.end());
	OptPaths edges;
	if(cand.size() == 0) return edges;
	pair<int,int> x = cand[0].second;
	edges.push_back(cand[0]);
	pair<int,int> y;
	for(int i = 0; i < cand.size(); i++){
		if(cand[i].second == x) continue;
		y = cand[i].second;
		edges.push_back(cand[i]);
		break;
	}
	if(edges.size() == 1){
		return edges;
	}

	if(x.first != y.first && x.second != y.second){
		edges.push_back(alt_path({x.first, y.second}, cand));
		edges.push_back(alt_path({y.first, x.second}, cand));
	} else if(x.first == y.first){
		Path q = alt_path({x.first, -1}, cand);
		edges.push_back(q);
		edges.push_back(alt_path({x.first, q.second.second}, cand));
	} else if(x.second == y.second){
		Path q = alt_path({-1, x.second}, cand);
		edges.push_back(q);
		edges.push_back(alt_path({q.second.first, x.second}, cand));
	}
	sort(edges.begin(), edges.end());
	return edges;
}

OptPaths join(OptPaths a, OptPaths b){
	OptPaths cand;
	for(auto x : a){
		for(auto y : b){
			if(x.second.second != y.second.first){
				cand.push_back({min(x.first + y.first, MAXD), {x.second.first, y.second.second}});
			}
		}
	}
	return cand;
}

OptPaths edge(int id, LL cost){
	OptPaths x;
	x.push_back({cost, {id, id}});
	return x;
}

vector<vector<OptPaths> > cachedpaths;
OptPaths best_paths(int s, int t){
	if(cachedpaths[s].empty()){
		// start bellman-ford at s
		vector<OptPaths> sssp(n);
		vector<vector<int> > adj(n); // to, id, cost
		for(int i = 0; i < m; i++){
			if(a[i] == s){
				sssp[b[i]] = edge(i, c[i]);
			} else if(b[i] == s){
				sssp[a[i]] = edge(i, c[i]);
			} else {
				adj[a[i]].push_back(i);
				adj[b[i]].push_back(i);
			}
		}
		int vis[n][4];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				vis[i][j] = 0;
			}
		}
		set<pair<LL,pair<int,int> > > stuff;
		for(int i = 0; i < n; i++){
			for(int idx = 0; idx < sssp[i].size(); idx++){
				stuff.insert({sssp[i][idx].first, {i, idx}});
			}
		}
		while(!stuff.empty()){
			pair<LL,pair<int,int> > v = *stuff.begin();
			stuff.erase(stuff.begin());
			vis[v.second.first][v.second.second] = 1;
			for(auto i : adj[v.second.first]){
				if(b[i] == v.second.first){
					swap(a[i], b[i]);
				}
				OptPaths e = edge(i, c[i]);
				OptPaths x = join(sssp[a[i]], e);
				x.insert(x.end(), sssp[b[i]].begin(), sssp[b[i]].end());
				for(int idx = 0; idx < sssp[b[i]].size(); idx++){
					stuff.erase({sssp[b[i]][idx].first, {b[i], idx}});
				}
				sssp[b[i]] = process(x);
				for(int idx = 0; idx < sssp[b[i]].size(); idx++){
					if(!vis[b[i]][idx]){
						stuff.insert({sssp[b[i]][idx].first, {b[i], idx}});
					}
				}
			}
		}
		cachedpaths[s] = sssp;
	}
	return cachedpaths[s][t];
}

int main(){
	cin.sync_with_stdio(0); cin.tie(0);
	cin >> n >> m >> t >> l;
	cachedpaths.resize(n);
	a.resize(m); b.resize(m); c.resize(m);
	for(int i = 0; i < m; i++){
		cin >> a[i] >> b[i] >> c[i];
		a[i]--; b[i]--;
	}
	vector<int> plan(l);
	for(int i = 0; i < l; i++){
		cin >> plan[i];
		plan[i]--;
	}
	vector<pair<int,int> > updates(t);
	for(int i = 0; i < t; i++){
		cin >> updates[i].first >> updates[i].second;
		updates[i].first--; updates[i].second--;
	}
	/*for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			cout << "paths from " << i << " to " << j << '\n';
			if(i != j){
				OptPaths x = best_paths(i, j);
				for(auto v : x){
					cout << v.second.first << " " << v.second.second << " " << v.first << '\n';
				}
			}
		}
	}*/
	for(int tt = 0; tt < t; tt++){
		plan[updates[tt].first] = updates[tt].second;
		OptPaths route = best_paths(plan[0], plan[1]);
		for(int j = 2; j < l; j++){
			route = process(join(route, best_paths(plan[j-1], plan[j]) ) );
		}
		cout << (route.size() > 0 && route[0].first < MAXD ? route[0].first : -1) << '\n';
	}
}

Compilation message

wild_boar.cpp: In function 'OptPaths process(OptPaths)':
wild_boar.cpp:35:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < cand.size(); i++){
                 ~~^~~~~~~~~~~~~
wild_boar.cpp: In function 'OptPaths best_paths(int, int)':
wild_boar.cpp:103:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int idx = 0; idx < sssp[i].size(); idx++){
                     ~~~~^~~~~~~~~~~~~~~~
wild_boar.cpp:118:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int idx = 0; idx < sssp[b[i]].size(); idx++){
                      ~~~~^~~~~~~~~~~~~~~~~~~
wild_boar.cpp:122:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int idx = 0; idx < sssp[b[i]].size(); idx++){
                      ~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 488 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 3 ms 488 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 3 ms 620 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 4 ms 620 KB Output is correct
10 Correct 3 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 3 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 0 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 3 ms 748 KB Output is correct
23 Correct 3 ms 748 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 3 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 488 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 3 ms 488 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 3 ms 620 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 4 ms 620 KB Output is correct
10 Correct 3 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 3 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 0 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 3 ms 748 KB Output is correct
23 Correct 3 ms 748 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 3 ms 748 KB Output is correct
26 Correct 14 ms 748 KB Output is correct
27 Correct 236 ms 2044 KB Output is correct
28 Correct 269 ms 2044 KB Output is correct
29 Correct 514 ms 2172 KB Output is correct
30 Correct 152 ms 2172 KB Output is correct
31 Correct 132 ms 2172 KB Output is correct
32 Correct 139 ms 2172 KB Output is correct
33 Correct 907 ms 3472 KB Output is correct
34 Correct 753 ms 3500 KB Output is correct
35 Correct 146 ms 3500 KB Output is correct
36 Correct 123 ms 3500 KB Output is correct
37 Correct 780 ms 3500 KB Output is correct
38 Correct 1096 ms 5288 KB Output is correct
39 Correct 116 ms 5288 KB Output is correct
40 Correct 959 ms 5288 KB Output is correct
41 Correct 936 ms 5384 KB Output is correct
42 Correct 157 ms 5384 KB Output is correct
43 Correct 1135 ms 7584 KB Output is correct
44 Correct 1136 ms 7584 KB Output is correct
45 Correct 1093 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 488 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 3 ms 488 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 3 ms 620 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 4 ms 620 KB Output is correct
10 Correct 3 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 3 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 0 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 3 ms 748 KB Output is correct
23 Correct 3 ms 748 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 3 ms 748 KB Output is correct
26 Correct 14 ms 748 KB Output is correct
27 Correct 236 ms 2044 KB Output is correct
28 Correct 269 ms 2044 KB Output is correct
29 Correct 514 ms 2172 KB Output is correct
30 Correct 152 ms 2172 KB Output is correct
31 Correct 132 ms 2172 KB Output is correct
32 Correct 139 ms 2172 KB Output is correct
33 Correct 907 ms 3472 KB Output is correct
34 Correct 753 ms 3500 KB Output is correct
35 Correct 146 ms 3500 KB Output is correct
36 Correct 123 ms 3500 KB Output is correct
37 Correct 780 ms 3500 KB Output is correct
38 Correct 1096 ms 5288 KB Output is correct
39 Correct 116 ms 5288 KB Output is correct
40 Correct 959 ms 5288 KB Output is correct
41 Correct 936 ms 5384 KB Output is correct
42 Correct 157 ms 5384 KB Output is correct
43 Correct 1135 ms 7584 KB Output is correct
44 Correct 1136 ms 7584 KB Output is correct
45 Correct 1093 ms 12280 KB Output is correct
46 Correct 354 ms 12280 KB Output is correct
47 Correct 277 ms 12280 KB Output is correct
48 Correct 200 ms 12280 KB Output is correct
49 Correct 324 ms 12280 KB Output is correct
50 Correct 222 ms 12280 KB Output is correct
51 Correct 188 ms 12280 KB Output is correct
52 Execution timed out 18073 ms 50492 KB Time limit exceeded
53 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 488 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 3 ms 488 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 3 ms 620 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 4 ms 620 KB Output is correct
10 Correct 3 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 3 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 0 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 3 ms 620 KB Output is correct
22 Correct 3 ms 748 KB Output is correct
23 Correct 3 ms 748 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 3 ms 748 KB Output is correct
26 Correct 14 ms 748 KB Output is correct
27 Correct 236 ms 2044 KB Output is correct
28 Correct 269 ms 2044 KB Output is correct
29 Correct 514 ms 2172 KB Output is correct
30 Correct 152 ms 2172 KB Output is correct
31 Correct 132 ms 2172 KB Output is correct
32 Correct 139 ms 2172 KB Output is correct
33 Correct 907 ms 3472 KB Output is correct
34 Correct 753 ms 3500 KB Output is correct
35 Correct 146 ms 3500 KB Output is correct
36 Correct 123 ms 3500 KB Output is correct
37 Correct 780 ms 3500 KB Output is correct
38 Correct 1096 ms 5288 KB Output is correct
39 Correct 116 ms 5288 KB Output is correct
40 Correct 959 ms 5288 KB Output is correct
41 Correct 936 ms 5384 KB Output is correct
42 Correct 157 ms 5384 KB Output is correct
43 Correct 1135 ms 7584 KB Output is correct
44 Correct 1136 ms 7584 KB Output is correct
45 Correct 1093 ms 12280 KB Output is correct
46 Correct 354 ms 12280 KB Output is correct
47 Correct 277 ms 12280 KB Output is correct
48 Correct 200 ms 12280 KB Output is correct
49 Correct 324 ms 12280 KB Output is correct
50 Correct 222 ms 12280 KB Output is correct
51 Correct 188 ms 12280 KB Output is correct
52 Execution timed out 18073 ms 50492 KB Time limit exceeded
53 Halted 0 ms 0 KB -