QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#462814#6306. Chase GameBalintRWA 1ms6244kbC++202.8kb2024-07-04 05:16:482024-07-04 05:16:48

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 05:16:48]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6244kb
  • [2024-07-04 05:16:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cpx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) begin(v), end(v)
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound(ALL(v), x) - (v).begin())
#define ubv(v, x) (upper_bound(ALL(v), x) - (v).begin())
template <typename T> void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cerr << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cerr << #arr; FR(_i, n) cerr << ' ' << (arr)[_i]; cerr << endl;}
template <typename T, typename U>
ostream& operator<<(ostream &os, pair<T, U> p){return os << "(" << p.fs << ", " << p.sn << ")";}

const int MN = 1e5 + 5;
int n, m, s, k;
vi adjList[MN];
int disN[MN], disS[MN];
int qu[MN], ql, qr;

ll getCost(int d){
    d++;
    int dv = d/k, rem = d % k;
    return (ll) dv*k*(k+1)/2 + (ll) rem*(k+k-rem+1)/2;
}

void bfs(int src, int *dis){
    fill_n(dis, n, -1);
    dis[src] = 0;
    ql = qr = 0;
    qu[qr++] = src;
    while(ql < qr){
        int n1 = qu[ql++];
        for(int n2 : adjList[n1]) if(dis[n2] == -1) dis[n2] = dis[n1]+1, qu[qr++] = n2;
    }
}

ll cost[MN], dp[MN];

void calcDp(){
    FR(i, n) cost[i] = getCost(disN[i]);

    ms(dp, INF);
    minPq<pair<ll, int>> pq;
    FR(i, n) if(disS[i] == k) dp[i] = cost[i], pq.push({cost[i], i});
    if(disS[n-1] < k) dp[n-1] = cost[n-1], pq.push({cost[n-1], n-1});

    while(!pq.empty()){
        auto [c1, n1] = pq.top(); pq.pop();
        if(c1 != dp[n1]) continue;
        for(int n2 : adjList[n1]) if(disS[n2] < k){
            ll c2 = c1 + k - disS[n2];
            if(c2 < dp[n2]) dp[n2] = c2, pq.push({c2, n2});
        }
    }
}

int main(){
    cin.sync_with_stdio(0); cin.tie(0);
    cin >> n >> m >> s >> k;
    s--;
    FR(i, m){
        int a, b;
        cin >> a >> b;
        a--; b--;
        adjList[a].pb(b);
        adjList[b].pb(a);
    }

    bfs(n-1, disN);
    bfs(s, disS);
    calcDp();

    /*dbgArr(disN, n);
    dbgArr(disS, n);
    dbgArr(cost, n);
    dbgArr(dp, n);*/

    ll ans = LLINF;
    for(int n1 : adjList[0]) ans = min(ans, disS[n1] < k ? dp[n1] : cost[n1]);
    cout << ans << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4316kb

input:

5 5 3 1
1 2
2 4
4 5
1 3
3 5

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 4420kb

input:

13 17 12 3
1 2
2 3
3 4
4 13
5 13
7 8
7 9
7 10
7 11
7 6
12 7
1 8
8 9
9 10
10 11
11 6
6 13

output:

7

result:

ok 1 number(s): "7"

Test #3:

score: 0
Accepted
time: 1ms
memory: 4412kb

input:

10 9 4 1
4 3
1 2
7 6
6 5
8 9
9 10
5 4
8 7
3 2

output:

9

result:

ok 1 number(s): "9"

Test #4:

score: 0
Accepted
time: 1ms
memory: 4420kb

input:

10 20 9 1
9 5
2 9
4 5
10 5
7 3
1 8
7 1
7 5
3 4
3 1
7 8
3 8
9 3
10 6
9 1
1 6
8 5
6 2
1 10
2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 1ms
memory: 6244kb

input:

10 20 3 1
1 4
6 7
5 4
5 3
2 1
8 1
7 8
2 6
2 4
4 8
9 5
1 10
7 4
5 8
4 10
2 5
6 10
4 6
3 7
1 3

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 1ms
memory: 5616kb

input:

10 20 4 1
2 7
6 4
2 3
2 4
6 5
8 5
6 7
10 2
3 10
1 8
3 9
3 7
1 7
5 1
6 1
3 4
2 1
5 2
9 2
9 10

output:

2

result:

ok 1 number(s): "2"

Test #7:

score: -100
Wrong Answer
time: 1ms
memory: 6148kb

input:

10 20 1 10
10 9
2 5
7 8
7 10
10 8
8 9
5 6
3 1
6 4
7 9
2 3
3 6
2 1
8 6
5 8
7 4
4 3
2 4
4 1
9 6

output:

27

result:

wrong answer 1st numbers differ - expected: '24', found: '27'