poj 3280 Cheapest Palindrome

http://poj.org/problem?id=3280

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <algorithm>

 4 #define maxn 2000010

 5 using namespace std;

 6 

 7 char str[maxn],ch;

 8 int a[maxn];

 9 int n,m;

10 int dp[2020][2020];

11 

12 int main()

13 {

14     while(scanf("%d%d",&n,&m)!=EOF)

15     {

16         getchar();

17         scanf("%s",str);

18         getchar();

19         int x,y;

20         for(int i=0; i<n; i++)

21         {

22             scanf("%c %d%d",&ch,&x,&y);

23             a[ch-'a']=min(x,y);

24             getchar();

25         }

26         for(int i=m-1; i>=0; i--)

27         {

28             for(int j=i+1; j<m; j++)

29             {

30                 if(str[i]==str[j]) dp[i][j]=dp[i+1][j-1];

31                 else dp[i][j]=min(dp[i+1][j]+a[str[i]-'a'],dp[i][j-1]+a[str[j]-'a']);

32             }

33         }

34         printf("%d\n",dp[0][m-1]);

35     }

36     return 0;

37 }
View Code

 

你可能感兴趣的:(heap)