Submission #73856

# Submission time Handle Problem Language Result Execution time Memory
73856 2018-08-29T06:57:01 Z 노영훈(#2275) Toll (APIO13_toll) C++11
0 / 100
3 ms 248 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int VMX=1010, EMX=5010;

void my_assert(bool b){
	if(b) return;
	exit(1);
}

int n, m, k, A[VMX];
vector<pii> G[VMX];
struct edge {
	int u,v,c; bool greed=false;
	void scan(){ cin>>u>>v>>c; }
	bool operator < (const edge &e) const {
		return c<e.c || (c==e.c || int(greed)>int(e.greed));
	}
} F[32];
vector<edge> E;

int U[VMX];
int find(int x){ return x==U[x] ? x : U[x]=find(U[x]); }
void unite(int x, int y){ U[find(y)]=find(x); }

vector<edge> W;
void krusk(){
	for(int i=1; i<=n; i++) G[i].clear(), U[i]=i;
	sort(W.begin(), W.end());
	for(edge e:W){
		int u=e.u, v=e.v, c=e.c;
		my_assert(0<u && 0<v);
		if(find(u)==find(v)) continue;
		unite(u,v);
		if(e.greed) c=-c;
		G[u].push_back({v,c});
		G[v].push_back({u,c});
	}
}

pii par[VMX]; int dep[VMX];

void dfs1(int v=1, int p=-1, int d=0){
	dep[v]=d;
	for(pii e:G[v]){
		int x,c; tie(x,c)=e; c=abs(c);
		if(x==p) continue;
		par[x]={v,abs(c)};
		dfs1(x,v,d+1);
	}
}

int lca_mx(int u, int v){
	int res=0;
	if(dep[u]<dep[v]) swap(u,v);
	while(dep[u]>dep[v]){
		my_assert(u>0 && v>0);
		res=max(res, par[u].second), u=par[u].first;
	}
	while(u!=v){
		my_assert(u>0 && v>0);
		res=max({res, par[u].second, par[v].second});
		u=par[u].first, v=par[v].first;
	}
	return res;
}

ll sub[VMX];

ll dfs2(int v=1, int p=-1){
	ll res=0; sub[v]=A[v];
	for(pii e:G[v]){
		int x,c; tie(x,c)=e;
		if(x==p) continue;
		res+=dfs2(x,v), sub[v]+=sub[x];
		if(c<0) res+=abs(c)*sub[x];
	}
	return res;
}

ll check(int s){
	W.clear();
	for(edge e:E) W.push_back(e);
	for(int i=0; i<k; i++)
		if(s&(1<<i)) W.push_back(F[i+1]);

	for(edge e:W) my_assert(0<e.u && 0<e.v && e.v<=n && e.u<=n);
	
	krusk();
	return G[1].size();
//	return dfs2();
}

void solve(){
	W.clear(); for(edge e:E) W.push_back(e);
	krusk();
	par[1]={-1,-1}; dfs1();

	for(int i=1; i<=n; i++) my_assert(par[i].first!=0);

	for(int i=1; i<=k; i++)
		F[i].c = lca_mx(F[i].u, F[i].v);

	ll ans=0;
	for(int i=0; i<(1<<k); i++){
		ans=max(ans, check(i));
	}
	cout<<ans<<'\n';
}

int main(){
	my_assert(false);
	ios::sync_with_stdio(0); cin.tie(0);
	cin>>n>>m>>k;
	E.resize(m);
	for(int i=1; i<=m; i++) E[i-1].scan();
	for(int i=1; i<=k; i++) cin>>F[i].u>>F[i].v, F[i].greed=true;
	for(int i=1; i<=n; i++) cin>>A[i];

	for(int i=1; i<=m; i++) my_assert(E[i-1].u!=E[i-1].v);
	for(int i=1; i<=k; i++) my_assert(F[i].u!=F[i].v);

	solve();

	return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 248 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 248 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 248 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 248 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 248 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -