QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#721979#5540. City HallinfCraft#TL 2ms12104kbC++173.0kb2024-11-07 17:21:362024-11-07 17:21:37

Judging History

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

  • [2024-11-07 17:21:37]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:12104kb
  • [2024-11-07 17:21:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define fori(x, y) for (int i=(x);i<=(y);++i)
#define forj(x, y) for (int j=(x);j<=(y);++j)
#define fork(x, y) for (int k=(x);k<=(y);++k)

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

#define debug(x) cerr << #x << " = " << x << endl

typedef long double ld;
const int N = 2e5+7;
ll h[N];
vector<pll> g[N];

bool vis[N];
ll dist[N][2];
struct Node {
    int u;
    ll dis;
    int fa;
    // int layer;  // 在哪一层
    Node(int u, ld dis, int fa): u(u), dis(dis), fa(fa) {}
};
struct cmp {
    bool operator()(Node& n1, Node& n2) {
        return n1.dis > n2.dis;
    }
};
void dij(int s, int n, int lay) {
    fori(1, n) {
        vis[i] = false;
        dist[i][lay] = 1e18;
    }
    priority_queue<Node,vector<Node>,cmp> pri;
    pri.push(Node(s, 0, 0));
    while (pri.size()) {
        auto no = pri.top();
        pri.pop();
        int u = no.u;
        if (vis[u]) continue;
        dist[u][lay] = no.dis;
        vis[u] = true;
        for (auto [v, w]: g[u]) {
            if (vis[v]) continue;
            pri.push(Node(v, no.dis+w, u));
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m, s, t;
    cin >> n >> m >> s >> t;
    fori(1, n) cin >> h[i];
    fori(1, m) {
        int u, v;
        cin >> u >> v;
        ll dis = (h[u]-h[v])*(h[u]-h[v])*2;
        g[u].push_back({v, dis});
        g[v].push_back({u, dis});
    }
    dij(s, n, 0);
    dij(t, n, 1);
    ll ans = min(dist[t][0], dist[s][1]);
    for (auto [v, w]: g[s]) {
        if (v == t) {
            ans = 0;
            break;
        }
    }
    fori(1, n) {
        if (i == s||i == t) continue;
        if (g[i].size()>500) {
            vector<bool> vis(n+1, false);
            vector<ll> dist1(n+1, 1e18);
            priority_queue<Node,vector<Node>,cmp> pri;
            pri.push(Node(s, 0, 0));
            while (pri.size()) {
                auto no = pri.top();
                pri.pop();
                int u = no.u;
                if (vis[u]) continue;
                dist1[u] = no.dis;
                vis[u] = true;
                for (auto [v, w]: g[u]) {
                    if (vis[v]) continue;
                    if (u == i) {
                        pri.push(Node(v, no.dis+(h[u]-h[no.fa])*(h[u]-h[no.fa]), u));
                    }
                    else if (v == i) pri.push(Node(v, no.dis, u));
                    else pri.push(Node(v, no.dis+w, u));
                }
            }
            ans = min(ans, dist1[t]);
        }
        else {
            forj(0, g[i].size()-1) fork(j+1, g[i].size()-1) {
                int u = g[i][j].first, v = g[i][k].first;
                if (dist[u][0]+dist[v][1]>dist[v][0]+dist[u][1]) swap(u, v);
                ans = min(ans, dist[u][0]+dist[v][1]+(h[u]-h[v])*(h[u]-h[v]));
            }
        }
    }
    cout << fixed << setprecision(10);
    cout << ans/2.0l << endl;

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 12104kb

input:

5 6 1 3
5 100 8 2 10
1 2
2 3
2 5
1 4
4 5
3 5

output:

4.5000000000

result:

ok found '4.5000000', expected '4.5000000', error '0.0000000'

Test #2:

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

input:

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

output:

3.0000000000

result:

ok found '3.0000000', expected '3.0000000', error '0.0000000'

Test #3:

score: 0
Accepted
time: 2ms
memory: 11952kb

input:

5 4 1 4
8 8 8 8 100
1 2
2 3
3 4
4 5

output:

0.0000000000

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #4:

score: 0
Accepted
time: 2ms
memory: 10220kb

input:

2 1 1 2
0 100000
1 2

output:

0.0000000000

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #5:

score: -100
Time Limit Exceeded

input:

632 199396 167 549
22513 93521 41403 35441 97617 53210 62622 4751 81863 14470 2994 93092 40432 30872 34423 36577 92540 92961 4110 14691 83321 10680 89112 80890 31108 24492 8973 42636 33792 27400 82361 85003 68940 31221 48625 276 52755 6649 34381 54399 6063 22628 17715 54052 58175 86609 82622 29917 9...

output:


result: