답안 #586759

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
586759 2022-06-30T16:34:51 Z 1ne 통행료 (APIO13_toll) C++14
16 / 100
1 ms 212 KB
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y,cost;
};
struct DSU{
	vector<int>parent,sz;
	//vector<pair<int,int>>ops;
	vector<int>maxxy;
	set<pair<int,int>>new_edge;
	vector<vector<pair<int,int>>>adj;
	vector<vector<int>>comp;
	void build(int n){
		parent.resize(n);
		sz.resize(n);
		adj.resize(n);
		comp.resize(n);
		for (int i = 0;i<n;++i){
			parent[i] = i;
			sz[i] = 1;
			comp[i].push_back(i);
		}
	}
	void add_edge(int u,int v){
		new_edge.insert({max(u,v),min(u,v)});
	}
	int findsets(int v){
		if (v == parent[v])return v;
		parent[v] = findsets(parent[v]);
		return parent[v];
	}
	bool unionset(int a,int b,int cost){
		int u = findsets(a);
		int v = findsets(b);
		if (u == v)return false;
		if (sz[u] < sz[v])swap(u,v);
		pair<int,int>pos ={-1,-1};
		for (auto x:comp[u]){
			for (auto y:comp[v]){
				if (new_edge.find({max(x,y),min(x,y)})!=new_edge.end()){
					pos ={x,y};
					new_edge.erase({max(x,y),min(x,y)});
				}
			}
		}
		if (pos.first!=-1){
			adj[pos.first].push_back({pos.second,cost});
			adj[pos.second].push_back({pos.first,cost});
		}
		else{
			adj[a].push_back({b,0});
			adj[b].push_back({a,0});
		}
		//ops.push_back({v,parent[v]});
		parent[v] = u;
		//ops.push_back({~u,sz[u]});
		sz[u]+=sz[v];
		comp[u].insert(comp[u].end(),comp[v].begin(),comp[v].end());
		comp[v].clear();
		return true;
	}
	/*void roll_back(int v){
		while((int)ops.size() > v){
			int i = ops.back().first;
			int j = ops.back().second;
			ops.pop_back();
			if (i >= 0){
				parent[i] = j;
			}
			else{
				sz[~i] = j;
			}
		}
	}
	*/
};
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m,k;cin>>n>>m>>k;
	vector<node>edge(m);
	for (int i = 0;i<m;++i){
		int x,y,cost;
		cin>>x>>y>>cost;
		--x;
		--y;
		edge.push_back({x,y,cost});
	}
	sort(edge.begin(),edge.end(),[&](auto x,auto y){
		return x.cost < y.cost;
	});
	DSU st;
	st.build(n);
	for (int i = 0;i<k;++i){
		int x,y;cin>>x>>y;
		--x;
		--y;
		st.add_edge(x,y);
	}
	vector<int>p(n);
	for (int i = 0;i<n;++i)cin>>p[i];
	for (auto x:edge){
		st.unionset(x.x,x.y,x.cost);
	}
	function<long long(int,int,long long)>dfs = [&](int u,int par,long long cost){
		long long ans = cost * p[u];
		for (auto x:st.adj[u]){
			if (x.first!=par){
				ans+=dfs(x.first,u,cost + x.second);
			}
		}
		return ans;
	};
	long long ans = dfs(0,-1,0);
	cout<<ans<<'\n';
	return 0;
}
/*
5 5 1
3 5 2 
1 2 3
2 3 5
2 4 4
4 3 6
1 3
10 20 30 40 50
*/

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -