답안 #628501

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
628501 2022-08-13T12:36:42 Z duytuandao21 Toll (BOI17_toll) C++17
25 / 100
3000 ms 56436 KB
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define ii pair<ll, int>
#define task "test"

const int inf = 1e9 + 7;
const ll infll = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 2e6 + 3;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}

int k,n,m,q;
int par[N], s[N];
int u[N], v[N];
vector<ii> adj[N];
struct ha {
	int x,y,z;
};
ha a[N];
bool sor(const ha X, const ha Y) {
	return X.x < Y.x;
}

void solve(int x,int y) {
    vector<ll> d(n+1, inf);
    d[x] = 0;
    priority_queue< ii, vector<ii>, greater<ii> > p;
    p.push(mp(0,x));
    while(!p.empty()) {
        int u = p.top().se;
        int val = p.top().fi; p.pop();
        if(val != d[u]) continue;
        for(ii X : adj[u]) {
            int v = X.se;
            int he = X.fi;
            if(d[v] > val + he) {
                d[v] = val + he;
                p.push(mp(d[v], v));
            }
        }
    }
    if(d[y] == inf) cout<<-1<<'\n'; else cout<<d[y]<<'\n';
}
int getroot(int x) {
	if(par[x] == x) return x;
	return par[x] = getroot(par[x]);
}
void join(int x,int y) {
	x = getroot(x);
	y = getroot(y);
	if(x == y) return;
	par[x] = y;
	s[y] = s[x] + adj[x][0].fi;
}
void sub1() {
	for(int i=1;i<=n;i++) par[i] = i;
	for(int i=1;i<=m;i++) {
		//cout<<a[i].x<<" "<<a[i].y<<'\n';
		join(a[i].x, a[i].y);
	}
	for(int i=1;i<=q;i++) {
		if(getroot(u[i]) != getroot(v[i])) cout<<-1<<'\n'; else 
			cout<<s[v[i]] - s[u[i]]<<'\n';
	}
	//cout<<s[4];
}
void sub2() {
	vector<ll> d(n+1, inf);
    d[0] = 0;
    priority_queue< ii, vector<ii>, greater<ii> > p;
    p.push(mp(0,0));
    while(!p.empty()) {
        int u = p.top().se;
        int val = p.top().fi; p.pop();
        if(val != d[u]) continue;
        for(ii X : adj[u]) {
            int v = X.se;
            int he = X.fi;
            if(d[v] > val + he) {
                d[v] = val + he;
                p.push(mp(d[v], v));
            }
        }
    }
	for(int i=1;i<=q;i++) {
		if(d[v[i]] == inf) cout<<-1<<'\n'; else cout<<d[v[i]]<<'\n';
	}
}
void bruteforce() {
	for(int i=1;i<=q;i++) {
		solve(u[i], v[i]);
	}
}
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);	
	// freopen(task".inp" , "r" , stdin);
	// freopen(task".out" , "w" , stdout);
	cin>>k>>n>>m>>q;
	for(int i=1;i<=m;i++) {
        int u,v,w; cin>>u>>v>>w;
		a[i] = {u,v,w};
		adj[u].pb(mp(w,v));
		//join(u,v);
	}
	sort(a+1,a+1+m,sor);
	int count_sub2 = 0;
	for(int i=1;i<=q;i++) {
		cin>>u[i]>>v[i];
		if(u[i] == 0) count_sub2++;
	}
	if(k == 1) sub1(); else 
	if(count_sub2 == q) sub2(); else 
	bruteforce();

    return 0;
}
/* 5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13 */
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 51356 KB Output is correct
2 Correct 25 ms 47308 KB Output is correct
3 Correct 25 ms 47304 KB Output is correct
4 Correct 25 ms 47304 KB Output is correct
5 Correct 25 ms 47312 KB Output is correct
6 Correct 25 ms 47316 KB Output is correct
7 Correct 23 ms 47292 KB Output is correct
8 Correct 52 ms 50868 KB Output is correct
9 Correct 49 ms 50816 KB Output is correct
10 Correct 27 ms 47616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 53060 KB Output is correct
2 Correct 25 ms 47272 KB Output is correct
3 Correct 28 ms 47212 KB Output is correct
4 Correct 27 ms 47216 KB Output is correct
5 Correct 25 ms 47276 KB Output is correct
6 Correct 25 ms 47188 KB Output is correct
7 Correct 28 ms 47472 KB Output is correct
8 Correct 27 ms 47492 KB Output is correct
9 Correct 49 ms 50800 KB Output is correct
10 Correct 93 ms 55600 KB Output is correct
11 Correct 74 ms 53244 KB Output is correct
12 Correct 58 ms 52004 KB Output is correct
13 Correct 94 ms 56436 KB Output is correct
14 Correct 74 ms 52732 KB Output is correct
15 Correct 57 ms 51660 KB Output is correct
16 Correct 57 ms 51644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47212 KB Output is correct
2 Correct 25 ms 47188 KB Output is correct
3 Correct 25 ms 47320 KB Output is correct
4 Correct 24 ms 47316 KB Output is correct
5 Correct 24 ms 47188 KB Output is correct
6 Correct 26 ms 47300 KB Output is correct
7 Correct 31 ms 47340 KB Output is correct
8 Correct 34 ms 47588 KB Output is correct
9 Correct 38 ms 47432 KB Output is correct
10 Correct 49 ms 50640 KB Output is correct
11 Correct 292 ms 52852 KB Output is correct
12 Correct 383 ms 55368 KB Output is correct
13 Correct 450 ms 56284 KB Output is correct
14 Correct 346 ms 53988 KB Output is correct
15 Correct 255 ms 51628 KB Output is correct
16 Correct 229 ms 51596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47212 KB Output is correct
2 Correct 25 ms 47188 KB Output is correct
3 Correct 25 ms 47320 KB Output is correct
4 Correct 24 ms 47316 KB Output is correct
5 Correct 24 ms 47188 KB Output is correct
6 Correct 26 ms 47300 KB Output is correct
7 Correct 31 ms 47340 KB Output is correct
8 Correct 34 ms 47588 KB Output is correct
9 Correct 38 ms 47432 KB Output is correct
10 Correct 49 ms 50640 KB Output is correct
11 Correct 292 ms 52852 KB Output is correct
12 Correct 383 ms 55368 KB Output is correct
13 Correct 450 ms 56284 KB Output is correct
14 Correct 346 ms 53988 KB Output is correct
15 Correct 255 ms 51628 KB Output is correct
16 Correct 229 ms 51596 KB Output is correct
17 Execution timed out 3070 ms 52904 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 51356 KB Output is correct
2 Correct 25 ms 47308 KB Output is correct
3 Correct 25 ms 47304 KB Output is correct
4 Correct 25 ms 47304 KB Output is correct
5 Correct 25 ms 47312 KB Output is correct
6 Correct 25 ms 47316 KB Output is correct
7 Correct 23 ms 47292 KB Output is correct
8 Correct 52 ms 50868 KB Output is correct
9 Correct 49 ms 50816 KB Output is correct
10 Correct 27 ms 47616 KB Output is correct
11 Correct 85 ms 53060 KB Output is correct
12 Correct 25 ms 47272 KB Output is correct
13 Correct 28 ms 47212 KB Output is correct
14 Correct 27 ms 47216 KB Output is correct
15 Correct 25 ms 47276 KB Output is correct
16 Correct 25 ms 47188 KB Output is correct
17 Correct 28 ms 47472 KB Output is correct
18 Correct 27 ms 47492 KB Output is correct
19 Correct 49 ms 50800 KB Output is correct
20 Correct 93 ms 55600 KB Output is correct
21 Correct 74 ms 53244 KB Output is correct
22 Correct 58 ms 52004 KB Output is correct
23 Correct 94 ms 56436 KB Output is correct
24 Correct 74 ms 52732 KB Output is correct
25 Correct 57 ms 51660 KB Output is correct
26 Correct 57 ms 51644 KB Output is correct
27 Correct 27 ms 47212 KB Output is correct
28 Correct 25 ms 47188 KB Output is correct
29 Correct 25 ms 47320 KB Output is correct
30 Correct 24 ms 47316 KB Output is correct
31 Correct 24 ms 47188 KB Output is correct
32 Correct 26 ms 47300 KB Output is correct
33 Correct 31 ms 47340 KB Output is correct
34 Correct 34 ms 47588 KB Output is correct
35 Correct 38 ms 47432 KB Output is correct
36 Correct 49 ms 50640 KB Output is correct
37 Correct 292 ms 52852 KB Output is correct
38 Correct 383 ms 55368 KB Output is correct
39 Correct 450 ms 56284 KB Output is correct
40 Correct 346 ms 53988 KB Output is correct
41 Correct 255 ms 51628 KB Output is correct
42 Correct 229 ms 51596 KB Output is correct
43 Execution timed out 3070 ms 52904 KB Time limit exceeded
44 Halted 0 ms 0 KB -