QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#278702#7880. Streak Manipulationucup-team073#RE 0ms0kbC++201.8kb2023-12-07 19:27:092023-12-07 19:27:09

Judging History

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

  • [2023-12-07 19:27:09]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-12-07 19:27:09]
  • 提交

answer

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

struct edge{
  int to,nxt,val;
}e[2000010];
int cnt,head[2000010];
void add(int u,int v,int w){
  //cout<<u<<' '<<v<<' '<<w<<endl;
  e[++cnt].to=v;
  e[cnt].nxt=head[u];
  e[cnt].val=w;
  head[u]=cnt;
}
int fa[2000010];
int find(int u){return u==fa[u]?u:fa[u]=find(fa[u]);}
int merge(int u,int v){
  u=find(u),v=find(v);
  fa[u]=v;
  return 1;
}
int n,m;
struct Edge{
  int u,v,w;
  int flag;
  bool operator<(Edge b)const{return w<b.w;}
}ed[2000010];
int f[2000010],g[2000010];
int ans=1e18;
signed main(){
  cin>>n>>m;
  for(int i=1;i<=m;++i){
    cin>>ed[i].u>>ed[i].v>>ed[i].w;
    if(ed[i].u==1&&ed[i].v==n)ans=min(ans,ed[i].w);
    if(ed[i].u==n&&ed[i].v==1)ans=min(ans,ed[i].w);
  }
  sort(ed+1,ed+m+1);
  for(int i=1;i<=n;++i)fa[i]=i;
  for(int i=1;i<=m;++i){
    if(merge(ed[i].u,ed[i].v)&&find(1)!=find(n))add(ed[i].u,ed[i].v,ed[i].w),add(ed[i].v,ed[i].u,ed[i].w);
    else break;
    ed[i].flag=1;
  }
  priority_queue<pair<int,int>>q;
  memset(f,0x3f,sizeof(f));
  memset(g,0x3f,sizeof(g));
  f[1]=g[n]=0;
  q.emplace(0,1);
  while(!q.empty()){
    int u=q.top().second;q.pop();
    for(int i=head[u];i;i=e[i].nxt){
      int v=e[i].to;
      if(f[v]>max(f[u],e[i].val)){
        f[v]=max(f[u],e[i].val);
        q.emplace(-f[v],v);
      }
    }
  }
  q.emplace(0,n);
  while(!q.empty()){
    int u=q.top().second;q.pop();
    for(int i=head[u];i;i=e[i].nxt){
      int v=e[i].to;
      if(g[v]>max(g[u],e[i].val)){
        g[v]=max(g[u],e[i].val);
        q.emplace(-g[v],v);
      }
    }
  }
  //for(int i=1;i<=n;++i)cout<<f[i]<<' '<<g[i]<<endl;
  for(int i=1;i<=m;++i)if(!ed[i].flag){
    ans=min(ans,ed[i].w+max(f[ed[i].u],g[ed[i].v]));
    ans=min(ans,ed[i].w+max(g[ed[i].u],f[ed[i].v]));
  }
  cout<<ans<<endl;
  return 0;
}

详细

Test #1:

score: 0
Runtime Error

input:

8 3 2
10110110

output:


result: