답안 #132265

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
132265 2019-07-18T15:39:43 Z gs14004 Wild Boar (JOI18_wild_boar) C++17
62 / 100
18000 ms 537464 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
const int MAXL = 100005;
using lint = long long;
using pi = pair<lint, int>;

struct edg{
	int pos, idx, cost;
};

int n, m, q, l;
vector<edg> gph[MAXN];
lint dist[MAXN * 2][MAXN * 2];

struct path_info{
	int midl, midr;
	lint cost;
	bool operator<(const path_info &p)const{
		return cost < p.cost;
	}
};

vector<path_info> plist[MAXN][MAXN];
int ed[MAXN * 2], cost[MAXN * 2];

void preprocess(){
	for(int i=0; i<m; i++){
		int s, e, x; scanf("%d %d %d",&s,&e,&x);
		ed[2 * i] = e; ed[2 * i + 1] = s;
		cost[2 * i] = cost[2 * i + 1] = x;
		gph[s].push_back({e, i*2, x});
		gph[e].push_back({s, i*2+1, x});
	}
	memset(dist, 0x3f, sizeof(dist));
	for(int i=0; i<2*m; i++){
		dist[i][i] = cost[i];
		priority_queue<pi, vector<pi>, greater<pi> > pq;
		pq.emplace(cost[i], i);
		int vis[MAXN] = {};
		while(!pq.empty()){
			auto x = pq.top(); pq.pop();
			if(dist[i][x.second] != x.first) continue;
			vis[ed[x.second]]++;
			if(vis[ed[x.second]] > 2) continue;
			for(auto &j : gph[ed[x.second]]){
				if((j.idx ^ x.second) == 1) continue;
				if(dist[i][j.idx] > x.first + j.cost){
					dist[i][j.idx] = x.first + j.cost;
					pq.emplace(dist[i][j.idx], j.idx);
				}
			}
		}
	}
	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++){
			if(i != j){
				vector<path_info> pth;
				for(auto &k : gph[i]){
					for(auto &l : gph[j]){
						lint cost = dist[k.idx][l.idx ^ 1];
						pth.push_back({k.pos, l.pos, cost});
					}
				}
				sort(pth.begin(), pth.end());
				if(pth.size() <= 2){
					plist[i][j] = pth;
					continue;
				}
				plist[i][j].push_back(pth[0]);
				plist[i][j].push_back(pth[1]);
				if(pth[0].midl != pth[1].midl && pth[0].midr != pth[1].midr){
					plist[i][j].push_back(pth[2]);
					pi nxt_punkty = pi(-1, -1);
					if(pth[2].midl == pth[1].midl){
						nxt_punkty = pi(pth[2].midl, pth[0].midr);
					}
					else if(pth[2].midl == pth[0].midl){
						nxt_punkty = pi(pth[2].midl, pth[1].midr);
					}
					else if(pth[2].midr == pth[1].midr){
						nxt_punkty = pi(pth[0].midl, pth[2].midr);
					}
					else if(pth[2].midr == pth[0].midr){
						nxt_punkty = pi(pth[1].midl, pth[2].midr);
					}
					for(int k = 3; k < pth.size(); k++){
						if(nxt_punkty.first != pth[k].midl && nxt_punkty.second != pth[k].midr){
							plist[i][j].push_back(pth[k]);
							break;
						}
					}
				}
				else if(pth[0].midl == pth[1].midl){
					for(int x = 0; x < pth.size(); x++){
						if(pth[x].midl != pth[0].midl){
							plist[i][j].push_back(pth[x]);
							pi nxt_punkty = pi(pth[0].midl, pth[x].midr);
							for(int k = x + 1; k < pth.size(); k++){
								if(nxt_punkty.first != pth[k].midl && nxt_punkty.second != pth[k].midr){
									plist[i][j].push_back(pth[k]);
									break;
								}
							}
							break;
						}
					}
				}
				else if(pth[0].midr == pth[1].midr){
					for(int x = 0; x < pth.size(); x++){
						if(pth[x].midr != pth[0].midr){
							plist[i][j].push_back(pth[x]);
							pi nxt_punkty = pi(pth[x].midl, pth[0].midr);
							for(int k = x + 1; k < pth.size(); k++){
								if(nxt_punkty.first != pth[k].midl && nxt_punkty.second != pth[k].midr){
									plist[i][j].push_back(pth[k]);
									break;
								}
							}
							break;
						}
					}
				}
				else assert(0);
			//	for(auto &k : plist[i][j]) printf("(%d -> %d), (%d -> %d), %lld\n", i, k.midl, k.midr, j, k.cost);
			}
		}
	}
}

int a[MAXL];
lint dp[MAXL][4];

int main(){
	scanf("%d %d %d %d",&n,&m,&q,&l);
	preprocess();
	for(int i=0; i<l; i++) scanf("%d",&a[i]);
	for(int i=0; i<q; i++){
		int x, v; scanf("%d %d",&x,&v);
		x--;
		a[x] = v;
		memset(dp, 0x3f, sizeof(dp));
		for(int k=0; k<plist[a[0]][a[1]].size(); k++){
			dp[1][k] = plist[a[0]][a[1]][k].cost;
		}
		for(int k=2; k<l; k++){
			for(int x = 0; x < plist[a[k - 2]][a[k - 1]].size(); x++){
				for(int y = 0; y < plist[a[k - 1]][a[k]].size(); y++){
					if(plist[a[k - 2]][a[k - 1]][x].midr != plist[a[k - 1]][a[k]][y].midl){
						dp[k][y] = min(dp[k][y], dp[k - 1][x] + plist[a[k - 1]][a[k]][y].cost);
					}
				}
			}
		}
		int w = plist[a[l - 2]][a[l - 1]].size();
		lint dap = *min_element(dp[l - 1], dp[l - 1] + w);
		if(dap > 9e17) dap = -1;
		printf("%lld\n", dap);
	}
}

Compilation message

