QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#276742#4165. 星际竞速SoyTony70 206ms9368kbC++142.9kb2023-12-06 10:14:522023-12-06 10:14:53

Judging History

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

  • [2023-12-06 10:14:53]
  • 评测
  • 测评结果:70
  • 用时:206ms
  • 内存:9368kb
  • [2023-12-06 10:14:52]
  • 提交

answer

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

const int maxn=1610;
const int maxm=7e5+10;
const int maxxn=0x3f3f3f3f;

inline int read(){
    int x=0,w=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x*w;
}


int n,m;
int a[805];
int S,T,SS,TT;
int cost[805][805];
int W[maxn];
struct Graph{
    struct edge{
        int to,nxt;
        int lim,c;
    }e[maxm<<1];
    int head[maxn],cnt=1;
    inline void add_edge(int u,int v,int w,int c){
        // printf("%d %d %d %d\n",u,v,w,c);
        e[++cnt].to=v,e[cnt].nxt=head[u],e[cnt].lim=w,e[cnt].c=c,head[u]=cnt;
        e[++cnt].to=u,e[cnt].nxt=head[v],e[cnt].lim=0,e[cnt].c=-c,head[v]=cnt;
    }
    int pre[maxn];
    int dis[maxn];
    bool vis[maxn];
    inline void SPFA(int s){
        queue<int> q;
        memset(dis,0x3f,sizeof(dis));
        memset(vis,0,sizeof(vis));
        dis[s]=0,vis[s]=1;
        q.push(s);
        while(!q.empty()){
            int u=q.front();
            vis[u]=0;
            q.pop();
            for(int i=head[u];i;i=e[i].nxt){
                int v=e[i].to,c=e[i].c;
                if(dis[u]+c<dis[v]&&e[i].lim){
                    pre[v]=i;
                    dis[v]=dis[u]+c;
                    if(vis[v]) continue;
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    inline int min_cost_max_flow(int s,int t){
        int max_flow=0,min_cost=0;
        while(1){
            SPFA(s);
            if(dis[t]==maxxn) return min_cost;
            int mn=maxxn;
            for(int u=t;u!=s;u=e[pre[u]^1].to) mn=min(mn,e[pre[u]].lim);
            max_flow+=mn;
            for(int u=t;u!=s;u=e[pre[u]^1].to){
                min_cost+=mn*e[pre[u]].c;
                e[pre[u]].lim-=mn,e[pre[u]^1].lim+=mn;
            }
        }
    }
}G;
int ans;
int main(){
    // freopen("test.in","r",stdin);
    // freopen("test.out","w",stdout);
    n=read(),m=read();
    S=n*2+1,T=n*2+2,SS=n*2+3,TT=n*2+4;
    for(int i=1;i<=n;++i){
        a[i]=read();
        G.add_edge(S,i,1,a[i]);
        G.add_edge(i,i+n,0,0);
        ++W[i],--W[i+n];
        G.add_edge(i+n,T,1,0);
    }
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            cost[i][j]=a[j];
        }
        cost[i][i]=0;
    }
    for(int i=1;i<=m;++i){
        int u=read(),v=read(),w=read();
        if(u>v) swap(u,v);
        cost[u][v]=min(cost[u][v],w);
    }
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(i==j) continue;
            G.add_edge(i+n,j,1,cost[i][j]);
        }
    }
    for(int i=1;i<=T;++i){
        if(W[i]>0) G.add_edge(i,TT,W[i],0);
        if(W[i]<0) G.add_edge(SS,i,-W[i],0);
    }
    G.add_edge(T,S,1e9,0);
    printf("%d\n",G.min_cost_max_flow(SS,TT));
    return 0;
}

详细

Test #1:

score: 10
Accepted
time: 1ms
memory: 5820kb

input:

4 5
539057 13724 518925 2598
4 1 10423
3 1 31021
3 4 7871
1 2 17315
2 4 3585

output:

586400

result:

ok single line: '586400'

Test #2:

score: 10
Accepted
time: 1ms
memory: 6020kb

input:

15 30
522387 29355 541794 521018 543985 542230 26389 523220 525853 535653 543915 18572 29378 525461 530719
12 9 3190
9 5 26126
4 8 1236
9 13 18451
15 10 25338
14 5 1575
1 5 31237
13 4 24538
15 1 765
11 4 9138
9 15 9881
1 13 11918
14 6 25910
6 2 22440
9 2 20730
6 1 7901
4 3 13426
10 5 5935
1 12 21191...

output:

1790859

result:

ok single line: '1790859'

Test #3:

score: 10
Accepted
time: 1ms
memory: 5864kb

input:

20 50
20426 12681 29314 4878 11984 14987 536269 532472 535906 525476 541019 526047 537789 521207 527194 14951 17826 20554 547772 537878
5 3 18553
19 5 19898
16 13 25686
17 20 19258
7 19 25324
15 14 11599
16 17 2116
8 13 18747
6 1 3787
18 11 22506
1 9 20431
8 20 703
15 10 13914
6 10 13672
8 1 30228
2...

output:

812779

result:

ok single line: '812779'

Test #4:

score: 10
Accepted
time: 157ms
memory: 7316kb

input:

200 1000
20813 546294 5674 525541 7701 1486 521494 5135 25492 546425 535568 534438 518163 15501 526948 21843 545151 519269 13123 16067 529899 534943 30015 27942 20747 524446 1745 19633 545979 7944 7379 545564 538998 542793 11270 20773 12768 543247 7054 517131 525629 529613 1905 7443 536823 537912 48...

output:

6277991

result:

ok single line: '6277991'

Test #5:

score: 10
Accepted
time: 178ms
memory: 9368kb

input:

200 2000
8659 520000 18451 13031 538496 539577 14299 542624 30074 21710 547648 15184 460 20710 2033 30738 19558 111 16063 521028 542620 30809 7201 531721 525734 534681 518319 712 12318 523943 11687 548523 10885 516609 543339 519457 24510 521894 16385 525661 2193 545948 543634 529109 523817 519786 54...

output:

4468321

result:

ok single line: '4468321'

Test #6:

score: 10
Accepted
time: 194ms
memory: 9368kb

input:

200 4000
520133 785 519519 16396 3400 8745 11920 523293 531896 4479 531231 15063 10685 539897 29203 541841 27746 521214 546076 526948 2712 534012 25353 544687 27313 996 524882 545686 21990 548009 523407 28542 519121 522693 8959 18834 540915 16051 538882 19778 546608 3702 525727 32190 31566 15633 181...

output:

2063842

result:

ok single line: '2063842'

Test #7:

score: 10
Accepted
time: 206ms
memory: 9036kb

input:

200 4000
30559 528162 31544 544939 31815 19278 17590 31115 22313 18789 543711 523798 544505 524289 537 533480 10031 26463 2192 11136 524617 19681 525746 8643 546118 908 545969 532088 4651 21387 26629 13770 525441 13226 530663 533705 725 29660 23511 521704 533303 546998 7447 19841 16828 241 13357 698...

output:

1524757

result:

ok single line: '1524757'

Test #8:

score: 0
Time Limit Exceeded

input:

600 12000
543932 12351 18423 541357 528639 4985 29393 6570 31434 539050 12221 22519 16196 544282 16211 4829 540691 532921 6637 538510 8420 545175 544553 530588 14057 361 12138 538240 538198 527115 13399 544276 22755 528120 25946 547865 32078 18388 31521 544691 9174 525539 536097 543976 10688 27279 1...

output:


result:


Test #9:

score: 0
Time Limit Exceeded

input:

800 14000
18996 24768 542822 11592 28975 13142 531386 4506 523343 13742 527235 6638 3244 5514 541848 545754 14207 532721 544053 3444 5796 520365 534394 21979 6175 519372 8938 547502 522388 530211 525293 16019 26829 543430 11086 543154 29240 535185 18109 12599 538727 532387 522574 19668 529550 518110...

output:


result:


Test #10:

score: 0
Time Limit Exceeded

input:

800 15000
549001 9281 27830 1808 6346 535277 12071 12667 18596 534113 521277 538268 8713 27161 547436 20611 527501 531802 522762 11589 527083 4009 521173 535674 547712 13649 4971 5567 548559 15074 1489 18485 4603 530860 538349 18177 523856 528712 17164 1226 526779 547856 533341 31038 518989 18595 54...

output:


result: