답안 #417377

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
417377 2021-06-03T15:49:32 Z kshitij_sodani 통행료 (APIO13_toll) C++14
16 / 100
158 ms 163844 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'


int t;
int par[1000001];
int par2[1000001];
vector<int> adj[1000001];
int it[1000001];

int find(int no){
	if(par[no]==no){
		return par[no];
	}
	par[no]=find(par[no]);
	return par[no];
}
int co=0;
int st[1000001];
int endd[1000001];
llo ss[100001];
llo val[100001];
//int par2[100001];
void dfs(int no,int par=-1){
	st[no]=co;
	co++;
	par2[no]=par;
	ss[no]=it[no];
	for(auto j:adj[no]){
		if(j!=par){
			dfs(j,no);
			ss[no]+=ss[j];
		}
	}
	endd[no]=co-1;
}
bool ispar(int x,int y){
	return st[x]<=st[y] and endd[x]>=endd[y];
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m,k;
	cin>>n>>m>>k;
	vector<pair<int,pair<int,int>>> ed;
	for(int i=0;i<m;i++){
		int aa,bb,cc;
		cin>>aa>>bb>>cc;
		aa--;
		bb--;
		ed.pb({cc,{aa,bb}});
	}
	sort(ed.begin(),ed.end());
	vector<pair<int,int>> zz;
	for(int i=0;i<k;i++){
		int aa,bb;
		cin>>aa>>bb;
		aa--;
		bb--;
		zz.pb({aa,bb});
	}
	for(int i=0;i<n;i++){
		cin>>it[i];
	}
	llo ans=0;
	vector<pair<int,pair<int,int>>> dd;
	for(int i=1;i<(1<<k);i++){
		for(int j=0;j<n;j++){
			par[j]=j;
			adj[i].clear();
		}
		int st=1;
		for(int j=0;j<k;j++){
			if((1<<j)&i){
				int x=find(zz[j].a);
				int y=find(zz[j].b);
				if(x==y){
					st=0;
					break;
				}
				par[x]=y;
				adj[zz[j].a].pb(zz[j].b);
				adj[zz[j].b].pb(zz[j].a);

			}
		}
		if(st==0){
			continue;
		}
		dd.clear();
		for(auto j:ed){
			int x=find(j.b.a);
			int y=find(j.b.b);
			if(x!=y){
				par[x]=y;
				adj[j.b.a].pb(j.b.b);
				adj[j.b.b].pb(j.b.a);
			}
			else{
				dd.pb(j);
			}
		}
		//continue;
		co=0;
		dfs(0);
		for(int j=0;j<n;j++){
			par[j]=par2[j];
			val[j]=1e9;
		}
		//continue;
		for(auto j:dd){
			int x=j.b.a;//find(j.b.a);
			int y=j.b.b;//find(j.b.b);
			int cur=x;
			vector<int> xx;
			//continue;
			if(!ispar(x,y)){
				while(!ispar(cur,y)){
					xx.pb(cur);

					//cout<<cur<<",";
					cur=par[cur];	
				}
				//cout<<endl;
			}
			for(int ii=0;ii<xx.size();ii++){
				val[xx[ii]]=min(val[xx[ii]],(llo)j.a);
				par[xx[ii]]=cur;
			}
			cur=y;
			xx.clear();
			if(!ispar(y,x)){
				while(!ispar(cur,x)){
					xx.pb(cur);
					cur=par[cur];	
				}
			}
			for(int ii=0;ii<xx.size();ii++){
				val[xx[ii]]=min(val[xx[ii]],(llo)j.a);
				par[xx[ii]]=cur;
			}
		}
		llo su=0;
		for(int j=0;j<k;j++){
			if((1<<j)&i){
				if(ispar(zz[j].a,zz[j].b)){
					su+=ss[zz[j].b]*val[zz[j].b];
				}
				else{
					su+=ss[zz[j].a]*val[zz[j].a];
				}
			}
		}
		ans=max(ans,su);
	}
	cout<<ans<<endl;











	return 0;
}
 

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:133:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |    for(int ii=0;ii<xx.size();ii++){
      |                 ~~^~~~~~~~~~
toll.cpp:145:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |    for(int ii=0;ii<xx.size();ii++){
      |                 ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 17 ms 23940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 17 ms 23940 KB Output is correct
3 Runtime error 158 ms 163844 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 17 ms 23940 KB Output is correct
3 Runtime error 158 ms 163844 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 17 ms 23940 KB Output is correct
3 Runtime error 158 ms 163844 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 17 ms 23940 KB Output is correct
3 Runtime error 158 ms 163844 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -