Submission #73876

# Submission time Handle Problem Language Result Execution time Memory
73876 2018-08-29T07:51:59 Z 노영훈(#2275) Toll (APIO13_toll) C++11
16 / 100
15 ms 544 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> G1[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;
typedef pair<edge, int> pei;
/*
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); }
*/
bool done[VMX];

pei get_pei(int v, pii p){
	edge e; e.u=v, e.v=p.first, e.c=abs(p.second), e.greed=p.second<0;
	pei res={e,A[p.first]};
	return res;
}

vector<edge> W;
void mst(){
	static vector<pii> G0[VMX];
	static priority_queue<pei, vector<pei>, greater<pei> > Q;
	for(int i=1; i<=n; i++) G1[i].clear(), G0[i].clear(), done[i]=false;
	while(!Q.empty()) Q.pop();

	for(edge &e:W){
		int u=e.u, v=e.v, c=e.c;
		if(e.greed) c=-c;
		G0[u].push_back({v,c});
		G0[v].push_back({u,c});
	}

	done[1]=true; for(pii p:G0[1]) Q.push(get_pei(1,p));

	while(!Q.empty()){
		edge e=Q.top().first; Q.pop();
		int u=e.u, v=e.v, c=e.c; if(e.greed) c=-c;
		if(done[v]) swap(u,v);
		if(done[v]) continue;

		done[v]=true;
		G1[u].push_back({v,c});
		G1[v].push_back({u,c});

		for(pii p:G0[v]) Q.push(get_pei(v,p));
	}
}

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

void dfs1(int v=1, int p=-1, int d=0){
	dep[v]=d;
	for(pii e:G1[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]){
		res=max(res, par[u].second), u=par[u].first;
	}
	while(u!=v){
		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:G1[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]);
	
	mst();
	return dfs2();
}

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

	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(){
	ios::sync_with_stdio(0); cin.tie(0);
	cin>>n>>m>>k;
	for(int i=1; i<=m; i++){
		edge e; e.scan();
		e.greed=false;
		E.push_back(e);
	}
	for(int i=1; i<=k; i++) cin>>F[i].u>>F[i].v, F[i].greed=true, F[i].c=0;
	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 Correct 2 ms 380 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Incorrect 15 ms 544 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Incorrect 15 ms 544 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Incorrect 15 ms 544 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Incorrect 15 ms 544 KB Output isn't correct
4 Halted 0 ms 0 KB -