QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#907156#3770. Minimum Spanning TreeoXUoAC ✓362ms13660kbC++141.9kb2025-02-20 13:54:162025-02-20 13:54:23

Judging History

This is the latest submission verdict.

  • [2025-02-20 13:54:23]
  • Judged
  • Verdict: AC
  • Time: 362ms
  • Memory: 13660kb
  • [2025-02-20 13:54:16]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F(i,l,r) for(int i = (l);i <= (r);++i)
#define D(i,r,l) for(int i = (r);i >= (l);--i)
#define vt vector
#define mem(a,x) memset(a,x,sizeof a)
#define fi first
#define in inline
#define se second
#define pb push_back
#define bug cerr<<"LINE:"<<__LINE__<<" "
mt19937 rd(std::chrono::steady_clock::now().time_since_epoch().count());
constexpr int N = 2e5+10;
constexpr ll inf = 1e18,mod = 998244353;


ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}

void cmax(ll &x,ll y){x = max(x,y);}
void cmin(ll &x,ll y){x = min(x,y);}


int n,m;
ll l,r,a[N],b[N];

struct line{
	int x,y;ll z;
	bool operator < (const line a)const{return z < a.z;};
}c[N],e[N];
struct DSU{
	int fa[N];
	void build(){F(i,1,n)fa[i] = i;}
	int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]);}
	void merge(int x,int y){
		x = find(x),y = find(y);
		if(x == y)return;
		fa[x] = y;
	}
}U;

ll ans = inf;
void kruskal(){
	ll sum = 0;
	U.build();
	sort(e + 1,e + 1 + m);
	F(i,1,m){
		int x = e[i].x,y = e[i].y;ll z = e[i].z;
		x = U.find(x),y = U.find(y);
		if(x == y)continue;
		U.merge(x,y),sum += z;
	}
//	cout<<sum<<endl;
//	puts("");
	cmin(ans,sum);
}
void solve(){
	while(cin>>n){
		m = read(),l = read(),r = read();
		ans = inf;
		F(i,1,m){
			c[i].x = read(),c[i].y = read();
			a[i] = read(),b[i] = read();
			c[i].z = a[i] + b[i] * l;
		}
		F(i,1,m)e[i] = c[i];
		kruskal();
		F(i,1,m)e[i] = {c[i].x,c[i].y,a[i] + b[i] * r};
		kruskal();
		printf("%lld\n",ans);
	}
}

int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int t = 1;
	while(t--)solve();
	return 0;
}




Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 362ms
memory: 13660kb

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