Submission #58231

# Submission time Handle Problem Language Result Execution time Memory
58231 2018-07-17T08:36:08 Z 김세빈(#1689) Wild Boar (JOI18_wild_boar) C++11
12 / 100
11488 ms 1049600 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;

const ll sz = 30;

struct path{
	ll s, e, v;
	path() {}
	path(ll _v, ll _s, ll _e) { s = _s, e = _e, v = _v; }
	bool operator< (const path& p) const { return v > p.v; }
};

typedef vector <path> vp;

vp T[303030];
priority_queue <path> Q;
vp K[2020][2020];
vector <pll> V[2020];
bool chk[2020][2020];
ll L[101010];
ll n, m, k;

vp add(vp &a, vp &b)
{
	vp ret;
	ll i, j, c1, c2;
	
	for(auto &p1: a){
		for(auto &p2 : b){
			if(p1.e != p2.s){
				ret.push_back(path(p1.v + p2.v, p1.s, p2.e));
			}
		}
	}
	
	if(!ret.empty()){
		sort(ret.begin(), ret.end());
		reverse(ret.begin(), ret.end());
		
		for(i=0;i<ret.size();i++){
			c1 = c2 = 0;
			for(j=0;j<i;j++){
				if(ret[j].v == -1) continue;
				if(ret[i].s == ret[j].s && ret[i].e == ret[j].e) break;
				else if(ret[i].s == ret[j].s) c1 ++;
				else if(ret[i].e == ret[j].e) c2 ++;
			}
			if(j < i || c1 >= 2 || c2 >= 2) ret[i].v = -1;
		}
		
		for(i=0;i<ret.size();){
			if(ret[i].v == -1){
				swap(ret[i], ret.back());
				ret.pop_back();
			}
			else i ++;
		}
		
		for(;ret.size() > sz;) ret.pop_back();
	}
	
	return ret;
}

void dijkstra(ll a, ll b, ll v)
{
	path p;
	ll i;
	
	chk[a][b] = 1;
	
	Q.push(path(v, a, b));
	
	for(;!Q.empty();){
		p = Q.top(); Q.pop();
		if(chk[p.e][p.s]) continue;
		
		chk[p.e][p.s] = 1;
		K[a][p.e].push_back(path(p.v, b, p.s));
		
		for(pll &j: V[p.e]){
			if(!chk[j.first][p.e] && j.first != p.s){
				Q.push(path(p.v + j.second, p.e, j.first));
			}
		}
	}
	
	for(i=1;i<=n;i++) for(pll &j: V[i]){
		chk[i][j.first] = 0;
	}
}

void init_seg(ll p, ll s, ll e)
{
	if(s == e){
		T[p] = K[L[s]][L[s+1]];
		return;
	}
	
	init_seg(p<<1, s, (s+e>>1));
	init_seg(p<<1|1, (s+e>>1)+1, e);
	
	T[p] = add(T[p<<1], T[p<<1|1]);
}

void update(ll p, ll s, ll e, ll v)
{
	if(s == e){
		T[p] = K[L[s]][L[s+1]];
		return;
	}
	
	if(v <= (s+e>>1)) update(p<<1, s, (s+e>>1), v);
	else update(p<<1|1, (s+e>>1)+1, e, v);
	
	T[p] = add(T[p<<1], T[p<<1|1]);
}

int main()
{
	ll i, j, q, a, b, c;
	path p;
	
	scanf("%lld%lld%lld%lld", &n, &m, &q, &k);
	
	for(i=1;i<=m;i++){
		scanf("%lld%lld%lld", &a, &b, &c);
		V[a].push_back(pll(b, c));
		V[b].push_back(pll(a, c));
	}
	
	for(i=1;i<=n;i++) for(pll &t: V[i]){
		dijkstra(i, t.first, t.second);
	}
	
	for(i=1;i<=n;i++) for(j=1;j<=n;j++){
		if(!K[i][j].empty()){
			sort(K[i][j].begin(), K[i][j].end());
			reverse(K[i][j].begin(), K[i][j].end());
			for(;K[i][j].size() > sz;) K[i][j].pop_back();
		}
	}
	
	for(i=1;i<=k;i++){
		scanf("%lld", L+i);
	}
	
	init_seg(1, 1, k-1);
	
	for(;q--;){
		scanf("%lld%lld", &a, &b);
		L[a] = b;
		if(a < k) update(1, 1, k-1, a);
		if(a > 1) update(1, 1, k-1, a-1);
		
		if(T[1].empty()) printf("-1\n");
		else printf("%lld\n", T[1][0].v);
	}
	
	return 0;
}

Compilation message

wild_boar.cpp: In function 'vp add(vp&, vp&)':
wild_boar.cpp:44:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0;i<ret.size();i++){
           ~^~~~~~~~~~~
wild_boar.cpp:55:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0;i<ret.size();){
           ~^~~~~~~~~~~
wild_boar.cpp: In function 'void init_seg(ll, ll, ll)':
wild_boar.cpp:104:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  init_seg(p<<1, s, (s+e>>1));
                     ~^~
wild_boar.cpp:105:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  init_seg(p<<1|1, (s+e>>1)+1, e);
                    ~^~
wild_boar.cpp: In function 'void update(ll, ll, ll, ll)':
wild_boar.cpp:117:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  if(v <= (s+e>>1)) update(p<<1, s, (s+e>>1), v);
           ~^~
wild_boar.cpp:117:38: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  if(v <= (s+e>>1)) update(p<<1, s, (s+e>>1), v);
                                     ~^~
wild_boar.cpp:118:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  else update(p<<1|1, (s+e>>1)+1, e, v);
                       ~^~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:128:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld%lld", &n, &m, &q, &k);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:149:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", L+i);
   ~~~~~^~~~~~~~~~~~~
wild_boar.cpp:155:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 106 ms 103288 KB Output is correct
2 Correct 120 ms 103404 KB Output is correct
3 Correct 118 ms 103608 KB Output is correct
4 Correct 103 ms 103616 KB Output is correct
5 Correct 102 ms 103616 KB Output is correct
6 Correct 105 ms 103616 KB Output is correct
7 Correct 106 ms 103616 KB Output is correct
8 Correct 105 ms 103616 KB Output is correct
9 Correct 109 ms 103680 KB Output is correct
10 Correct 110 ms 103680 KB Output is correct
11 Correct 99 ms 103716 KB Output is correct
12 Correct 110 ms 103716 KB Output is correct
13 Correct 110 ms 103800 KB Output is correct
14 Correct 95 ms 103800 KB Output is correct
15 Correct 107 ms 103800 KB Output is correct
16 Correct 115 ms 103800 KB Output is correct
17 Correct 101 ms 103800 KB Output is correct
18 Correct 101 ms 103800 KB Output is correct
19 Correct 99 ms 103800 KB Output is correct
20 Correct 94 ms 103800 KB Output is correct
21 Correct 96 ms 103800 KB Output is correct
22 Correct 109 ms 103800 KB Output is correct
23 Correct 117 ms 103800 KB Output is correct
24 Correct 108 ms 103800 KB Output is correct
25 Correct 93 ms 103800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 103288 KB Output is correct
2 Correct 120 ms 103404 KB Output is correct
3 Correct 118 ms 103608 KB Output is correct
4 Correct 103 ms 103616 KB Output is correct
5 Correct 102 ms 103616 KB Output is correct
6 Correct 105 ms 103616 KB Output is correct
7 Correct 106 ms 103616 KB Output is correct
8 Correct 105 ms 103616 KB Output is correct
9 Correct 109 ms 103680 KB Output is correct
10 Correct 110 ms 103680 KB Output is correct
11 Correct 99 ms 103716 KB Output is correct
12 Correct 110 ms 103716 KB Output is correct
13 Correct 110 ms 103800 KB Output is correct
14 Correct 95 ms 103800 KB Output is correct
15 Correct 107 ms 103800 KB Output is correct
16 Correct 115 ms 103800 KB Output is correct
17 Correct 101 ms 103800 KB Output is correct
18 Correct 101 ms 103800 KB Output is correct
19 Correct 99 ms 103800 KB Output is correct
20 Correct 94 ms 103800 KB Output is correct
21 Correct 96 ms 103800 KB Output is correct
22 Correct 109 ms 103800 KB Output is correct
23 Correct 117 ms 103800 KB Output is correct
24 Correct 108 ms 103800 KB Output is correct
25 Correct 93 ms 103800 KB Output is correct
26 Correct 96 ms 104344 KB Output is correct
27 Correct 152 ms 110180 KB Output is correct
28 Correct 147 ms 110180 KB Output is correct
29 Runtime error 11488 ms 1049600 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 106 ms 103288 KB Output is correct
2 Correct 120 ms 103404 KB Output is correct
3 Correct 118 ms 103608 KB Output is correct
4 Correct 103 ms 103616 KB Output is correct
5 Correct 102 ms 103616 KB Output is correct
6 Correct 105 ms 103616 KB Output is correct
7 Correct 106 ms 103616 KB Output is correct
8 Correct 105 ms 103616 KB Output is correct
9 Correct 109 ms 103680 KB Output is correct
10 Correct 110 ms 103680 KB Output is correct
11 Correct 99 ms 103716 KB Output is correct
12 Correct 110 ms 103716 KB Output is correct
13 Correct 110 ms 103800 KB Output is correct
14 Correct 95 ms 103800 KB Output is correct
15 Correct 107 ms 103800 KB Output is correct
16 Correct 115 ms 103800 KB Output is correct
17 Correct 101 ms 103800 KB Output is correct
18 Correct 101 ms 103800 KB Output is correct
19 Correct 99 ms 103800 KB Output is correct
20 Correct 94 ms 103800 KB Output is correct
21 Correct 96 ms 103800 KB Output is correct
22 Correct 109 ms 103800 KB Output is correct
23 Correct 117 ms 103800 KB Output is correct
24 Correct 108 ms 103800 KB Output is correct
25 Correct 93 ms 103800 KB Output is correct
26 Correct 96 ms 104344 KB Output is correct
27 Correct 152 ms 110180 KB Output is correct
28 Correct 147 ms 110180 KB Output is correct
29 Runtime error 11488 ms 1049600 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 106 ms 103288 KB Output is correct
2 Correct 120 ms 103404 KB Output is correct
3 Correct 118 ms 103608 KB Output is correct
4 Correct 103 ms 103616 KB Output is correct
5 Correct 102 ms 103616 KB Output is correct
6 Correct 105 ms 103616 KB Output is correct
7 Correct 106 ms 103616 KB Output is correct
8 Correct 105 ms 103616 KB Output is correct
9 Correct 109 ms 103680 KB Output is correct
10 Correct 110 ms 103680 KB Output is correct
11 Correct 99 ms 103716 KB Output is correct
12 Correct 110 ms 103716 KB Output is correct
13 Correct 110 ms 103800 KB Output is correct
14 Correct 95 ms 103800 KB Output is correct
15 Correct 107 ms 103800 KB Output is correct
16 Correct 115 ms 103800 KB Output is correct
17 Correct 101 ms 103800 KB Output is correct
18 Correct 101 ms 103800 KB Output is correct
19 Correct 99 ms 103800 KB Output is correct
20 Correct 94 ms 103800 KB Output is correct
21 Correct 96 ms 103800 KB Output is correct
22 Correct 109 ms 103800 KB Output is correct
23 Correct 117 ms 103800 KB Output is correct
24 Correct 108 ms 103800 KB Output is correct
25 Correct 93 ms 103800 KB Output is correct
26 Correct 96 ms 104344 KB Output is correct
27 Correct 152 ms 110180 KB Output is correct
28 Correct 147 ms 110180 KB Output is correct
29 Runtime error 11488 ms 1049600 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
30 Halted 0 ms 0 KB -