Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)
用spfa,和dp是一样的。转移只和最后一个吃的dish和吃了哪些有关。
把松弛改成变长。因为是DAG,所以一定没环。操作最多有84934656,514ms跑过,实际远远没这么多。
脑补过一下费用流,但是限制流量不能保证吃到m个菜
#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; }