답안 #145874

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
145874 2019-08-21T09:06:20 Z TadijaSebez 시간이 돈 (balkan11_timeismoney) C++11
100 / 100
840 ms 760 KB
#include <bits/stdc++.h>
using namespace std;
#define ldb long double
#define ll long long
#define pb push_back
#define mp make_pair
struct pt{ ll x,y;pt(){}pt(ll a, ll b):x(a),y(b){}};
pt operator - (pt a, pt b){ return pt(a.x-b.x,a.y-b.y);}
struct Edge
{
	int u,v,a,b;
	Edge(){}
	Edge(int _u, int _v, int _a, int _b):u(_u),v(_v),a(_a),b(_b){}
	ll Get(ll ma, ll mb){ return mb*a+ma*b;}
};
vector<Edge> edges;
vector<pair<int,int>> tree,res;
ll ma,mb;
bool comp(Edge x, Edge y)
{
	return x.Get(ma,mb)<y.Get(ma,mb);
}
const int N=205;
struct DSU
{
	int p[N];
	void init(){ for(int i=0;i<N;i++) p[i]=i;}
	DSU(){ init();}
	int Find(int x){ return x==p[x]?x:p[x]=Find(p[x]);}
	void Union(int x, int y){ p[Find(x)]=Find(y);}
} DS;
ll ans=9e18;
pt best;
pt MST(ll ma, ll mb)
{
	DS.init();
	tree.clear();
	::ma=ma;::mb=mb;
	sort(edges.begin(),edges.end(),comp);
	ll A=0,B=0;
	for(Edge e:edges)
	{
		if(DS.Find(e.u)!=DS.Find(e.v))
		{
			DS.Union(e.u,e.v);
			A+=e.a;
			B+=e.b;
			tree.pb({e.u,e.v});
		}
	}
	ans=min(ans,A*B);
	if(ans==A*B) best=pt(A,B),res=tree;
	return pt(A,B);
}
ll cross(pt a, pt b){ return a.x*b.y-a.y*b.x;}
void Solve(pt F, pt S)
{
	pt M=MST(S.x-F.x,F.y-S.y);
	if(cross(F-M,S-M)!=0)
	{
		Solve(F,M);
		Solve(M,S);
	}
}
int main()
{
	int n,m,u,v,a,b;
	scanf("%i %i",&n,&m);
	for(int i=1;i<=m;i++) scanf("%i %i %i %i",&u,&v,&a,&b),u++,v++,edges.pb(Edge(u,v,a,b));
	pt F=MST(0,1);
	pt S=MST(1,0);
	Solve(F,S);
	printf("%lld %lld\n",best.x,best.y);
	for(auto e:res) printf("%i %i\n",e.first-1,e.second-1);
	return 0;
}

Compilation message

timeismoney.cpp: In function 'int main()':
timeismoney.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
timeismoney.cpp:69:64: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=m;i++) scanf("%i %i %i %i",&u,&v,&a,&b),u++,v++,edges.pb(Edge(u,v,a,b));
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 252 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 8 ms 760 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 252 KB Output is correct
14 Correct 8 ms 376 KB Output is correct
15 Correct 6 ms 376 KB Output is correct
16 Correct 97 ms 376 KB Output is correct
17 Correct 102 ms 376 KB Output is correct
18 Correct 97 ms 376 KB Output is correct
19 Correct 830 ms 632 KB Output is correct
20 Correct 840 ms 632 KB Output is correct