Submission #491866

# Submission time Handle Problem Language Result Execution time Memory
491866 2021-12-04T20:53:52 Z codr0 Cities (BOI16_cities) C++14
100 / 100
5877 ms 52604 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>(mask/2);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});
			auto it=st.begin();
			while(it!=st.end()){
				pii x0=*(it);
				int wmn=x0.F;
				int umn=x0.S;
				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]=w+wmn;
						st.insert({dp[v][j],v});
					}
				}
				it++;
			}
		}
	}
    
	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 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 2 ms 4992 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2426 ms 48308 KB Output is correct
2 Correct 2264 ms 47816 KB Output is correct
3 Correct 1606 ms 42564 KB Output is correct
4 Correct 112 ms 14084 KB Output is correct
5 Correct 1971 ms 48392 KB Output is correct
6 Correct 101 ms 13920 KB Output is correct
7 Correct 10 ms 5452 KB Output is correct
8 Correct 8 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 5452 KB Output is correct
2 Correct 12 ms 5460 KB Output is correct
3 Correct 10 ms 5324 KB Output is correct
4 Correct 8 ms 5296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3418 ms 48312 KB Output is correct
2 Correct 3343 ms 47756 KB Output is correct
3 Correct 2232 ms 42564 KB Output is correct
4 Correct 1837 ms 31368 KB Output is correct
5 Correct 353 ms 16952 KB Output is correct
6 Correct 99 ms 17476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5877 ms 48320 KB Output is correct
2 Correct 5722 ms 52604 KB Output is correct
3 Correct 5515 ms 52008 KB Output is correct
4 Correct 3234 ms 44684 KB Output is correct
5 Correct 2944 ms 35560 KB Output is correct
6 Correct 490 ms 20984 KB Output is correct
7 Correct 106 ms 20800 KB Output is correct