http://blog.youkuaiyun.com/libin56842/article/details/24316493
再做还是会卡一些地方。。。
要习惯用结构体来存储。。。
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<iomanip>
#include<cstdio>
#include<stack>
#include<iostream>
#include<map>
#include<queue>
#include<cmath>
#include<strstream>
using namespace std;
#define sf scanf
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a));
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define N 1000010
#define M 200020
#define mod 998244353
#define ULL unsigned long long
#define LL long long
#define inf 0x3f3f3f3f
#define ls (i<<1)
#define rs (ls|1)
#define md ((ll+rr)>>1)
#define mxn 1<<16
//2017年08月08日08:05:11 15min
struct Node{
string name;
int end,ned;
}a[50];
struct P{
int pre,now;
int time,score;
}dp[mxn];
int main(){
freopen("in.txt","r",stdin);
int T;sf("%d",&T);
while(T--){
mem(dp,0);
int n;sf("%d",&n);
rep(i,0,n-1){ cin>>a[i].name;sf("%d%d",&a[i].end,&a[i].ned); }
int up=1<<n;
for(int s=1;s<up;++s){
dp[s].score=inf;
for(int i=n-1;i>=0;--i){
int tem=1<<i;
if(tem&s){
int past=s-tem;
int st=dp[past].time+a[i].ned-a[i].end;
if(st<0)st=0;
if(st+dp[past].score<dp[s].score){
dp[s].score=dp[past].score+st;
dp[s].pre=past;
dp[s].now=i;
dp[s].time=dp[past].time+a[i].ned;
}
}
}
}
pf("%d\n",dp[up-1].score);
vector<int>vec;
int tmp=up-1;
while(tmp){
vec.push_back(dp[tmp].now);
tmp=dp[tmp].pre;
}
for(int i=vec.size()-1;i>=0;--i){
cout<<a[vec[i]].name<<'\n';
}
}
}
本文通过一个具体的竞赛题目,详细介绍了如何使用结构体存储数据,并采用动态规划算法求解最优方案。文章首先定义了两个结构体类型用于表示任务及其完成情况,随后利用动态规划的方法在所有可能的任务组合中寻找得分最低的解决方案。
3万+

被折叠的 条评论
为什么被折叠?



