# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
100003 | jamielim | Roller Coaster Railroad (IOI16_railroad) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "railroad.h"
using namespace std;
long long adj[20][20];
long long memo[20][66000];
long long tsp(int x,int bm){
if(memo[x][bm]!=-1)return memo[x][bm];
if(bm==0)return memo[x][bm]=0;
long long ans=1000000000000000010;
int bm2=bm;
while(bm2&(-bm2)){
int y=__builtin_ctz(bm2);
ans=min(ans,tsp(y,bm-(bm2&(-bm2)))+adj[x][y]);
bm2-=(bm2&(-bm2));
}
return memo[x][bm]=ans;
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
int n=(int)s.size();
if(n>16){
return 1;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)adj[i][j]=0;
else adj[i][j]=max(0,t[i]-s[j]);
}
}
memset(memo,-1,sizeof(memo));
long long ans=4000000000000000010;
for(int i=0;i<n;i++){
ans=min(ans,tsp(i,(1<<n)-1-(1<<i)));
}
return ans;
}
int main() {
int n;
assert(1 == scanf("%d", &n));
vector<int> s(n), t(n);
for (int i = 0; i < n; ++i)
assert(2 == scanf("%d%d", &s[i], &t[i]));
long long ans = plan_roller_coaster(s, t);
printf("%lld\n", ans);
return 0;
}