HDU - 4284 Travel(floyd+状压dp)

Travel

 

  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




题意:1能否经过h个点并回到1处?在经过边时花费边权,第一次到达h中任意点时先花费后点权,再收获前点权,若在次过程中无法保证非负,输出NO。
先预处理出h个点之间两两的最短路,然后状压dp求出在满足限制下的最大收益,-1输出NO。

#include<bits/stdc++.h>
#define MAX 105
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

int a[MAX][MAX],b[16][2];
int dp[1<<15][16];
int mp[16];

int main()
{
    int t,n,m,mon,h,i,j,k;
    int x,y,z;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&n,&m,&mon);
        memset(a,INF,sizeof(a));
        for(i=1;i<=n;i++){
            a[i][i]=0;
        }
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&z);
            if(a[x][y]==INF){
                a[x][y]=z;
                a[y][x]=z;
            }
            else if(z<a[x][y]){
                a[x][y]=z;
                a[y][x]=z;
            }
        }
        for(k=1;k<=n;k++){
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                    a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
                }
            }
        }
        scanf("%d",&h);
        memset(mp,0,sizeof(mp));
        for(i=1;i<=h;i++){
            scanf("%d%d%d",&x,&y,&z);
            mp[i]=x;
            b[i][1]=y;
            b[i][0]=z;
        }
        memset(dp,-1,sizeof(dp));
        dp[0][1]=mon;
        for(i=1;i<=h;i++){
            if(mon-a[1][mp[i]]-b[i][0]<0) continue;
            dp[1<<(i-1)][i]=mon-a[1][mp[i]]-b[i][0]+b[i][1];
        }
        for(i=0;i<(1<<h);i++){
            for(j=1;j<=h;j++){
                if(!(i&(1<<(j-1)))) continue;
                for(k=1;k<=h;k++){
                    if(j==k||!(i&(1<<(k-1)))) continue;
                    if(dp[i^(1<<(j-1))][k]<0||a[mp[k]][mp[j]]==INF) continue;
                    if(dp[i^(1<<(j-1))][k]-a[mp[k]][mp[j]]-b[j][0]<0) continue;
                    dp[i][j]=max(dp[i][j],dp[i^(1<<(j-1))][k]-a[mp[k]][mp[j]]-b[j][0]+b[j][1]);
                }
            }
        }
        int maxx=-1;
        for(i=1;i<=h;i++){
            maxx=max(maxx,dp[(1<<h)-1][i]-a[1][mp[i]]);
        }
        if(maxx<0) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}
 
 
 

转载于:https://www.cnblogs.com/yzm10/p/9610323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值