标签:
用spfa,和dp是一样的。转移只和最后一个吃的dish和吃了哪些有关。
把松弛改成变长。因为是DAG,所以一定没环。状态最多有84934656,514ms跑过,cf机子就是快。
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> nd; typedef long long ll; #define fi first #define se second int n,m,a[18]; int g[18][18]; const int maxs = 1<<18; ll d[maxs][18]; bool vis[maxs][18]; inline int bitcount(int s) { int ct = 0; while(s){ ct += s&1; s >>= 1; } return ct; } ll spfa() { queue<nd>q; ll ans = 0; for(int i = 0; i < n; i++){ q.push(nd(1<<i,i)); d[1<<i][i] = a[i]; vis[1<<i][i] = true; } while(q.size()){ nd &u = q.front(); vis[u.fi][u.se] = false; int bc = bitcount(u.fi); if(bc == m) { ans = max(ans,d[u.fi][u.se]); q.pop(); continue; } for(int i = 0; i < n; i++){ if(1&(u.fi>>i)) continue; int ns = 1<<i|u.fi; if(d[ns][i] < d[u.fi][u.se] + g[u.se][i] + a[i]){ d[ns][i] = d[u.fi][u.se] + g[u.se][i] + a[i]; if(!vis[ns][i]){ q.push(nd(ns,i)); vis[ns][i] = true; } } } q.pop(); } return ans; } int main() { //freopen("in.txt","r",stdin); int k;scanf("%d%d%d",&n,&m,&k); for(int i = 0; i < n; i++){ scanf("%d",a+i); } while(k--){ int x,y; scanf("%d%d",&x,&y); scanf("%d",g[x-1]+y-1); } printf("%I64d",spfa()); return 0; }
Codeforces Round #321 (Div. 2) D Kefa and Dishes
标签:
原文地址:http://www.cnblogs.com/jerryRey/p/4831311.html