QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#386726#3770. Minimum Spanning TreeieeAC ✓285ms19032kbC++231.3kb2024-04-11 19:38:582024-04-11 19:38:59

Judging History

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

  • [2024-04-11 19:38:59]
  • 评测
  • 测评结果:AC
  • 用时:285ms
  • 内存:19032kb
  • [2024-04-11 19:38:58]
  • 提交

answer

#include<bits/stdc++.h>
#define N 400005
#define LL long long
#define int LL
#define SZ(x) ((LL)(x.size()))
using namespace std;
long long read(){
	long long q=0,w=1;
	char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){q=q*10+(ch-'0');ch=getchar();}
	return q*w;
}
void write(LL x){
	if(x<0){putchar('-');x=(-x);}
	if(x>9)write(x/10);
	putchar('0'+x%10);
}
void writeln(LL x){write(x);puts("");}
void writecs(LL x){write(x);putchar(' ');}

struct Edge{int u,v,a,b,w;}e[N];

int n,m,l,r;

namespace DSU{
	int fa[N],siz[N];
	int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
	bool Union(int x,int y){
		if((x=Find(x))==(y=Find(y)))return false;
		if(siz[x]>siz[y])swap(x,y);
		siz[fa[x]=y]+=siz[x];
		return true;
	}
}
using namespace DSU;

int solve(int x){
	int ans=0;
	for(int i=1;i<=m;i++)e[i].w=e[i].a+e[i].b*x;
	for(int i=1;i<=n;i++)siz[fa[i]=i]=1;
	sort(e+1,e+m+1,[](Edge x,Edge y){return x.w<y.w;});
	for(int i=1;i<=m;i++)
		if(Union(e[i].u,e[i].v))ans+=e[i].w;
	return ans;
}

signed main(){
	while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
		for(int i=1;i<=m;i++)e[i].u=read(),e[i].v=read(),e[i].a=read(),e[i].b=read();
		printf("%lld\n",min(solve(l),solve(r)));
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 285ms
memory: 19032kb

input:

5 6 1 5
1 2 3 1
2 3 5 -1
3 4 1 2
4 5 1 -1
5 1 5 3
2 4 3 -1
5 6 1 5
1 2 1 1
2 3 1 2
3 4 1 -10
3 4 2 10
5 1 3 10
2 4 5 -10
10 10 23 47
5 8 43 13
9 5 55 43
4 9 84 -35
5 10 78 -70
6 4 15 70
7 3 15 67
1 7 2 58
6 7 86 20
5 7 77 7
2 9 32 -91
4 15 82 98
1 2 95 12
4 3 28 100
3 1 98 -25
2 3 39 12
1 2 21 5
1 4...

output:

2
-35
748
-24308
-5125
-4533
-10093
-13414
-13237
-8499
-6782
-6151
-15031
-13554
-50866
-2745
-14186
-5363
-13339
-23410
-5161
-12841
-7280
-22485
-15045
6008
-13410
-17388
-21254
-19576
-10606
-16471
-9741
-37226
-24158
-9383
-20665
-17547
-7919
-11201
-6651
-18673
-5076
-12809
-7285
-18708
-5878
...

result:

ok 53406 lines