现在的位置: 首页 > 综合 > 正文

hdu4284 Travel

2018年04月23日 ⁄ 综合 ⁄ 共 2843字 ⁄ 字号 评论关闭

Travel

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

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
 

Recommend
liuyiding

H只有15,显然状态dp,特判下1就行了。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define INF 1000000000

typedef struct
{
    int num,c,d;
}DP;

int map[105][105];
DP a[25];
int dp[(1<<17)+1][25];

int main()
{
    int n,m,mon,T,i,x,j,k,p,s,y,z,h,ok;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&mon);
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            {
                map[i][j]=INF;
            }
            map[i][i]=0;
        }
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            x--;y--;
            map[x][y]=min(map[x][y],z);
            map[y][x]=map[x][y];
        }
        for (k=0;k<n;k++)
        {
            for (i=0;i<n;i++)
            {
                for (j=0;j<n;j++)
                {
                    if (i==j || j==k || i==k) continue;
                    map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
                }
            }
        }
        scanf("%d",&h);
        ok=-1;
        for (i=0;i<h;i++)
        {
            scanf("%d%d%d",&a[i].num,&a[i].c,&a[i].d);
            a[i].num--;
            if (a[i].num==0) ok=i;
        }
        if (ok==-1)
        {
            a[h].num=0;
            a[h].c=0;
            a[h].d=0;
            ok=h;
            h++;
        }
        memset(dp,-1,sizeof(dp));
        if (mon-a[ok].d>=0) dp[(1<<ok)][ok]=a[ok].c-a[ok].d+mon;
        dp[0][ok]=mon;
        for (i=0;i<(1<<h);i++)
        {
            for (j=0;j<h;j++)
            {
                if (dp[i][j]==-1) continue;
                for (k=0;k<h;k++)
                {
                    if (k==j || (i & (1<<k))!=0) continue;
                    p=i | (1<<k);
                    if (dp[i][j]>=a[k].d+map[a[j].num][a[k].num])
                    {
                        dp[p][k]=max(dp[p][k],dp[i][j]+a[k].c-a[k].d-map[a[j].num][a[k].num]);
                    }
                }
            }
        }
        for (i=0;i<h;i++)
        {
            if (dp[(1<<h)-1][i]-map[a[i].num][0]>=0) break;
        }
        if (i<h) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.