UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#144373#793. 序列Lockman0117ms2436kbC++112.4kb2022-01-27 16:42:202022-01-27 16:42:21

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define inf 0x7fffffff
#define MAXM 2000100
#define MAXN 200100
using namespace std;
struct edge{
    int to,next,dis;
};
edge e[MAXM];
int head[MAXN],dis[MAXN],cnt=0;
int height[MAXN];
bool vis[MAXN];
int n,m,s=1;
void addEdge(int u,int v,int d){
    cnt++;
    e[cnt].dis=d;
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
int ans=0;
int fa[MAXN];
bool cmp(edge a,edge b){
    return a.dis<b.dis;
}
void init(int n){
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
}
int find(int x){
    if(fa[x]==x){
        return x;
    }
    fa[x]=find(fa[x]);
    return fa[x];
}
void kruskal(){
    int cnt2=0;
    sort(e,e+m,cmp);
    for(int i=0;i<m;i++){
        int fu=find(e[i].to);
        int fv=find(e[i].next);
        if(fu==fv){
            continue;
        }
        ans+=e[i].dis;
        // printf("%d\n",ans);
        fa[fu]=fv;
        cnt2++;
    }
    printf("%d\n",ans);
}
struct node{
    int dis;
    int pos;
    bool operator <(const node &x)const{
        return x.dis<dis;
    }
};
priority_queue<node> q;
void dijkstra(){
    for(int i=1;i<=n;i++){
        dis[i]=inf;
    }
    dis[s]=0;
    q.push((node){0,s});
    while(!q.empty()){
        node tmp=q.top();
        q.pop();
        int x=tmp.pos;
        if(vis[x]){
            continue;
        }
        vis[x]=true;
        for(int i=head[x];i;i=e[i].next){
            int y=e[i].to;
            if(dis[y]>dis[x]+e[i].dis){
                dis[y]=dis[x]+e[i].dis;
                if(!vis[y]){
                    q.push((node){dis[y],y});
                }
            }
        }
    }
}
int main(void){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&height[i]);
    }
    int a,b,c;
    init(2*n+10);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&a,&b,&c);
        if(height[a]>=height[b]){
            // printf("%d %d %d\n",a,b,c);
            addEdge(a,b,c);
        }
        if(height[b]>=height[a]){
            // printf("%d %d %d\n",b,a,c);
            addEdge(b,a,c);
        }
    }
    dijkstra();
    int total=0;
    for(int i=1;i<=n;i++){
        if(dis[i]!=inf){
            total++;
        }
    }
    printf("%d ",total);
    kruskal();
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 1276kb

input:

100 269 4
485 603 637 728 140
989 957 446 369 839
483 32 502 31 92
291 806 345 79 177
386 7 753 980 ...

output:

1 32957

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 52ms
memory: 2436kb

input:

100000 299 4
504 637 676 447 537
120 358 775 645 76
989 61 532 75 321
499 298 338 819 318
761 692 15...

output:

1 0

result:

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

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 65ms
memory: 2428kb

input:

100000 259 1
328 241
589 414
132 769
134 904
554 18
152 748
912 631
338 977
316 622
726 293
588 622
...

output:

1 0

result:

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

Subtask #4:

score: 0
Skipped