hdu4284 状态压缩dp

http://acm.hdu.edu.cn/showproblem.php?pid=4284

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
/**
hdu4284  状态压缩dp
题目大意:给定一个城市网络,要求从1出发在回到1,必须经过指定的一些点并在这些点中打工,打工可以挣一些路费,但是事先需要买许可证,问能否按照
          要求最终回到1点
解题思路:一开始以为是图论题,想偏了== 指定的点最多有15个,我们可以用dp[st][j] 表示在st状态下最终留在j点剩下的最多钱。floy预处理点与点之间
         的最短距离,然后状态转移即可。方程为:dp[s][i]=max(dp[s][i],dp[s'][j]-g[j][i]-d[i]+c[i]);
*/
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int g[155][155];
int dp[1<<16][16];
int num[22],earn[22],cost[22];
int n,m,mon;
int main()
{
     int T;
     scanf("%d",&T);
     while(T--)
     {
         scanf("%d%d%d",&n,&m,&mon);
         memset(g,inf,sizeof(g));
         for(int i=1;i<=n;i++)
            g[i][i]=0;
         int a,b,c;
         while(m--)
         {
             scanf("%d%d%d",&a,&b,&c);
             if(c<g[a][b])/// 有重边
                g[a][b]=g[b][a]=c;
         }
         ///预处理最短路
         for(int k=1;k<=n;k++)
         {
             for(int i=1;i<=n;i++)
             {
                 if(g[i][k]!=inf)
                 {
                     for(int j=1;j<=n;j++)
                     {
                         if(g[k][j]!=inf)
                         {
                             g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
                         }
                     }
                 }
             }
         }
         memset(dp,-1,sizeof(dp));
         int h,tmp;
         scanf("%d",&h);
         for(int i=0;i<h;i++)
         {
             scanf("%d%d%d",&num[i],&earn[i],&cost[i]);
         }
         for(int i=0;i<h;i++)
         {
             tmp=mon-g[1][num[i]]-cost[i];
             if(tmp>=0)///先初始化1到每个指定点
             {
                 dp[1<<i][i]=tmp+earn[i];
             }
         }
         int st=(1<<h)-1;
         for(int i=1;i<=st;i++)
         {
             for(int j=0;j<h;j++)
             {
                 if(dp[i][j]<0)continue;
                 for(int k=0;k<h;k++)
                 {
                     if(i&(1<<k))continue;
                     tmp=dp[i][j]-g[num[j]][num[k]]-cost[k];
                     if(tmp>=0)
                     {
                         tmp+=earn[k];
                         int stat=i|(1<<k);
                         dp[stat][k]=max(dp[stat][k],tmp);
                     }
                 }
             }
         }
         int flag=0;
         for(int i=0;i<h;i++)
         {
             tmp=dp[st][i]-g[num[i]][1];//最后还要回到1点
             if(tmp>=0)
             {
                 flag=1;
                 break;
             }
         }
         if(flag)
            printf("YES\n");
         else
            printf("NO\n");
     }
     return 0;
}


你可能感兴趣的:(hdu4284 状态压缩dp)