Submission #487395

# Submission time Handle Problem Language Result Execution time Memory
487395 2021-11-15T11:17:32 Z Koosha_mv Cities (BOI16_cities) C++14
74 / 100
6000 ms 43248 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

const int N=2e5+99,K=5;
const ll inf=1e15;
 
int n,k,m,a[N],imp[N],col[N];
ll ans=inf,dp[N][(1<<K)];
vector<int> v[(1<<K)];
vector<pair<int,int> > g[N];
set<pair<ll,int> > s;
 
void build(){
	f(mask1,0,(1<<k)){
		f(mask2,0,(1<<k)){
			if(((mask1&mask2)==0)){
				v[mask1].pb(mask2);
			}
		}
	}
}
void upd(int u,int mask,ll x,int mk){
	if(dp[u][mask]<=x) return ;
	if(mask==mk) s.erase(mp(dp[u][mask],u));
	dp[u][mask]=x;
	if(mask==mk) s.insert(mp(dp[u][mask],u));	
}
void upd(int u,int mask,ll x){
	s.erase(mp(dp[u][mask],u));
	dp[u][mask]=x;
	s.insert(mp(dp[u][mask],u));	
}
void solve(int mk){
	s.clear();
	f(i,1,n+1){
		s.insert(mp(dp[i][mk],i));
	}
	while(s.size()){
		pair<ll,int> p=*s.begin();
		int u=p.S,mask=mk;
		s.erase(p);
		f(i,0,g[u].size()){
			f(j,0,1){
				int nmask=v[mask][j];
				upd(g[u][i].F,mask|nmask,dp[u][mask]+dp[g[u][i].F][nmask]+g[u][i].S,mk);
			}
		}
	}
	f(i,1,n+1){
		int u=i,mask=mk;
		f(i,0,g[u].size()){
			f(j,1,v[mask].size()){
				int nmask=v[mask][j];
				//minm(dp[u][mask|nmask],dp[u][mask]+dp[g[u][i].F][nmask]+g[u][i].S);
				minm(dp[g[u][i].F][mask|nmask],dp[u][mask]+dp[g[u][i].F][nmask]+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){
		minm(ans,dp[i][(1<<k)-1]);
	}
	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++)
......
   58 |   f(i,0,g[u].size()){
      |     ~~~~~~~~~~~~~~~            
cities.cpp:58:3: note: in expansion of macro 'f'
   58 |   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++)
......
   67 |   f(i,0,g[u].size()){
      |     ~~~~~~~~~~~~~~~            
cities.cpp:67:3: note: in expansion of macro 'f'
   67 |   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++)
......
   68 |    f(j,1,v[mask].size()){
      |      ~~~~~~~~~~~~~~~~~~        
cities.cpp:68:4: note: in expansion of macro 'f'
   68 |    f(j,1,v[mask].size()){
      |    ^
cities.cpp: At global scope:
cities.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   77 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 4 ms 5708 KB Output is correct
4 Correct 3 ms 5708 KB Output is correct
5 Correct 3 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1541 ms 43248 KB Output is correct
2 Correct 1556 ms 42936 KB Output is correct
3 Correct 809 ms 40236 KB Output is correct
4 Correct 117 ms 10468 KB Output is correct
5 Correct 706 ms 43244 KB Output is correct
6 Correct 71 ms 10480 KB Output is correct
7 Correct 8 ms 6220 KB Output is correct
8 Correct 6 ms 6092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 6092 KB Output is correct
2 Correct 12 ms 6092 KB Output is correct
3 Correct 9 ms 6156 KB Output is correct
4 Correct 9 ms 5996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3335 ms 43132 KB Output is correct
2 Correct 3233 ms 42860 KB Output is correct
3 Correct 1819 ms 40236 KB Output is correct
4 Correct 1807 ms 26880 KB Output is correct
5 Correct 414 ms 13372 KB Output is correct
6 Correct 195 ms 12144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6047 ms 43092 KB Time limit exceeded
2 Halted 0 ms 0 KB -