QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#278702 | #7880. Streak Manipulation | ucup-team073# | RE | 0ms | 0kb | C++20 | 1.8kb | 2023-12-07 19:27:09 | 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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
8 3 2 10110110