QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661728#7883. Takeout DeliveringsdnuwyWA 142ms49484kbC++172.6kb2024-10-20 17:48:042024-10-20 17:48:04

Judging History

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

  • [2024-10-20 17:48:04]
  • 评测
  • 测评结果:WA
  • 用时:142ms
  • 内存:49484kb
  • [2024-10-20 17:48:04]
  • 提交

answer

//Think twice,code once.
#include<bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define ff first
#define ss second
#define pii pair<int,int>
#define mem(i,n) memset(i,n,sizeof i)
#define dg(a) std::cout << #a << " = " << a << endl
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int N=3e5+10;
const int M=1e6+10;
int fa[N];
bool b[M][2];
vector<int>g[N];
struct edge
{
    int u,v,w;
    bool operator<(const edge&b) const {
        return w<b.w;
    }   
}e[M];

int res=inf;

int find(int k)
{
    return k==fa[k]?fa[k]:find(fa[k]);
}

int n;
ll ans=INF;

void merge(int x,int y)
{
    int nx=find(x);
    int ny=find(y);
    if(nx==ny) return;
    if(nx>ny)
    {
        swap(nx,ny);
    }
    if(nx==1)
    {
        for(int i:g[ny])
        {
            b[i][1]=1;
            if(b[i][0]+b[i][1]==2)
            {
                res=min(res,e[i].w);
            }
        }
        g[ny].clear();
        fa[ny]=nx;
    }
    else if(ny==n)
    {
        for(int i:g[nx])
        {
            b[i][0]=1;
            if(b[i][0]+b[i][1]==2)
            {
                res=min(res,e[i].w);
            }
        }
        g[nx].clear();
        fa[nx]=ny;
    }
    else
    {
        if(g[nx].size()>g[ny].size())
        {
            for(int i:g[ny])
            {
                g[nx].push_back(i);
            }
            g[ny].clear();
            fa[ny]=nx;
        }
        else
        {
            for(int i:g[nx])
            {
                g[ny].push_back(i);
            }
            g[nx].clear();
            fa[nx]=ny;
        }
    }
}

void solve()
{
    int m;
    cin >> n >> m;
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
        cin >> e[i].u >> e[i].v >> e[i].w;
        if((e[i].u==1&&e[i].v==n)||(e[i].u==n&&e[i].v==1))
        {
            ans=min(ans,1ll*e[i].w);
        }
    }
    sort(e+1,e+1+m);
    for(int i=1;i<=m;i++)
    {
        g[e[i].u].push_back(i);
        g[e[i].v].push_back(i);
        if(e[i].u==1||e[i].v==1) b[i][1]=1;
        if(e[i].v==n||e[i].v==n) b[i][0]=1;
    }
    for(int i=1;i<=m;i++)
    {
        auto [u,v,w]=e[i];
        merge(u,v);
        if(find(1)==find(n)) break;
        ans=min(ans,1ll*w+res);
    }
    cout << ans << endl;
}

int32_t main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int t = 1;
    // std::cin >> t;
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 13796kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: -100
Wrong Answer
time: 142ms
memory: 49484kb

input:

300000 299999
80516 80517 597830404
110190 110191 82173886
218008 218009 954561262
250110 250111 942489774
66540 66541 156425292
34947 34948 239499776
273789 273790 453201232
84428 84429 439418398
98599 98600 326095035
55636 55637 355015760
158611 158612 684292473
43331 43332 43265001
171621 171622 ...

output:

1061111492

result:

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