답안 #889463

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
889463 2023-12-19T19:10:07 Z WonderfulWhale 시간이 돈 (balkan11_timeismoney) C++17
0 / 100
5 ms 2772 KB
    #include<bits/stdc++.h>
    using namespace std;
     
    #define int int64_t
    #define pb push_back
    #define pii pair<int, int>
    #define st first
    #define nd second
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
     
    int n, m;
    vector<pair<int, pii>> G[209];
    vector<pair<pii, pii>> edges;
    vector<int> cur;
    vector<int> G2[209];
    bool used[10009];
     
    bool vis[209];
     
    int p[209];
    int Find(int x) {
    	return p[x]==x?x:p[x]=Find(p[x]);
    }
    void Union(int a, int b) {
    	p[Find(a)] = Find(b);
    }
     
    void dfs(int x, int y) {
    	vis[x] = true;
    	for(int z:G2[x])
    		if(z!=y) 
    			dfs(z, x);
    }
     
    bool improve() {
    	int sum1=0, sum2=0;
    	for(int i=0;i<n;i++) G2[i].clear();
    	for(int i=0;i<n-1;i++) {
    		auto e = edges[cur[i]];
    		G2[e.st.st].pb(e.st.nd);
    		G2[e.st.nd].pb(e.st.st);
    		sum1 += e.nd.st, sum2 += e.nd.nd;
    	}
    	tuple<int, int, int> Opt = {-1, -1, sum1*sum2};
    	for(int i=0;i<n-1;i++) {
    		auto e = edges[cur[i]];
    		memset(vis, 0, sizeof(vis));
    		dfs(e.st.st, e.st.nd);
    		for(int j=0;j<m;j++) {
    			if(!used[j]) {
    				auto ne = edges[j];
    				if(vis[ne.st.st]^vis[ne.st.nd]) {
    					int nsum1 = sum1-e.nd.st+ne.nd.st;
    					int nsum2 = sum2-e.nd.nd+ne.nd.nd;
    					if(nsum1*nsum2<(get<2>(Opt))) {
    						Opt = {i, j, nsum1*nsum2};
    					}
    				}
    			}
    		}
    	}
    	if(get<0>(Opt)==-1) return false;
    	auto [a, b, c] = Opt;
    	used[cur[a]] = false;
    	cur[a] = b;
    	used[b] = true;
    	return true;
    }
    	
    int32_t main() {
    	ios_base::sync_with_stdio(false);
    	cin.tie(NULL);
     
    	srand(666);
     
    	cin >> n >> m;
    	for(int i=0;i<m;i++) {
    		int a, b, c, d;
    		cin >> a >> b >> c >> d;
    		G[a].pb({b, {c, d}});
    		G[b].pb({a, {c, d}});
    		edges.pb({{c, d}, {a, b}});
    	}
    	int SUM1=1e9, SUM2=1e9;
    	vector<pii> ANS;
    	for(int k=2;k<3;k++) {
    		iota(p, p+n, 0);
    		if(k==0) {
    			sort(all(edges));
    			for(auto &[x, y]:edges) swap(x, y);
    		} else if(k==1) {
    			for(auto &[x, y]:edges) {
    				swap(x, y);
    				swap(x.st, x.nd);
    			}
    			sort(all(edges));
    			for(auto &[x, y]:edges) swap(x, y);
    		} else if(k==2) {
    			for(auto &[x, y]:edges) {
    				swap(x, y);
    				x.st *= x.nd;
    			}
    			sort(all(edges));
    			for(auto &[x, y]:edges) {
    				x.st /= x.nd;
    				swap(x, y);
    			}
    		}
    		cur.clear();
    		memset(used, 0, sizeof(used));
    		for(int i=0;i<m;i++) {
    			auto [x, y] = edges[i];
    			if(Find(x.st)!=Find(x.nd)) {
    				used[i] = true;
    				cur.pb(i);
    				Union(x.st, x.nd);
    			} 
    		}
    		while(improve()) {}
    		int sum1=0, sum2=0;
    		vector<pii> ans;
    		for(int x:cur) {
    			sum1 += edges[x].nd.st;
    			sum2 += edges[x].nd.nd;
    			ans.pb(edges[x].st);
    		}
    		if(sum1*sum2<SUM1*SUM2) {
    			SUM1 = sum1;
    			SUM2 = sum2;
    			ANS = ans;
    		}
    	}
    	// cout << SUM1*SUM2 << "\n";
    	cout << SUM1 << " " << SUM2 << "\n";
    	for(pii x:ANS) cout << x.st << " " << x.nd << "\n";
    }
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Runtime error 1 ms 604 KB Execution killed with signal 8
3 Runtime error 1 ms 604 KB Execution killed with signal 11
4 Runtime error 1 ms 604 KB Execution killed with signal 8
5 Runtime error 1 ms 604 KB Execution killed with signal 8
6 Runtime error 1 ms 604 KB Execution killed with signal 11
7 Runtime error 2 ms 1116 KB Execution killed with signal 8
8 Runtime error 5 ms 2772 KB Execution killed with signal 8
9 Runtime error 1 ms 604 KB Execution killed with signal 8
10 Runtime error 1 ms 608 KB Execution killed with signal 8
11 Runtime error 1 ms 604 KB Execution killed with signal 11
12 Runtime error 1 ms 604 KB Execution killed with signal 11
13 Runtime error 1 ms 604 KB Execution killed with signal 8
14 Runtime error 1 ms 604 KB Execution killed with signal 8
15 Runtime error 1 ms 604 KB Execution killed with signal 11
16 Runtime error 2 ms 996 KB Execution killed with signal 8
17 Runtime error 2 ms 1116 KB Execution killed with signal 8
18 Runtime error 1 ms 1116 KB Execution killed with signal 8
19 Runtime error 5 ms 2772 KB Execution killed with signal 8
20 Runtime error 5 ms 2772 KB Execution killed with signal 8