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

HDU - 4284 Travel(floyd+状压dp)

时间:2018-09-08 22:36:31      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:its   pac   follow   must   printf   ret   during   Plan   integer   

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;
}

 

HDU - 4284 Travel(floyd+状压dp)

标签:its   pac   follow   must   printf   ret   during   Plan   integer   

原文地址:https://www.cnblogs.com/yzm10/p/9610323.html

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