HDU 4284 Travel(状压dp)

Travel

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3830    Accepted Submission(s): 996


Problem Description
  PP loves travel. Her dream is to travel around country A which consists of N cities and M roads connecting them. PP has measured the money each road costs. But she still has one more problem: she doesn't have enough money. So she must work during her travel. She has chosen some cities that she must visit and stay to work. In City_i she can do some work to earn Ci money, but before that she has to pay Di money to get the work license. She can't work in that city if she doesn't get the license but she can go through the city without license. In each chosen city, PP can only earn money and get license once. In other cities, she will not earn or pay money so that you can consider Ci=Di=0. Please help her make a plan to visit all chosen cities and get license in all of them under all rules above.
  PP lives in city 1, and she will start her journey from city 1. and end her journey at city 1 too.
 

Input
  The first line of input consists of one integer T which means T cases will follow.
  Then follows T cases, each of which begins with three integers: the number of cities N (N <= 100) , number of roads M (M <= 5000) and her initiative money Money (Money <= 10^5) .
  Then follows M lines. Each contains three integers u, v, w, which means there is a road between city u and city v and the cost is w. u and v are between 1 and N (inclusive), w <= 10^5.
  Then follows a integer H (H <= 15) , which is the number of chosen cities.
  Then follows H lines. Each contains three integers Num, Ci, Di, which means the i_th chosen city number and Ci, Di described above.(Ci, Di <= 10^5)
 

Output
  If PP can visit all chosen cities and get all licenses, output "YES", otherwise output "NO".
 

Sample Input
      
      
2 4 5 10 1 2 1 2 3 2 1 3 2 1 4 1 3 4 2 3 1 8 5 2 5 2 3 10 1 2 1 100 1 2 10000 1 2 100000 1
 

Sample Output
      
      
YES NO
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   4267  4268  4269  4270  4271 
 

Statistic |  Submit |  Discuss | Note
Hangzhou Dianzi University Online Judge 3.0

这题有毒,一开始走得到1这个点的工作证可能不是最优的,坑我好久


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;

using namespace std;
#define INF 0x3f3f3f3f
#define N 105

int a[N][N];
int out[N],in[N];
int n,m,money;
int dp[1<<17][17];
vector<int>g;

void floyd()
{
    int i,j,k;
    for(k=0;k<=n;k++)
        for(i=0;i<=n;i++)
           for(j=0;j<=n;j++)
               a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
}

bool solve()
{
    floyd();
   int i,j;
   for(i=0;i<g.size();i++)  //不一定一开始就得到1这个点的工作证
   {
       if(money>=a[0][g[i]]+out[g[i]])
          dp[1<<i][i]=money-a[0][g[i]]-out[g[i]]+in[g[i]];
   }

   int k=g.size()-1;

   int len=1<<g.size();

   for(int cur=0;cur<len;cur++)
     for(int i=0;i<=k;i++)
    {
        if(!(cur&(1<<i))) continue;
        if(dp[cur][i]<0) continue;

        for(j=0;j<=k;j++)
        {
            if(cur&(1<<j)) continue;
            if(dp[cur][i]<out[g[j]]+a[g[i]][g[j]]) continue;
            int to=cur|(1<<j);
            dp[to][j]=max(dp[to][j],dp[cur][i]+in[g[j]]-out[g[j]]-a[g[i]][g[j]]);
        }
    }

    for(i=0;i<=k;i++)
        if(dp[len-1][i]>=a[g[i]][1]) return true;
    return false;
}

int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&money);
        memset(a,INF,sizeof(a));
        int u,v,len;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&len);
            if(a[u][v]>len) a[u][v]=a[v][u]=len;
        }
        for(i=0;i<=n;i++)
            a[i][i]=0;
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        g.clear();
        int h;
        scanf("%d",&h);
        int pos=-1;
        while(h--)
        {
            scanf("%d",&u);
            scanf("%d%d",&in[u],&out[u]);
            g.push_back(u);
            if(u==1) pos=1;
        }
        if(pos==-1)
        {
            g.push_back(1);
            pos=g.size()-1;
        }
        memset(dp,-1,sizeof(dp));
        sort(g.begin(),g.end());
        a[0][1]=a[1][0]=0;
        if(solve())
            printf("YES\n");
        else
            printf("NO\n");
    }
 return 0;
}

/*

4
2 1 100
1 2 10000
1
2 100000 1

*/




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值