码迷,mamicode.com
首页 > 其他好文 > 详细

BNU25359Escape Time II(状态压缩DP)

时间:2014-08-29 22:45:58      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:map   dp   

There is a fire in LTR ’ s home again. The fire can destroy all the things in t seconds, so LTR has to escape in t seconds. But there are some jewels in LTR ’ s rooms, LTR love jewels very much so he wants to take his jewels as many as possible before he goes to the exit. Assume that the ith room has ji jewels. At the beginning LTR is in room s, and the exit is in room e.

Your job is to find a way that LTR can go to the exit in time and take his jewels as many as possible.

Input

There are multiple test cases.
For each test case:
The 1st line contains 3 integers n (2 ≤ n ≤ 10), m, t (1 ≤ t ≤ 1000000) indicating the number of rooms, the number of edges between rooms and the escape time.
The 2nd line contains 2 integers s and e, indicating the starting room and the exit.
The 3rd line contains n integers, the ith interger ji (1 ≤ ji ≤ 1000000) indicating the number of jewels in the ith room.
The next m lines, every line contains 3 integers a, b, c, indicating that there is a way between room a and room b and it will take c (1 ≤ ct) seconds.

Output

For each test cases, you should print one line contains one integer the maximum number of jewels that LTR can take. If LTR can not reach the exit in time then output 0 instead.

Sample Input

3 3 5
0 2
10 10 10
0 1 1 
0 2 2
1 2 3
5 7 9
0 3
10 20 20 30 20
0 1 2
1 3 5
0 3 3
2 3 2
1 2 5
1 4 4
3 4 2

Sample Output

30
80

Source

Author

FU, Yujun

#include<stdio.h>
#include<string.h>
#define mulit(i) (1<<i)
#define inf 999999999
#define ll long long
ll map[15][15],v[15],n,s,e,maxtim;
ll dp[mulit(11)+5][15];//dp[sta][i]表示状态sta以点i为结尾点
void dfs(int sta,int i)
{
    for(int j=0;j<n;j++)
    if(map[i][j]!=-1&&dp[sta|mulit(j)][j]>dp[sta][i]+map[i][j]&&dp[sta][i]+map[i][j]<=maxtim)
    {
        dp[sta|mulit(j)][j]=dp[sta][i]+map[i][j];
        dfs(sta|mulit(j),j);
    }
}
ll findpath()
{
   ll maxv=0,tv;
   for(int i=0;i<mulit(n);i++)
   for(int j=0;j<n;j++)
   dp[i][j]=inf;

   dp[mulit(s)][s]=0;
   dfs(mulit(s),s);
   for(int sta=1;sta<mulit(n);sta++)
   {
       tv=0;
       for(int i=0;mulit(i)<=sta;i++)
        if(mulit(i)&sta)
        tv+=v[i];
        if(dp[sta][e]<=maxtim&&tv>maxv)maxv=tv;
   }
    return maxv;
}
int main()
{
    ll m,a,b,c;
    while(scanf("%lld%lld%lld",&n,&m,&maxtim)>0)
    {
        scanf("%lld%lld",&s,&e);
        for(int i=0;i<n;i++)scanf("%lld",&v[i]);
        memset(map,-1,sizeof(map));
        while(m--)
        {
            scanf("%lld%lld%lld",&a,&b,&c);
            if(map[a][b]>c||map[a][b]<0)
            map[a][b]=map[b][a]=c;
        }
        printf("%lld\n",findpath());
    }
}


 

BNU25359Escape Time II(状态压缩DP)

标签:map   dp   

原文地址:http://blog.csdn.net/u010372095/article/details/38930921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!