QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661724#7880. Streak ManipulationsdnuwyRE 0ms0kbC++172.6kb2024-10-20 17:47:052024-10-20 17:47:05

Judging History

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

  • [2024-10-20 17:47:05]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-20 17:47:05]
  • 提交

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: 0
Runtime Error

input:

8 3 2
10110110

output:


result: