QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#710559#7883. Takeout DeliveringSDNUyuqiRE 0ms0kbC++231.8kb2024-11-04 20:26:362024-11-04 20:26:37

Judging History

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

  • [2024-11-04 20:26:37]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-04 20:26:36]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=1e6+100;

struct EDGE
{
    int u,v,w;
    bool operator<(const EDGE & e)
    {
        return w<e.w;
    }
};

int n,m;

vector<EDGE>e;

int fa[N],fa1[N],fa2[N];

int find(int x,int faa[])
{
    if(faa[x]!=x)
    {
        faa[x]=find(faa[x],faa);
    }
    return faa[x];
}

void hb(int x,int y,int faa[])
{
    x=find(x,faa);
    y=find(y,faa);
    if(x==y)
    {
        return ;
    }
    faa[x]=y;
}

ll K(int faa[],int id,int x,int y)
{
    vector<EDGE>E;
    for(int i=1;i<=n;i++)
    {
        faa[i]=i;
    }
    for(auto [u,v,w]:e)
    {
        int sign=find(u,fa);
        int sign2=find(v,fa);
        if(find(u,fa)==id&&find(v,fa)==id)
        {
            E.push_back({u,v,w});
        }
    }
    for(auto [u,v,w]:E)
    {
        hb(u,v,faa);
        if(find(x,faa)==find(y,faa))
        {
            return w;
        }
    }
    return 0;
}


signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
        ll u,v,w;
        cin>>u>>v>>w;
        e.push_back({u,v,w});
    }
    sort(e.begin(),e.end());
    ll an=0;
    for(auto [u,v,w]:e)
    {
        int fu=find(u,fa),fv=find(v,fa);
        int f1=find(1,fa),fn=find(n,fa);
        int sign1=(f1==fu?u:v);
        int signn=(fn==fu?u:v);
        if(f1==find(sign1,fa)&&fn==find(signn,fa))
        {
            int sign=(f1==fu?u:v);
            ll a=K(fa1,f1,1,sign);
            sign=(fn==fu?u:v);
            ll b=K(fa2,fn,n,sign);
            an=w+max(a,b);
            break;
        }
        fa[fu]=fv;
    }
    cout<<an<<endl;
    system("pause");
    return 0;
}

詳細信息

Test #1:

score: 0
Dangerous Syscalls

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:

5

result: