Submission #491862

# Submission time Handle Problem Language Result Execution time Memory
491862 2021-12-04T20:44:15 Z codr0 Cities (BOI16_cities) C++14
74 / 100
6000 ms 52672 KB
// Code by Parsa Eslami
     
#include <bits/stdc++.h>
#define int long long
#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 pair<int,int> pii;
const int N=1e5+4;
const int INF=1e15+100;
const int PW=32;
int kh[5];
int n,k,m;
vector<int> adj[N];
vector<int> W[N];
int 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>0;pw=(pw-1)&mask){
				dp[i][j]=min(dp[i][j],dp[i][pw]+dp[i][(pw^j)]);
			}
			}
			set<pii> st;
			FOR(i,1,n) st.insert({dp[i][j],i});
			while(!st.empty()){
				pii 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],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;
	int 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 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4968 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3121 ms 52536 KB Output is correct
2 Correct 2731 ms 52020 KB Output is correct
3 Correct 2080 ms 44740 KB Output is correct
4 Correct 103 ms 17484 KB Output is correct
5 Correct 2332 ms 52672 KB Output is correct
6 Correct 108 ms 17348 KB Output is correct
7 Correct 11 ms 5452 KB Output is correct
8 Correct 9 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 5496 KB Output is correct
2 Correct 14 ms 5492 KB Output is correct
3 Correct 10 ms 5412 KB Output is correct
4 Correct 9 ms 5320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4185 ms 52636 KB Output is correct
2 Correct 3922 ms 51980 KB Output is correct
3 Correct 2625 ms 44684 KB Output is correct
4 Correct 2063 ms 35736 KB Output is correct
5 Correct 369 ms 20884 KB Output is correct
6 Correct 98 ms 20932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6092 ms 52420 KB Time limit exceeded
2 Halted 0 ms 0 KB -