#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
int inf=1000000007;
int mod=1000000007;
int mod1=998244353;
const int N=207,M=10007;
int U[M],V[M],X[M],Y[M];
bool cmp(pair<int,int>L,pair<int,int>R)
{
return L.st*R.nd<R.st*L.nd;
}
vector<int>W;
int n;
int r[N];
int Find(int v)
{
if(r[v]==v) return v;
return r[v]=Find(r[v]);
}
vector<pair<int,int>>ans,act;
int sX,sY,bX,bY,res=2*inf;
void Union(int u,int v,int i)
{
int uu=Find(u),vv=Find(v);
if(uu==vv) return ;
r[uu]=vv;
act.pb({u,v});
sX+=X[i];
sY+=Y[i];
}
void calc()
{
for(int i=0;i<n;i++) r[i]=i;
act.clear();
sX=0;
sY=0;
for(auto i:W)
{
Union(U[i],V[i],i);
if(sX*sY>=res) break;
}
if(sX*sY<res)
{
res=sX*sY;
ans=act;
bX=sX;
bY=sY;
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m;
cin>>n>>m;
vector<pair<int,int>>ord;
for(int i=0;i<m;i++)
{
cin>>U[i]>>V[i]>>X[i]>>Y[i];
ord.pb({X[i],i});
}
sort(all(ord));
for(auto [c,x]:ord) W.pb(x);
vector<pair<int,int>>F;
F.pb({0,1});
for(int a=1;a<=255;a++) for(int b=1;b<=255;b++) if(__gcd(a,b)==1) F.pb({a,b});
sort(all(F),cmp);
calc();
for(auto [a,b]:F)
{
for(int i=0;i<m;i++)
{
int j=i;
while(j>0&&a*X[W[j]]+b*Y[W[j]]<a*X[W[j-1]]+b*Y[W[j-1]])
{
swap(W[j],W[j-1]);
j--;
}
}
calc();
}
cout<<bX<<" "<<bY<<endl;
for(auto [x,y]:ans) cout<<x<<" "<<y<<endl;
return 0;
}
Compilation message
timeismoney.cpp: In function 'int main()':
timeismoney.cpp:82:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | for(auto [c,x]:ord) W.pb(x);
| ^
timeismoney.cpp:88:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
88 | for(auto [a,b]:F)
| ^
timeismoney.cpp:102:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
102 | for(auto [x,y]:ans) cout<<x<<" "<<y<<endl;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
984 KB |
Output is correct |
2 |
Correct |
11 ms |
992 KB |
Output is correct |
3 |
Correct |
18 ms |
992 KB |
Output is correct |
4 |
Correct |
41 ms |
992 KB |
Output is correct |
5 |
Correct |
155 ms |
988 KB |
Output is correct |
6 |
Correct |
162 ms |
988 KB |
Output is correct |
7 |
Correct |
392 ms |
992 KB |
Output is correct |
8 |
Correct |
1312 ms |
1564 KB |
Output is correct |
9 |
Correct |
11 ms |
992 KB |
Output is correct |
10 |
Correct |
14 ms |
992 KB |
Output is correct |
11 |
Correct |
18 ms |
992 KB |
Output is correct |
12 |
Correct |
39 ms |
1244 KB |
Output is correct |
13 |
Correct |
45 ms |
988 KB |
Output is correct |
14 |
Correct |
129 ms |
988 KB |
Output is correct |
15 |
Correct |
166 ms |
984 KB |
Output is correct |
16 |
Correct |
451 ms |
992 KB |
Output is correct |
17 |
Correct |
468 ms |
992 KB |
Output is correct |
18 |
Correct |
450 ms |
1248 KB |
Output is correct |
19 |
Correct |
1746 ms |
1564 KB |
Output is correct |
20 |
Correct |
1623 ms |
1564 KB |
Output is correct |