wild_boar.cpp: In function 'void preprocess()':
wild_boar.cpp:87:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int k = 3; k < pth.size(); k++){
                     ~~^~~~~~~~~~~~
wild_boar.cpp:95:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int x = 0; x < pth.size(); x++){
                     ~~^~~~~~~~~~~~
wild_boar.cpp:99:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
        for(int k = x + 1; k < pth.size(); k++){
                           ~~^~~~~~~~~~~~
wild_boar.cpp:110:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int x = 0; x < pth.size(); x++){
                     ~~^~~~~~~~~~~~
wild_boar.cpp:114:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
        for(int k = x + 1; k < pth.size(); k++){
                           ~~^~~~~~~~~~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:143:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0; k<plist[a[0]][a[1]].size(); k++){
                ~^~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:147:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int x = 0; x < plist[a[k - 2]][a[k - 1]].size(); x++){
                   ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:148:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int y = 0; y < plist[a[k - 1]][a[k]].size(); y++){
                    ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp: In function 'void preprocess()':
wild_boar.cpp:29:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, e, x; scanf("%d %d %d",&s,&e,&x);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:135: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,&q,&l);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:137:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0; i<l; i++) scanf("%d",&a[i]);
                         ~~~~~^~~~~~~~~~~~
wild_boar.cpp:139:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, v; scanf("%d %d",&x,&v);
             ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 223736 KB Output is correct
2 Correct 191 ms 223864 KB Output is correct
3 Correct 192 ms 223864 KB Output is correct
4 Correct 190 ms 223872 KB Output is correct
5 Correct 203 ms 223964 KB Output is correct
6 Correct 190 ms 223952 KB Output is correct
7 Correct 190 ms 223864 KB Output is correct
8 Correct 189 ms 223960 KB Output is correct
9 Correct 190 ms 223860 KB Output is correct
10 Correct 191 ms 223736 KB Output is correct
11 Correct 191 ms 223736 KB Output is correct
12 Correct 189 ms 223900 KB Output is correct
13 Correct 194 ms 223736 KB Output is correct
14 Correct 192 ms 223864 KB Output is correct
15 Correct 196 ms 223736 KB Output is correct
16 Correct 194 ms 223788 KB Output is correct
17 Correct 191 ms 223836 KB Output is correct
18 Correct 192 ms 223864 KB Output is correct
19 Correct 192 ms 223924 KB Output is correct
20 Correct 192 ms 223864 KB Output is correct
21 Correct 196 ms 223864 KB Output is correct
22 Correct 193 ms 223952 KB Output is correct
23 Correct 227 ms 223864 KB Output is correct
24 Correct 197 ms 223872 KB Output is correct
25 Correct 193 ms 223772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 223736 KB Output is correct
2 Correct 191 ms 223864 KB Output is correct
3 Correct 192 ms 223864 KB Output is correct
4 Correct 190 ms 223872 KB Output is correct
5 Correct 203 ms 223964 KB Output is correct
6 Correct 190 ms 223952 KB Output is correct
7 Correct 190 ms 223864 KB Output is correct
8 Correct 189 ms 223960 KB Output is correct
9 Correct 190 ms 223860 KB Output is correct
10 Correct 191 ms 223736 KB Output is correct
11 Correct 191 ms 223736 KB Output is correct
12 Correct 189 ms 223900 KB Output is correct
13 Correct 194 ms 223736 KB Output is correct
14 Correct 192 ms 223864 KB Output is correct
15 Correct 196 ms 223736 KB Output is correct
16 Correct 194 ms 223788 KB Output is correct
17 Correct 191 ms 223836 KB Output is correct
18 Correct 192 ms 223864 KB Output is correct
19 Correct 192 ms 223924 KB Output is correct
20 Correct 192 ms 223864 KB Output is correct
21 Correct 196 ms 223864 KB Output is correct
22 Correct 193 ms 223952 KB Output is correct
23 Correct 227 ms 223864 KB Output is correct
24 Correct 197 ms 223872 KB Output is correct
25 Correct 193 ms 223772 KB Output is correct
26 Correct 193 ms 223836 KB Output is correct
27 Correct 214 ms 224688 KB Output is correct
28 Correct 209 ms 224760 KB Output is correct
29 Correct 276 ms 225016 KB Output is correct
30 Correct 277 ms 225112 KB Output is correct
31 Correct 267 ms 224940 KB Output is correct
32 Correct 265 ms 224936 KB Output is correct
33 Correct 278 ms 225912 KB Output is correct
34 Correct 279 ms 225912 KB Output is correct
35 Correct 264 ms 226188 KB Output is correct
36 Correct 266 ms 226040 KB Output is correct
37 Correct 284 ms 226040 KB Output is correct
38 Correct 284 ms 227448 KB Output is correct
39 Correct 278 ms 227320 KB Output is correct
40 Correct 284 ms 227292 KB Output is correct
41 Correct 283 ms 227448 KB Output is correct
42 Correct 273 ms 228380 KB Output is correct
43 Correct 284 ms 229240 KB Output is correct
44 Correct 288 ms 229112 KB Output is correct
45 Correct 259 ms 231228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 223736 KB Output is correct
2 Correct 191 ms 223864 KB Output is correct
3 Correct 192 ms 223864 KB Output is correct
4 Correct 190 ms 223872 KB Output is correct
5 Correct 203 ms 223964 KB Output is correct
6 Correct 190 ms 223952 KB Output is correct
7 Correct 190 ms 223864 KB Output is correct
8 Correct 189 ms 223960 KB Output is correct
9 Correct 190 ms 223860 KB Output is correct
10 Correct 191 ms 223736 KB Output is correct
11 Correct 191 ms 223736 KB Output is correct
12 Correct 189 ms 223900 KB Output is correct
13 Correct 194 ms 223736 KB Output is correct
14 Correct 192 ms 223864 KB Output is correct
15 Correct 196 ms 223736 KB Output is correct
16 Correct 194 ms 223788 KB Output is correct
17 Correct 191 ms 223836 KB Output is correct
18 Correct 192 ms 223864 KB Output is correct
19 Correct 192 ms 223924 KB Output is correct
20 Correct 192 ms 223864 KB Output is correct
21 Correct 196 ms 223864 KB Output is correct
22 Correct 193 ms 223952 KB Output is correct
23 Correct 227 ms 223864 KB Output is correct
24 Correct 197 ms 223872 KB Output is correct
25 Correct 193 ms 223772 KB Output is correct
26 Correct 193 ms 223836 KB Output is correct
27 Correct 214 ms 224688 KB Output is correct
28 Correct 209 ms 224760 KB Output is correct
29 Correct 276 ms 225016 KB Output is correct
30 Correct 277 ms 225112 KB Output is correct
31 Correct 267 ms 224940 KB Output is correct
32 Correct 265 ms 224936 KB Output is correct
33 Correct 278 ms 225912 KB Output is correct
34 Correct 279 ms 225912 KB Output is correct
35 Correct 264 ms 226188 KB Output is correct
36 Correct 266 ms 226040 KB Output is correct
37 Correct 284 ms 226040 KB Output is correct
38 Correct 284 ms 227448 KB Output is correct
39 Correct 278 ms 227320 KB Output is correct
40 Correct 284 ms 227292 KB Output is correct
41 Correct 283 ms 227448 KB Output is correct
42 Correct 273 ms 228380 KB Output is correct
43 Correct 284 ms 229240 KB Output is correct
44 Correct 288 ms 229112 KB Output is correct
45 Correct 259 ms 231228 KB Output is correct
46 Correct 386 ms 227064 KB Output is correct
47 Correct 3527 ms 244020 KB Output is correct
48 Correct 3519 ms 272064 KB Output is correct
49 Correct 3550 ms 302492 KB Output is correct
50 Correct 3563 ms 302700 KB Output is correct
51 Correct 3545 ms 302636 KB Output is correct
52 Correct 3578 ms 302732 KB Output is correct
53 Correct 3552 ms 302712 KB Output is correct
54 Correct 3560 ms 302668 KB Output is correct
55 Correct 3555 ms 302584 KB Output is correct
56 Correct 3610 ms 319048 KB Output is correct
57 Correct 3682 ms 337224 KB Output is correct
58 Correct 3677 ms 356788 KB Output is correct
59 Correct 3753 ms 377708 KB Output is correct
60 Correct 3736 ms 400412 KB Output is correct
61 Correct 3772 ms 424628 KB Output is correct
62 Correct 3725 ms 450588 KB Output is correct
63 Correct 3638 ms 477892 KB Output is correct
64 Correct 2200 ms 537352 KB Output is correct
65 Correct 2166 ms 537464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 223736 KB Output is correct
2 Correct 191 ms 223864 KB Output is correct
3 Correct 192 ms 223864 KB Output is correct
4 Correct 190 ms 223872 KB Output is correct
5 Correct 203 ms 223964 KB Output is correct
6 Correct 190 ms 223952 KB Output is correct
7 Correct 190 ms 223864 KB Output is correct
8 Correct 189 ms 223960 KB Output is correct
9 Correct 190 ms 223860 KB Output is correct
10 Correct 191 ms 223736 KB Output is correct
11 Correct 191 ms 223736 KB Output is correct
12 Correct 189 ms 223900 KB Output is correct
13 Correct 194 ms 223736 KB Output is correct
14 Correct 192 ms 223864 KB Output is correct
15 Correct 196 ms 223736 KB Output is correct
16 Correct 194 ms 223788 KB Output is correct
17 Correct 191 ms 223836 KB Output is correct
18 Correct 192 ms 223864 KB Output is correct
19 Correct 192 ms 223924 KB Output is correct
20 Correct 192 ms 223864 KB Output is correct
21 Correct 196 ms 223864 KB Output is correct
22 Correct 193 ms 223952 KB Output is correct
23 Correct 227 ms 223864 KB Output is correct
24 Correct 197 ms 223872 KB Output is correct
25 Correct 193 ms 223772 KB Output is correct
26 Correct 193 ms 223836 KB Output is correct
27 Correct 214 ms 224688 KB Output is correct
28 Correct 209 ms 224760 KB Output is correct
29 Correct 276 ms 225016 KB Output is correct
30 Correct 277 ms 225112 KB Output is correct
31 Correct 267 ms 224940 KB Output is correct
32 Correct 265 ms 224936 KB Output is correct
33 Correct 278 ms 225912 KB Output is correct
34 Correct 279 ms 225912 KB Output is correct
35 Correct 264 ms 226188 KB Output is correct
36 Correct 266 ms 226040 KB Output is correct
37 Correct 284 ms 226040 KB Output is correct
38 Correct 284 ms 227448 KB Output is correct
39 Correct 278 ms 227320 KB Output is correct
40 Correct 284 ms 227292 KB Output is correct
41 Correct 283 ms 227448 KB Output is correct
42 Correct 273 ms 228380 KB Output is correct
43 Correct 284 ms 229240 KB Output is correct
44 Correct 288 ms 229112 KB Output is correct
45 Correct 259 ms 231228 KB Output is correct
46 Correct 386 ms 227064 KB Output is correct
47 Correct 3527 ms 244020 KB Output is correct
48 Correct 3519 ms 272064 KB Output is correct
49 Correct 3550 ms 302492 KB Output is correct
50 Correct 3563 ms 302700 KB Output is correct
51 Correct 3545 ms 302636 KB Output is correct
52 Correct 3578 ms 302732 KB Output is correct
53 Correct 3552 ms 302712 KB Output is correct
54 Correct 3560 ms 302668 KB Output is correct
55 Correct 3555 ms 302584 KB Output is correct
56 Correct 3610 ms 319048 KB Output is correct
57 Correct 3682 ms 337224 KB Output is correct
58 Correct 3677 ms 356788 KB Output is correct
59 Correct 3753 ms 377708 KB Output is correct
60 Correct 3736 ms 400412 KB Output is correct
61 Correct 3772 ms 424628 KB Output is correct
62 Correct 3725 ms 450588 KB Output is correct
63 Correct 3638 ms 477892 KB Output is correct
64 Correct 2200 ms 537352 KB Output is correct
65 Correct 2166 ms 537464 KB Output is correct
66 Correct 971 ms 224256 KB Output is correct
67 Correct 17081 ms 279564 KB Output is correct
68 Correct 2078 ms 445688 KB Output is correct
69 Execution timed out 18124 ms 447368 KB Time limit exceeded
70 Halted 0 ms 0 KB -