QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#682348#7883. Takeout DeliveringWedonotLikeStudying#ML 2ms23204kbC++231.6kb2024-10-27 15:06:422024-10-27 15:06:42

Judging History

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

  • [2024-10-27 15:06:42]
  • 评测
  • 测评结果:ML
  • 用时:2ms
  • 内存:23204kb
  • [2024-10-27 15:06:42]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define ll long long
#define vi vector<int>
#define pb emplace_back
using namespace std;

template<class T>inline void read(T &x) {
	T f=1; x=0; char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	x*=f;
}

const int N=3e5;
const int M=1e6;
struct Edge {
	int u,v,w;
}e[M+5];
inline bool cmp(Edge a,Edge b) {
	return a.w<b.w;
}

int d1[N+5],d2[N+5];
int bcj[N+5],Ans,n,m;
set<int> st[N+5];
inline int find(int x) {
	if(x==bcj[x]) return x;
	return bcj[x]=find(bcj[x]);
}

inline void upd(int x,int y,int z) {
	if(find(1)!=find(x)) return;
	if(find(n)!=find(y)) return;
	Ans=min(Ans,z+max(d1[x],d2[y]));
}

inline void get(int x,int z,int *d) {
	for(int x:st[x]) d[x]=z;
}

inline void solve() {
	read(n); read(m);
	rep(i,1,m) {
		read(e[i].u);
		read(e[i].v);
		read(e[i].w);
	}
	sort(e+1,e+m+1,cmp);
	rep(i,1,n) bcj[i]=i,st[i].insert(i);
	Ans=0x7fffffff;
	rep(i,1,m) {
		upd(e[i].u,e[i].v,e[i].w);
		upd(e[i].v,e[i].u,e[i].w);
		int x=e[i].u,y=e[i].v;
		if(find(x)==find(y)) continue;
		if(find(1)==find(x)) get(find(y),e[i].w,d1);
		if(find(1)==find(y)) get(find(x),e[i].w,d1);
		if(find(n)==find(x)) get(find(y),e[i].w,d2);
		if(find(n)==find(y)) get(find(x),e[i].w,d2);
		x=find(x); y=find(y);
		if(st[x].size()>st[y].size()) swap(x,y);
		bcj[x]=y;
		for(int X:st[x]) st[y].insert(X);
	}
	printf("%d\n",Ans);
}

int main() {
	//int T; read(T); while(T--)
	solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 23204kb

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
Memory Limit Exceeded

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:

1999991697

result: