답안 #243917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
243917 2020-07-02T07:19:16 Z arnold518 Wild Boar (JOI18_wild_boar) C++14
100 / 100
6972 ms 412580 KB
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 4000;
const int MAXL = 1e5;
const ll INF = 4e18;

struct Edge
{
	int u, v, w, p;
};

int N, M, T, L;
vector<Edge> adj[MAXN+10], radj[MAXN+10];
Edge E[MAXN+10];
ll dist[MAXN+10][MAXN+10];
int A[MAXL+10];

struct Queue
{
	int v; ll w;
	bool operator < (const Queue &p) const { return w>p.w; }
};

struct Node
{
	int l, r; ll w;
	Node() : l(0), r(0), w(INF) {}
	Node(int l, int r, ll w) : l(l), r(r), w(w) {}
	bool operator < (const Node &p) const { return w<p.w; }
};

Node dist2[MAXN/2+10][MAXN/2+10][4];
Node tree[MAXL*4+10][4];

void add(int node)
{
	int i, j, k;
	for(i=0; i<4; i++) tree[node][i]=Node();

	vector<Node> V;
	for(i=0; i<4; i++) for(j=0; j<4; j++) if(tree[node*2][i].r!=tree[node*2+1][j].l) V.push_back({tree[node*2][i].l, tree[node*2+1][j].r, tree[node*2][i].w+tree[node*2+1][j].w});

	for(i=0; i<V.size(); i++) tree[node][0]=min(tree[node][0], V[i]);
	for(i=0; i<V.size(); i++) if(V[i].l!=tree[node][0].l && V[i].r!=tree[node][0].r) tree[node][1]=min(tree[node][1], V[i]);
	for(i=0; i<V.size(); i++) if(V[i].l!=tree[node][0].l && V[i].r!=tree[node][1].r) tree[node][2]=min(tree[node][2], V[i]);
	for(i=0; i<V.size(); i++) if(V[i].l!=tree[node][1].l && V[i].r!=tree[node][0].r) tree[node][3]=min(tree[node][3], V[i]);
}

void init(int node, int tl, int tr)
{
	if(tl==tr)
	{
		int i, j;
		for(i=0; i<4; i++) tree[node][i]=dist2[A[tl]][A[tl+1]][i];
		return;
	}
	int mid=tl+tr>>1;
	init(node*2, tl, mid);
	init(node*2+1, mid+1, tr);
	add(node);
}

void update(int node, int tl, int tr, int p)
{
	if(tr<p-1 || p<tl) return;
	if(tl==tr)
	{
		int i, j;
		for(i=0; i<4; i++) tree[node][i]=dist2[A[tl]][A[tl+1]][i];
		return;
	}
	int mid=tl+tr>>1;
	update(node*2, tl, mid, p);
	update(node*2+1, mid+1, tr, p);
	add(node);
}

int main()
{
	int i, j, k;
	scanf("%d%d%d%d", &N, &M, &T, &L);
	for(i=1; i<=M; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		adj[u].push_back({u, v, w, i*2-1}); radj[v].push_back({u, v, w, i*2-1});
		adj[v].push_back({v, u, w, i*2}); radj[u].push_back({v, u, w, i*2});
		E[i*2-1]={u, v, w, i*2-1};
		E[i*2]={v, u, w, i*2};
	}
	for(i=1; i<=N; i++) sort(adj[i].begin(), adj[i].end(), [&](const Edge &p, const Edge &q) { return p.v<q.v; });

	for(i=1; i<=2*M; i++)
	{
		for(j=1; j<=2*M; j++) dist[i][j]=INF;

		priority_queue<Queue> PQ;
		PQ.push({i, E[i].w}); dist[i][i]=E[i].w;
		while(!PQ.empty())
		{
			Queue now=PQ.top(); PQ.pop();
			if(dist[i][now.v]!=now.w) continue;
			for(auto nxt : adj[E[now.v].v]) if(nxt.v!=E[now.v].u && dist[i][nxt.p]>now.w+nxt.w)
			{
				dist[i][nxt.p]=now.w+nxt.w;
				PQ.push({nxt.p, now.w+nxt.w});
			}
		}
	}

	for(i=1; i<=N; i++)
	{
		for(j=1; j<=N; j++)
		{
			vector<Node> V;
			for(k=0; k<4; k++) dist2[i][j][k]=Node();
			for(auto it : adj[i]) for(auto jt : radj[j]) V.push_back(Node(it.v, jt.u, dist[it.p][jt.p]));

			for(k=0; k<V.size(); k++) dist2[i][j][0]=min(dist2[i][j][0], V[k]);
			for(k=0; k<V.size(); k++) if(V[k].l!=dist2[i][j][0].l && V[k].r!=dist2[i][j][0].r) dist2[i][j][1]=min(dist2[i][j][1], V[k]);
			for(k=0; k<V.size(); k++) if(V[k].l!=dist2[i][j][0].l && V[k].r!=dist2[i][j][1].r) dist2[i][j][2]=min(dist2[i][j][2], V[k]);
			for(k=0; k<V.size(); k++) if(V[k].l!=dist2[i][j][1].l && V[k].r!=dist2[i][j][0].r) dist2[i][j][3]=min(dist2[i][j][3], V[k]);
		}
	}

	//for(i=1; i<=2*M; i++) { for(j=1; j<=2*M; j++) printf("%lld ", dist[i][j]); printf("\n"); }

	//for(i=1; i<=N; i++) { for(j=1; j<=N; j++) { for(k=0; k<4; k++) printf("%lld ", dist2[i][j][k].w); printf(" / "); } printf("\n"); }

	for(i=1; i<=L; i++) scanf("%d", &A[i]);
	init(1, 1, L-1);

	while(T--)
	{
		int p, q;
		scanf("%d%d", &p, &q); A[p]=q;
		update(1, 1, L-1, p);
		if(tree[1][0].w==INF) printf("-1\n");
		else printf("%lld\n", tree[1][0].w);
	}
}

Compilation message

wild_boar.cpp: In function 'void add(int)':
wild_boar.cpp:48:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++) tree[node][0]=min(tree[node][0], V[i]);
           ~^~~~~~~~~
wild_boar.cpp:49:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++) if(V[i].l!=tree[node][0].l && V[i].r!=tree[node][0].r) tree[node][1]=min(tree[node][1], V[i]);
           ~^~~~~~~~~
wild_boar.cpp:50:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++) if(V[i].l!=tree[node][0].l && V[i].r!=tree[node][1].r) tree[node][2]=min(tree[node][2], V[i]);
           ~^~~~~~~~~
wild_boar.cpp:51:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++) if(V[i].l!=tree[node][1].l && V[i].r!=tree[node][0].r) tree[node][3]=min(tree[node][3], V[i]);
           ~^~~~~~~~~
wild_boar.cpp:42:12: warning: unused variable 'k' [-Wunused-variable]
  int i, j, k;
            ^
wild_boar.cpp: In function 'void init(int, int, int)':
wild_boar.cpp:58:10: warning: unused variable 'j' [-Wunused-variable]
   int i, j;
          ^
wild_boar.cpp:62:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
wild_boar.cpp: In function 'void update(int, int, int, int)':
wild_boar.cpp:73:10: warning: unused variable 'j' [-Wunused-variable]
   int i, j;
          ^
wild_boar.cpp:77:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:124:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(k=0; k<V.size(); k++) dist2[i][j][0]=min(dist2[i][j][0], V[k]);
             ~^~~~~~~~~
wild_boar.cpp:125:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(k=0; k<V.size(); k++) if(V[k].l!=dist2[i][j][0].l && V[k].r!=dist2[i][j][0].r) dist2[i][j][1]=min(dist2[i][j][1], V[k]);
             ~^~~~~~~~~
wild_boar.cpp:126:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(k=0; k<V.size(); k++) if(V[k].l!=dist2[i][j][0].l && V[k].r!=dist2[i][j][1].r) dist2[i][j][2]=min(dist2[i][j][2], V[k]);
             ~^~~~~~~~~
wild_boar.cpp:127:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(k=0; k<V.size(); k++) if(V[k].l!=dist2[i][j][1].l && V[k].r!=dist2[i][j][0].r) dist2[i][j][3]=min(dist2[i][j][3], V[k]);
             ~^~~~~~~~~
wild_boar.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &N, &M, &T, &L);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:135:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=L; i++) scanf("%d", &A[i]);
                      ~~~~~^~~~~~~~~~~~~
wild_boar.cpp:141:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &p, &q); A[p]=q;
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 278648 KB Output is correct
2 Correct 153 ms 278648 KB Output is correct
3 Correct 160 ms 278648 KB Output is correct
4 Correct 150 ms 278648 KB Output is correct
5 Correct 166 ms 278732 KB Output is correct
6 Correct 150 ms 278728 KB Output is correct
7 Correct 157 ms 278652 KB Output is correct
8 Correct 168 ms 278648 KB Output is correct
9 Correct 168 ms 278648 KB Output is correct
10 Correct 152 ms 278520 KB Output is correct
11 Correct 149 ms 278648 KB Output is correct
12 Correct 154 ms 278648 KB Output is correct
13 Correct 164 ms 278648 KB Output is correct
14 Correct 154 ms 278648 KB Output is correct
15 Correct 150 ms 278648 KB Output is correct
16 Correct 149 ms 278648 KB Output is correct
17 Correct 149 ms 278648 KB Output is correct
18 Correct 149 ms 278648 KB Output is correct
19 Correct 167 ms 278624 KB Output is correct
20 Correct 152 ms 278752 KB Output is correct
21 Correct 162 ms 278648 KB Output is correct
22 Correct 150 ms 278648 KB Output is correct
23 Correct 162 ms 278780 KB Output is correct
24 Correct 154 ms 278648 KB Output is correct
25 Correct 159 ms 278648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 278648 KB Output is correct
2 Correct 153 ms 278648 KB Output is correct
3 Correct 160 ms 278648 KB Output is correct
4 Correct 150 ms 278648 KB Output is correct
5 Correct 166 ms 278732 KB Output is correct
6 Correct 150 ms 278728 KB Output is correct
7 Correct 157 ms 278652 KB Output is correct
8 Correct 168 ms 278648 KB Output is correct
9 Correct 168 ms 278648 KB Output is correct
10 Correct 152 ms 278520 KB Output is correct
11 Correct 149 ms 278648 KB Output is correct
12 Correct 154 ms 278648 KB Output is correct
13 Correct 164 ms 278648 KB Output is correct
14 Correct 154 ms 278648 KB Output is correct
15 Correct 150 ms 278648 KB Output is correct
16 Correct 149 ms 278648 KB Output is correct
17 Correct 149 ms 278648 KB Output is correct
18 Correct 149 ms 278648 KB Output is correct
19 Correct 167 ms 278624 KB Output is correct
20 Correct 152 ms 278752 KB Output is correct
21 Correct 162 ms 278648 KB Output is correct
22 Correct 150 ms 278648 KB Output is correct
23 Correct 162 ms 278780 KB Output is correct
24 Correct 154 ms 278648 KB Output is correct
25 Correct 159 ms 278648 KB Output is correct
26 Correct 155 ms 278904 KB Output is correct
27 Correct 210 ms 280184 KB Output is correct
28 Correct 188 ms 280056 KB Output is correct
29 Correct 271 ms 284280 KB Output is correct
30 Correct 299 ms 284280 KB Output is correct
31 Correct 281 ms 284384 KB Output is correct
32 Correct 292 ms 284476 KB Output is correct
33 Correct 270 ms 284280 KB Output is correct
34 Correct 270 ms 284280 KB Output is correct
35 Correct 287 ms 284404 KB Output is correct
36 Correct 282 ms 284664 KB Output is correct
37 Correct 296 ms 284280 KB Output is correct
38 Correct 278 ms 284280 KB Output is correct
39 Correct 266 ms 284280 KB Output is correct
40 Correct 259 ms 284284 KB Output is correct
41 Correct 266 ms 284280 KB Output is correct
42 Correct 277 ms 284280 KB Output is correct
43 Correct 250 ms 284280 KB Output is correct
44 Correct 260 ms 284308 KB Output is correct
45 Correct 242 ms 284280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 278648 KB Output is correct
2 Correct 153 ms 278648 KB Output is correct
3 Correct 160 ms 278648 KB Output is correct
4 Correct 150 ms 278648 KB Output is correct
5 Correct 166 ms 278732 KB Output is correct
6 Correct 150 ms 278728 KB Output is correct
7 Correct 157 ms 278652 KB Output is correct
8 Correct 168 ms 278648 KB Output is correct
9 Correct 168 ms 278648 KB Output is correct
10 Correct 152 ms 278520 KB Output is correct
11 Correct 149 ms 278648 KB Output is correct
12 Correct 154 ms 278648 KB Output is correct
13 Correct 164 ms 278648 KB Output is correct
14 Correct 154 ms 278648 KB Output is correct
15 Correct 150 ms 278648 KB Output is correct
16 Correct 149 ms 278648 KB Output is correct
17 Correct 149 ms 278648 KB Output is correct
18 Correct 149 ms 278648 KB Output is correct
19 Correct 167 ms 278624 KB Output is correct
20 Correct 152 ms 278752 KB Output is correct
21 Correct 162 ms 278648 KB Output is correct
22 Correct 150 ms 278648 KB Output is correct
23 Correct 162 ms 278780 KB Output is correct
24 Correct 154 ms 278648 KB Output is correct
25 Correct 159 ms 278648 KB Output is correct
26 Correct 155 ms 278904 KB Output is correct
27 Correct 210 ms 280184 KB Output is correct
28 Correct 188 ms 280056 KB Output is correct
29 Correct 271 ms 284280 KB Output is correct
30 Correct 299 ms 284280 KB Output is correct
31 Correct 281 ms 284384 KB Output is correct
32 Correct 292 ms 284476 KB Output is correct
33 Correct 270 ms 284280 KB Output is correct
34 Correct 270 ms 284280 KB Output is correct
35 Correct 287 ms 284404 KB Output is correct
36 Correct 282 ms 284664 KB Output is correct
37 Correct 296 ms 284280 KB Output is correct
38 Correct 278 ms 284280 KB Output is correct
39 Correct 266 ms 284280 KB Output is correct
40 Correct 259 ms 284284 KB Output is correct
41 Correct 266 ms 284280 KB Output is correct
42 Correct 277 ms 284280 KB Output is correct
43 Correct 250 ms 284280 KB Output is correct
44 Correct 260 ms 284308 KB Output is correct
45 Correct 242 ms 284280 KB Output is correct
46 Correct 337 ms 290556 KB Output is correct
47 Correct 5791 ms 408300 KB Output is correct
48 Correct 5187 ms 408160 KB Output is correct
49 Correct 4298 ms 406396 KB Output is correct
50 Correct 4661 ms 407764 KB Output is correct
51 Correct 5116 ms 408448 KB Output is correct
52 Correct 3324 ms 405240 KB Output is correct
53 Correct 3323 ms 405256 KB Output is correct
54 Correct 3372 ms 405312 KB Output is correct
55 Correct 3403 ms 405380 KB Output is correct
56 Correct 3349 ms 405308 KB Output is correct
57 Correct 3271 ms 405472 KB Output is correct
58 Correct 3277 ms 405240 KB Output is correct
59 Correct 3314 ms 405300 KB Output is correct
60 Correct 3212 ms 405240 KB Output is correct
61 Correct 3030 ms 405368 KB Output is correct
62 Correct 2950 ms 405304 KB Output is correct
63 Correct 2837 ms 405216 KB Output is correct
64 Correct 1357 ms 405368 KB Output is correct
65 Correct 1340 ms 405372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 278648 KB Output is correct
2 Correct 153 ms 278648 KB Output is correct
3 Correct 160 ms 278648 KB Output is correct
4 Correct 150 ms 278648 KB Output is correct
5 Correct 166 ms 278732 KB Output is correct
6 Correct 150 ms 278728 KB Output is correct
7 Correct 157 ms 278652 KB Output is correct
8 Correct 168 ms 278648 KB Output is correct
9 Correct 168 ms 278648 KB Output is correct
10 Correct 152 ms 278520 KB Output is correct
11 Correct 149 ms 278648 KB Output is correct
12 Correct 154 ms 278648 KB Output is correct
13 Correct 164 ms 278648 KB Output is correct
14 Correct 154 ms 278648 KB Output is correct
15 Correct 150 ms 278648 KB Output is correct
16 Correct 149 ms 278648 KB Output is correct
17 Correct 149 ms 278648 KB Output is correct
18 Correct 149 ms 278648 KB Output is correct
19 Correct 167 ms 278624 KB Output is correct
20 Correct 152 ms 278752 KB Output is correct
21 Correct 162 ms 278648 KB Output is correct
22 Correct 150 ms 278648 KB Output is correct
23 Correct 162 ms 278780 KB Output is correct
24 Correct 154 ms 278648 KB Output is correct
25 Correct 159 ms 278648 KB Output is correct
26 Correct 155 ms 278904 KB Output is correct
27 Correct 210 ms 280184 KB Output is correct
28 Correct 188 ms 280056 KB Output is correct
29 Correct 271 ms 284280 KB Output is correct
30 Correct 299 ms 284280 KB Output is correct
31 Correct 281 ms 284384 KB Output is correct
32 Correct 292 ms 284476 KB Output is correct
33 Correct 270 ms 284280 KB Output is correct
34 Correct 270 ms 284280 KB Output is correct
35 Correct 287 ms 284404 KB Output is correct
36 Correct 282 ms 284664 KB Output is correct
37 Correct 296 ms 284280 KB Output is correct
38 Correct 278 ms 284280 KB Output is correct
39 Correct 266 ms 284280 KB Output is correct
40 Correct 259 ms 284284 KB Output is correct
41 Correct 266 ms 284280 KB Output is correct
42 Correct 277 ms 284280 KB Output is correct
43 Correct 250 ms 284280 KB Output is correct
44 Correct 260 ms 284308 KB Output is correct
45 Correct 242 ms 284280 KB Output is correct
46 Correct 337 ms 290556 KB Output is correct
47 Correct 5791 ms 408300 KB Output is correct
48 Correct 5187 ms 408160 KB Output is correct
49 Correct 4298 ms 406396 KB Output is correct
50 Correct 4661 ms 407764 KB Output is correct
51 Correct 5116 ms 408448 KB Output is correct
52 Correct 3324 ms 405240 KB Output is correct
53 Correct 3323 ms 405256 KB Output is correct
54 Correct 3372 ms 405312 KB Output is correct
55 Correct 3403 ms 405380 KB Output is correct
56 Correct 3349 ms 405308 KB Output is correct
57 Correct 3271 ms 405472 KB Output is correct
58 Correct 3277 ms 405240 KB Output is correct
59 Correct 3314 ms 405300 KB Output is correct
60 Correct 3212 ms 405240 KB Output is correct
61 Correct 3030 ms 405368 KB Output is correct
62 Correct 2950 ms 405304 KB Output is correct
63 Correct 2837 ms 405216 KB Output is correct
64 Correct 1357 ms 405368 KB Output is correct
65 Correct 1340 ms 405372 KB Output is correct
66 Correct 214 ms 279544 KB Output is correct
67 Correct 686 ms 319096 KB Output is correct
68 Correct 1473 ms 404480 KB Output is correct
69 Correct 1726 ms 405496 KB Output is correct
70 Correct 1841 ms 406648 KB Output is correct
71 Correct 6638 ms 408244 KB Output is correct
72 Correct 6139 ms 408176 KB Output is correct
73 Correct 4190 ms 408068 KB Output is correct
74 Correct 4318 ms 408080 KB Output is correct
75 Correct 4187 ms 407888 KB Output is correct
76 Correct 5635 ms 407372 KB Output is correct
77 Correct 6199 ms 408396 KB Output is correct
78 Correct 6972 ms 412580 KB Output is correct
79 Correct 4169 ms 407840 KB Output is correct
80 Correct 4251 ms 407940 KB Output is correct
81 Correct 4857 ms 407544 KB Output is correct
82 Correct 4141 ms 407928 KB Output is correct
83 Correct 4183 ms 407496 KB Output is correct
84 Correct 3637 ms 407988 KB Output is correct
85 Correct 2412 ms 408404 KB Output is correct