Submission #491865

# Submission time Handle Problem Language Result Execution time Memory
491865 2021-12-04T20:50:45 Z codr0 Cities (BOI16_cities) C++14
0 / 100
6000 ms 44484 KB
// Code by Parsa Eslami
     
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define pb push_back
#define SZ(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define err(x) cerr<<#x<<" : "<<x<<'\n'
#define MOD(x) if(x>=M) x-=M; if(x<0) x+=M;
 
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int N=1e5+4;
const ll INF=1e15+100;
const int PW=32;
int kh[5];
int n,k,m;
vector<int> adj[N];
vector<int> W[N];
ll dp[N][PW];
    
    void pre(){
		FOR(i,0,4) kh[i]=-1;
		FOR(i,1,n) FOR(j,0,PW-1) dp[i][j]=INF;
	}
    
    int LOG(int x){
		int i=0;
		x/=2;
		while(x){
			i++;
			x/=2;
		}
		return i;
	}
    
    void FILL(){
		FOR(i,1,n) dp[i][0]=0;
		FOR(j,1,PW-1){
			FOR(i,1,n){
			if(j==(j&(-j))){
				if(i==kh[LOG(j)]) dp[i][j]=0;
				continue;
			}
			int mask=j;
			for(int pw=mask;pw>(mask/2);pw=(pw-1)&mask){
				dp[i][j]=min(dp[i][j],dp[i][pw]+dp[i][(pw^j)]);
			}
			}
			set<pll> st;
			FOR(i,1,n) st.insert({dp[i][j],i});
			while(!st.empty()){
				pll x0=*(st.begin());
				int wmn=x0.F;
				int umn=x0.S;
				st.erase(x0);
				FOR(i,0,SZ(adj[umn])-1){
					int v=adj[umn][i];
					int w=W[umn][i];
					if(dp[v][j]>w+wmn){
						st.erase({dp[v][j],v});
						dp[v][j]=min(dp[v][j],1LL*w+wmn);
						st.insert({dp[v][j],v});
					}
				}
			}
		}
	}
    
	int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	cin>>n>>k>>m;
	pre();
	FOR(i,0,k-1) cin>>kh[i];
	FOR(i,1,m){
		int u,v,w;
		cin>>u>>v>>w;
		adj[u].pb(v);
		adj[v].pb(u);
		W[u].pb(w);
		W[v].pb(w);
	}
	FILL();
	int k2=pow(2,k)-1;
	ll mn=INF;
	FOR(i,1,n) mn=min(mn,dp[i][k2]);
	cout<<mn<<'\n';
	
	
	return 0;
    }
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Execution timed out 6043 ms 4940 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6053 ms 44472 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6072 ms 5392 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6062 ms 44484 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6073 ms 44484 KB Time limit exceeded
2 Halted 0 ms 0 KB -