Submission #487449

# Submission time Handle Problem Language Result Execution time Memory
487449 2021-11-15T14:53:44 Z Koosha_mv Cities (BOI16_cities) C++14
100 / 100
2480 ms 43704 KB
#include <bits/stdc++.h>
using namespace std;
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define Add(x,y) x=(x+y)%mod
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define pll pair<ll,ll>

const int N=2e5+99,K=5;
const ll inf=1e15;
 
int n,k,m,mv,a[N],imp[N],col[N];
ll ans=inf,dp[N][(1<<K)];
vector<int> v[(1<<K)];
vector<pair<int,int> > g[N];

void build(){
	f(mask1,0,(1<<k)){
		f(mask2,0,(1<<k)){
			if(((mask1&mask2)==0)){
				v[mask1].pb(mask2);
			}
		}
	}
}
void solve(int mk){
	if(nb(mk)>2) return ;
	
	priority_queue<pll, vector<pll>, greater<pll> > s;
	f(i,1,n+1){
		s.push(mp(dp[i][mk],i));
	}
	while(s.size()){
		pair<ll,int> p=s.top();
		s.pop();
		if(dp[p.S][mk]!=p.F) continue ;
		
		int u=p.S;
		f(i,0,g[u].size()){
			if(dp[u][mk]+g[u][i].S<dp[g[u][i].F][mk]){
				dp[g[u][i].F][mk]=dp[u][mk]+g[u][i].S;
				s.push(mp(dp[g[u][i].F][mk],g[u][i].F));
			}
		}
	}
	
	f(i,1,n+1){
		int u=i,mask=mk;
		f(i,0,g[u].size()){
			f(j,1,v[mask].size()){
				minm(dp[g[u][i].F][mask|v[mask][j]],dp[u][mask]+dp[g[u][i].F][v[mask][j]]+g[u][i].S);
			}
		}
	}
}

main(){
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);	
	fill(col,col+N,-1);
	cin>>n>>k>>m;
	build();
	f(i,0,k){
		cin>>imp[i];
		col[imp[i]]=i;
	}
	f(i,0,m){
		int u,v,c;
		cin>>u>>v>>c;
		g[u].pb(mp(v,c));
		g[v].pb(mp(u,c));
	}
	f(i,1,n+1){
		f(mask,0,(1<<k)){
			dp[i][mask]=inf;
		}
	}
	f(i,1,n+1){
		if(col[i]==-1){
			dp[i][0]=0;
		}
		else{
			dp[i][(1<<col[i])]=0;
		}
	}
	f(mask,0,(1<<k)){
		solve(mask);
	}
	f(i,1,n+1){
		f(mask1,0,(1<<k)){
			f(mask2,0,(1<<k)){
				int mask3=0;
				if(col[i]!=-1) mask3=(1<<col[i]);
				if((mask1|mask2|mask3)==(1<<k)-1){
					minm(ans,dp[i][mask1]+dp[i][mask2]);
				}
			}
		}
	}
	cout<<ans;
}

Compilation message

cities.cpp: In function 'void solve(int)':
cities.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   51 |   f(i,0,g[u].size()){
      |     ~~~~~~~~~~~~~~~            
cities.cpp:51:3: note: in expansion of macro 'f'
   51 |   f(i,0,g[u].size()){
      |   ^
cities.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   61 |   f(i,0,g[u].size()){
      |     ~~~~~~~~~~~~~~~            
cities.cpp:61:3: note: in expansion of macro 'f'
   61 |   f(i,0,g[u].size()){
      |   ^
cities.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   62 |    f(j,1,v[mask].size()){
      |      ~~~~~~~~~~~~~~~~~~        
cities.cpp:62:4: note: in expansion of macro 'f'
   62 |    f(j,1,v[mask].size()){
      |    ^
cities.cpp: At global scope:
cities.cpp:69:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   69 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 2 ms 5708 KB Output is correct
3 Correct 2 ms 5708 KB Output is correct
4 Correct 3 ms 5708 KB Output is correct
5 Correct 2 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 796 ms 43644 KB Output is correct
2 Correct 745 ms 43628 KB Output is correct
3 Correct 514 ms 37676 KB Output is correct
4 Correct 91 ms 10752 KB Output is correct
5 Correct 428 ms 43588 KB Output is correct
6 Correct 88 ms 10712 KB Output is correct
7 Correct 7 ms 6184 KB Output is correct
8 Correct 5 ms 6092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6092 KB Output is correct
2 Correct 7 ms 6168 KB Output is correct
3 Correct 7 ms 6220 KB Output is correct
4 Correct 6 ms 5964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1352 ms 43704 KB Output is correct
2 Correct 1310 ms 43640 KB Output is correct
3 Correct 843 ms 37792 KB Output is correct
4 Correct 923 ms 27828 KB Output is correct
5 Correct 253 ms 14612 KB Output is correct
6 Correct 165 ms 12440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2327 ms 43692 KB Output is correct
2 Correct 2480 ms 43620 KB Output is correct
3 Correct 2292 ms 43664 KB Output is correct
4 Correct 1552 ms 37668 KB Output is correct
5 Correct 1452 ms 27860 KB Output is correct
6 Correct 506 ms 14580 KB Output is correct
7 Correct 362 ms 12480 KB Output is correct