HDU 4284 Travel 第37届ACM/ICPC 天津赛区网络赛1007题 (状态压缩DP)

Travel

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

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
2012 ACM/ICPC Asia Regional Tianjin Online  

 

Recommend liuyiding     比较简单,主要是要理解清楚题意。 从1点出发,要回到一点。需要经过H个点,1点可能包括在H个点中,也可能不包括。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f  //这个无穷大不能太大,小心后面相加时溢出
const int MAXN=150;

int dis[MAXN][MAXN];
int dp[20][1<<17];



struct Node
{
    int num;
    int C,D;
}node[20];


void floyed(int n)//节点从1~n编号
{
    int i,j,k;
    for(k=1;k<=n;k++)
       for(i=1;i<=n;i++)
         for(j=1;j<=n;j++)
         {
             if(dis[i][j]>dis[i][k]+dis[k][j])
                 dis[i][j]=dis[i][k]+dis[k][j];
         }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    int t;
    int T;
    int u,v,w;
    int H;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&t);
        for(int i=1;i<=n;i++)
          for(int j=i;j<=n;j++)
          {
              if(i==j)dis[i][j]=0;
              else dis[i][j]=dis[j][i]=INF;
          }
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(w<dis[u][v])
              dis[u][v]=dis[v][u]=w;
        }
        floyed(n);
        scanf("%d",&H);
        int start=-1;
        for(int i=0;i<H;i++)
        {
            scanf("%d%d%d",&node[i].num,&node[i].C,&node[i].D);
            if(node[i].num==1)start=i;
        }
        if(start==-1)
        {
            node[H].num=1;
            node[H].C=node[H].D=0;
            start=H;
            H++;
        }
        for(int i=0;i<H;i++)
          for(int j=0;j<(1<<H);j++)dp[i][j]=-INF;
        //可以先不拿1点的licenses,之后返回来拿也是可以的
        dp[start][0]=t;
        if(t>=node[start].D)dp[start][1<<start]=t-node[start].D+node[start].C;


        for(int j=0;j<(1<<H);j++)
          for(int i=0;i<H;i++)//这个顺序一定要注意不能换
          {
              if(dp[i][j]<0)continue;
              for(int k=0;k<H;k++)
                if(k!=i)
                {
                    int t=(1<<k);
                    if((t&j)==0)
                    {
                        int temp;
                        if(dp[i][j]<dis[node[i].num][node[k].num])temp=-INF;
                        else
                        {
                            temp=dp[i][j]-dis[node[i].num][node[k].num];
                            if(temp<node[k].D)temp=-INF;
                            else temp+=node[k].C-node[k].D;
                        }
                        dp[k][t|j]=max(dp[k][t|j],temp);
                    }
                }
          }

        bool flag=false;
        int pp=(1<<H)-1;
        for(int i=0;i<H;i++)
        {
            if(dp[i][pp]<0)continue;
            if(dp[i][pp]>=dis[node[i].num][1])
            {
                flag=true;
                break;
            }
        }

        if(!flag)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/09/11/2680748.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