hdu 4284 Travel【floyd+DFS+全文翻译】

本文介绍了一个旅行规划问题的解决方法,通过使用Floyd算法预处理城市间的最短路径,并采用深度优先搜索策略遍历所有必访城市,确保在有限的资金条件下完成旅行目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Travel

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


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

个人全文翻译:PP喜欢旅游,他的梦想是在A国家(由M个城市组成)旅游。PP知道走每一条路需要花费的金钱,但是她仍然有一个问题,她没有足够的钱,所以她需要边打工边旅游,她选择一些城市是必须去的并且必须在那里工作。在城市I 她可以获得CI薪水,但是她在工作前需要花费Di的钱来获得通行证(许可证),她如果没有许可证就不能再那里工作,但是没有许可证缺可以穿过这个城市,对于每个挑选的城市,PP仅仅可以买一次通行证并且只能工作一次,在其他没有挑选的城市里边,不能卖许可证工作,所以CI=DI=0,请帮助她制作一个计划去访问所有她所选择的城市并且获得许可证和工作。PP在城市1居住,所以她开始履行于城市1,并且结束也要在城市1。

思路:用floyd预处理,求出城市i到城市j所需要的最少花费,因为H比较小,所以我们枚举所有的H进行暴力搜索是不会超时的。

注意点:

1、对于暴力搜索的过程千万别枚举N,要枚举H,因为我们要到的地方只有H个,不需要枚举辣么多。

2、一个贪心思维的点:不需要判断是否在这个城市工作,只要到的城市能够工作,那么我们就工作,因为工作是一定会赚钱的,不用考虑是否在这个城市这个时间点工作与否、

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct work
{
    int num,ci,di;
}ww[25];
int ok;
int vis[115];
int map[115][115];
int n,m,money;
int q;
void init()
{
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            if(i==j)
                map[i][j]=0;
            else
                map[i][j]=10000000;
        }
    }
    memset(vis,0,sizeof(vis));
    ok=0;
}
void floyd()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=n;k++)
            {
                map[j][k]=min(map[j][k],map[j][i]+map[i][k]);
            }
        }
    }
}
void dfs(int x,int cont,int yu)
{
    //printf("%d %d %d\n",x,cont,yu);
    if(ok==1)return ;
    if(cont==q)
    {
        if(yu-map[x][1]>=0)
        {
            ok=1;
            return ;
        }
    }
    for(int i=0;i<q;i++)
    {
        if(vis[ww[i].num]==0&&map[x][ww[i].num]!=10000000)
        {
            if(yu-map[x][ww[i].num]>=ww[i].di)
            {
                vis[ww[i].num]=1;
                dfs(ww[i].num,cont+1,yu-map[x][ww[i].num]-ww[i].di+ww[i].ci);
                if(ok==1)return ;
                vis[ww[i].num]=0;
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&money);
        init();
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            if(x!=y)
            {
                map[x][y]=min(map[x][y],w);
                map[y][x]=min(map[y][x],w);
            }
        }
        floyd();
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            scanf("%d%d%d",&ww[i].num,&ww[i].ci,&ww[i].di);
        }
        ok=0;
        dfs(1,0,money);
        if(ok==1)printf("YES\n");
        else printf("NO\n");
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值