hdu_5711_Ingress(TSP+贪心)
题目连接:hdu5711
这题是 HDU 女生赛最后一题,TSP+贪心,确实不好想,看了wkc巨巨的题解,然后再做的
题解传送门:Ingress
1 #include<cstdio> 2 #include<queue> 3 #include<algorithm> 4 #define F(i,a,b) for(int i=a;i<=b;i++) 5 using namespace std; 6 typedef pair<int,int> P; 7 inline void up(int &x,int y){if(x>y)x=y;} 8 9 int ic=1,t,n,m,ans,k,en,l,u,v,c,inf=1e9+7; 10 int a[20],b[20],g[20][20],dp[1<<16][17],vis[1<<16]; 11 12 int main(){ 13 scanf("%d",&t); 14 while(t--){ 15 scanf("%d%d%d%d",&n,&m,&k,&l);en=(1<<n)-1; 16 F(i,0,n)F(j,0,n)g[i][j]=(i==j?0:inf); 17 F(i,0,en)F(j,0,n)dp[i][j]=inf; 18 F(i,0,en)vis[i]=0; 19 F(i,1,n)scanf("%d",a+i); 20 F(i,1,n)scanf("%d",b+i); 21 F(i,1,m)scanf("%d%d%d",&u,&v,&c),up(g[u][v],c),up(g[v][u],c); 22 F(kk,0,n)F(i,0,n)F(j,0,n)up(g[i][j],g[i][kk]+g[kk][j]);//Floyd 23 dp[0][0]=0,ans=0; 24 F(i,0,en){ 25 F(j,0,n)if(dp[i][j]<=l){ 26 if(dp[i][j]+g[j][0]<=l)vis[i]=1; 27 F(q,1,n)if(q==j||i&1<<q-1)continue; 28 else up(dp[i|1<<q-1][q],dp[i][j]+g[j][q]); 29 } 30 if(vis[i]){ 31 priority_queue<P>Q; 32 F(j,1,n)if(i&1<<j-1)Q.push(P(a[j],b[j])); 33 if(Q.empty())continue; 34 int tmp=0; 35 F(j,1,k){ 36 tmp+=Q.top().first; 37 Q.push(P(max(Q.top().first-Q.top().second,0),Q.top().second)); 38 Q.pop(); 39 } 40 ans=max(ans,tmp); 41 } 42 } 43 printf("Case %d: %d\n",ic++,ans); 44 } 45 return 0; 46 }