HDU 4284 Travel(当状压DP遇到最短路。。。)

Travel
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

题目:给出一些城市,从1出发,旅游一圈回到1,由于花费可能不够,所以选择一些城市打工,打工之前需要花费d买一个证,工资为c。选中的城市必须去工作一次,而且只能工作一次,问能不能完成旅行

因为城市数量太多而且 不是选中的城市在中间过程考虑 情况会很复杂而且只会有花费的增加并不会改变有花费的改变。。。所以用最短路floyd把结果记录下来就好不需要再考虑中间过程,在状压DP时直接在选中的城市之间转移就好,还有只有有钱买证才能得到钱

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 16;
const int inf = 0x3f3f3f3f;
typedef long long LL;
int  dp[1<<N][20], s[110][110], work[20], v[20], w[20];


int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m, money;
        scanf("%d %d %d", &n, &m, &money);
        memset(dp,-1,sizeof(dp));
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                s[i][j]=inf;
            }
            s[i][i]=0;
        }
        for(int i=0; i<m; i++)
        {
            int x, y, c;
            scanf("%d %d %d", &x, &y, &c);
            x--,y--;
            if(s[x][y]>c)
                s[x][y]=s[y][x]=c;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    if(j!=i&&k!=i&&k!=j&&s[j][k]>s[j][i]+s[i][k])
                        s[j][k]=s[j][i]+s[i][k];
                }
            }
        }
        int num;
        scanf("%d",&num);
        int pos=-1;
        for(int i=0; i<num; i++)
        {
            scanf("%d %d %d",&work[i], &v[i], &w[i]);
            work[i]--;
            if(work[i]==0)
               pos=i;
        }
        if(pos==-1)
        {
            work[num]=0,v[num]=0,w[num]=0;
            pos=num++;
        }
        if(money>=w[pos])
            dp[1<<pos][pos]=money-w[pos]+v[pos];
        dp[0][pos]=money;
        for(int i=0;i<(1<<num);i++)
        {
            for(int j=0;j<num;j++)
            {
                if(dp[i][j]==-1)
                    continue;
                for(int k=0;k<num;k++)
                {
                    if(k==j||(i&(1<<k))||s[j][k]==inf)
                        continue;
                    int state=i|(1<<k);
                    if(dp[i][j]>=w[k]+s[work[j]][work[k]])
                        dp[state][k]=max(dp[state][k],dp[i][j]-s[work[j]][work[k]]-w[k]+v[k]);
                }
            }
        }
        int ans=-1;
        for(int i=0;i<num;i++)
        {
            if(dp[(1<<num)-1][i]>=s[work[i]][0])
                ans=1;
        }
        if(ans==-1)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}




参考:



#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#define inf 1<<28
#define N 105
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,money,h;
int path[N][N];
int dp[20][1<<16];
int work[20],c[20],d[20];
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&money);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
path[i][j]=inf;
path[i][i]=0;
}
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
u--;v--;
path[u][v]=Min(path[u][v],w);
path[v][u]=path[u][v];
}
//Floyd预处理
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=k&&i!=j&&j!=k)
path[i][j]=Min(path[i][k]+path[k][j],path[i][j]);
scanf("%d",&h);
int pos=-1;
for(int i=0;i<h;i++){
scanf("%d%d%d",&work[i],&c[i],&d[i]);
work[i]--;
if(work[i]==0) pos=i;   //说明必需点中包含了起点1
}
//如果不包含,我们加入冗余点,便于后面处理,c和d都为0
if(pos==-1){
work[h]=0;c[h]=0;d[h]=0;
pos=h++;
}
memset(dp,-1,sizeof(dp));
if(money-d[pos]>=0) dp[pos][1<<pos]=money-d[pos]+c[pos];dp[pos][0]=money;
for(int i=0;i<(1<<h);i++){
for(int j=0;j<h;j++){
if(dp[j][i]==-1) continue;
for(int k=0;k<h;k++){
if(k==j||((1<<k)&i)) continue;
//钱够在两个城市之间移动,而且够买证
if(dp[j][i]>=path[work[j]][work[k]]+d[k])
dp[k][i|(1<<k)]=Max(dp[k][i|(1<<k)],dp[j][i]-path[work[j]][work[k]]-d[k]+c[k]);
}
}
}
bool ans=false;
for(int i=0;i<h;i++)
//最后判断能不能返回起点
if(dp[i][(1<<h)-1]>=path[work[i]][0]){
ans=true;
break;
}
puts(ans?"YES":"NO");
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值