QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#710631#5458. Shortest Path Query123456zmyTL 0ms4184kbC++172.4kb2024-11-04 20:46:372024-11-04 20:46:37

Judging History

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

  • [2024-11-04 20:46:37]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:4184kb
  • [2024-11-04 20:46:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
long long ans[50011];
int main()
{
    memset(ans,0x3f,sizeof(ans));
    int n,m;
    scanf("%d%d",&n,&m);
    // n=50000,m=100000;
    vector<vector<pair<int,int>>>to(n+1);
    for(int i=1,u,v,w;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        // u=(i+1)/2,v=(i+1)/2+1,w=i&1;
        to[v].emplace_back(u,w);
    }
    int q;
    scanf("%d",&q);
    // q=50000;
    // vector<long long>ans(q+1,0x3f3f3f3f);
    vector<vector<pair<pair<int,int>,int>>>query(n+1);
    for(int i=1,a,b,x;i<=q;i++)
    {
        scanf("%d%d%d",&a,&b,&x);
        // a=rand()%10000+1,b=rand()%10000+1,x=rand()%n+1;
        query[x].push_back({{a,b},i});
    }
    vector<int>mx(n+1);
    vector<vector<pair<int,int>>>dp(n+1);
    dp[1].push_back({0,0});
    for(int i=2;i<=n;i++)
    {
        dp[i&1023].clear();
        for(auto tmp_:to[i])
        {
            int u=tmp_.first,w=tmp_.second;
            int i_=i&1023,u_=u&1023;
            auto cmp=[&](pair<int,int>i,pair<int,int>j,pair<int,int>k)
            {
                j.first-=i.first,j.second-=i.second;
                k.first-=i.first,k.second-=i.second;
                return k.first*j.second<j.first*k.second;
            };
            auto ins=[&](vector<pair<int,int>>&v,pair<int,int>p)
            {
                if(v.size()&&v.back()==p)v.pop_back();
                while(v.size()>2&&cmp(v[v.size()-2],v[v.size()-1],p))v.pop_back();
                v.push_back(p);
            };
            int l=0;
            vector<pair<int,int>>dp1;
            for(auto&tmp:dp[u_])
            {
                while(l<dp[i_].size()&&dp[i_][l]<tmp)
                    ins(dp1,dp[i_][l++]);
                ins(dp1,{tmp.first+(w^1),tmp.second+w});
            }
            while(l<dp[i_].size())ins(dp1,dp[i_][l++]);
            dp[i_].swap(dp1);
        }
        for(auto tmp:query[i])
        {
            long long a=tmp.first.first,b=tmp.first.second,l=tmp.second;
            int i_=i&1023;
            for(auto&p:dp[i])ans[l]=min(ans[l],p.first*a+p.second*b);
        }
    }
    for(auto tmp:query[1])ans[tmp.second]=0;
    long long anss=0;
    for(int i=1;i<=q;i++)//anss^=ans[i];//
        printf("%lld\n",ans[i]);
    // printf("%d %d\n",anss,clock());
    return 0;
}
/*
4 4
1 2 0
1 3 1
2 4 0
3 4 1
3
3 5 2
3 2 4
2 3 4
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 4184kb

input:

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

output:

3
4
4

result:

ok 3 number(s): "3 4 4"

Test #2:

score: -100
Time Limit Exceeded

input:

50000 100000
1 2 1
2 3 0
3 4 1
4 5 0
5 6 1
6 7 0
7 8 1
8 9 1
9 10 0
10 11 1
11 12 1
12 13 1
13 14 0
14 15 0
15 16 0
16 17 0
17 18 1
18 19 1
19 20 0
20 21 1
21 22 0
22 23 0
23 24 1
24 25 1
25 26 0
26 27 1
27 28 0
28 29 0
29 30 0
30 31 0
31 32 1
32 33 0
33 34 1
34 35 1
35 36 1
36 37 1
37 38 0
38 39 0
...

output:


result: