Travel

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

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   |   We have carefully selected several similar problems for you:  
4268 
4269 
4270 
4271 
4272 
 

题意:

一个人要去旅游。给你n个城市和m条道路。其中有h个城市必须在那里打工。打工的话必须办证。办证要花掉d[i]的钱币。但是有c[i]的工资。每条道路都需花费一定的钱币。告诉你他的初始钱币数。问他能否将这h个城市都工作完。

思路:

y由于h的范围比较小。可以状态压缩。考虑经过的情况直接可以用floyd求出两个城市间的最小花费。然后就是典型的TSP了。

详细见代码:

#include<iostream>
#include<string.h>
#include<cstdio>
const int INF=0x3f3f3f3f;
using namespace std;
int dp[105][1<<16];
int dis[105][105];
int C[150],D[150],id[150];
int n,m,h,mon;
void floyd()
{
for (int k=1;k<=n;k++)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
int main()
{
int t,i,j,u,v,w,lim,ns,s,ans;
scanf("%d",&t);
while (t--)
{
memset(dis,0x3f,sizeof dis);
scanf("%d%d%d",&n,&m,&mon);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
dis[u][v]=dis[v][u]=min(dis[u][v],w);//习惯性的就判重了。据说这题还真有重边
}
for(i=1;i<=n;i++)
dis[i][i]=0;
floyd();
scanf("%d",&h);
for(i=1;i<=h;i++)
scanf("%d%d%d",&id[i],&C[i],&D[i]);//异常2B。开始后面把id[i]和i混为一谈了
memset(dp,0xcf,sizeof dp);
dp[1][0]=mon;
for(i=1;i<=h;i++)
{
v=id[i];
if(dp[1][0]>=D[i]+dis[1][v])
{
ns=1<<(i-1);
dp[v][ns]=dp[1][0]-D[i]-dis[1][v]+C[i];//只有距离才用还原到原标号
}
}
lim=1<<h;
for(s=0;s<lim;s++)
{
for(i=1;i<=h;i++)//开始2B了。套了两个n果断超时了。
{
u=id[i];//其实直接考虑必须工作的城市就行了.因为最后还是会到必须到的城市
if(dp[u][s]<0||!(s&(1<<(i-1))))//保证状态有效
continue;
for(j=1;j<=h;j++)
{
v=id[j];
if(!(s&(1<<(j-1))))
{
ns=s|(1<<(j-1));
if(dp[u][s]>=D[j]+dis[u][v])
dp[v][ns]=max(dp[v][ns],dp[u][s]-D[j]-dis[u][v]+C[j]);
}
}
}
}
ans=-INF;
for(i=1;i<=h;i++)
{
u=id[i];
ans=max(ans,dp[u][lim-1]-dis[u][1]);
}
if(ans>=0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

hdu 4284 Travel(壮压DP&TSP&floyd)的更多相关文章

  1. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  2. [Usaco2006 Nov]Corn Fields牧场的安排 壮压DP

    看到第一眼就发觉是壮压DP 然后就三进制枚举子集吧. 这题真是壮压入门好题... 对于dp[i][j] 表示第i行,j状态下前i行的分配方案数. 那么dp[i][j]肯定是从i-1行转过来的 那么由于 ...

  3. POJ 2686 Traveling by Stagecoach 壮压DP

    大意是有一个人从某个城市要到另一个城市(点数<=30) 然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票. 花费的时间呢,是马车票上有个速率值,用边/速率就是花的时间. 问最后这个人花费 ...

  4. HDU - 4284 Travel(floyd+状压dp)

    Travel PP loves travel. Her dream is to travel around country A which consists of N cities and M roa ...

  5. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

  6. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  8. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  9. hdu 4284 Travel(floyd + TSP)

    虽然题中有n<=100个点,但实际上你必须走过的点只有H<=15个.而且经过任意点但不消耗C[i]跟D[i]可以为无限次,所以可以floyd预处理出H个点的最短路,之后剩下的...就成了裸 ...

随机推荐

  1. ThinkPHP自动令牌验证(附实例)

    一.数据表结构 user表结构如下: id username password 二.view模板部分 /view/index.html页面如下:   1 2 3 4 5 6 <form acti ...

  2. 2018-2019-2 《网络对抗技术》Exp7 网络欺诈防范 Week10 20165233

    Exp7 网络欺诈防范 目录 一.基础问题 二.实验步骤 实验点一:简单应用SET工具建立冒名网站 实验点二:ettercap DNS spoof 实验点三:结合应用两种技术,用DNS spoof引导 ...

  3. 第四篇 Flask 中的模板语言 Jinja2 及 render_template 的深度用法

    是时候开始写个前端了,Flask中默认的模板语言是Jinja2 现在我们来一步一步的学习一下 Jinja2 捎带手把 render_template 中留下的疑问解决一下 首先我们要在后端定义几个字符 ...

  4. 搞点事情,使用node搭建反向代理

    导语 最近有个需求,需要对业务管理后台的操作记录进行上报.一般这种上报需求都是又后台同学来做比较合适的.但是因为后台人力的原因.这个工作落到了我这个小前端的头上.这里记录下做这个需求踩的一些坑. 一. ...

  5. Rhythmk 一步一步学 JAVA(9) JAVA 基础笔记[枚举,...]

    1.装箱就是值类型转换为object类型,拆箱相反:object转化为值类型 eg:Integer i=1; // 装箱 int j=i; // 拆箱 2.静态导入: eg: 导入: import s ...

  6. 使用Tor创建.onion域名网站(创建暗网服务和暗网的网站)

    使用Tor 的.onion域名创建匿名服务器 Tor不仅可以提供客户端的匿名访问,Tor还可以提供服务器的匿名.通过使用Tor网络,用户可以维护位置不可知的服务器.当然如果要访问这个隐蔽的服务,客户端 ...

  7. SmartGit过期后破解方法

    根据自己的操作系统,进入相应的文件夹 ,可能还有一个版本号的文件夹,再进入 Windows: %APPDATA%\syntevo\SmartGit\OS X: ~/Library/Preference ...

  8. git的突出解决--git rebase之abort、continue、skip

    (1)应用实例描述 假设在github或者gitoschina上建立了一个项目,默认分支为master分支,远程master分支上c.sh文件内容: 开发者A.B分别将项目拷贝到自己本地进行开发 某一 ...

  9. 第五章 大数据平台与技术 第12讲 大数据处理平台Spark

    Spark支持多种的编程语言 对比scala和Java编程上节课的计数程序.相比之下,scala简洁明了. Hadoop的IO开销大导致了延迟高,也就是说任务和任务之间涉及到I/O操作.前一个任务完成 ...

  10. DBArtist之Oracle入门第1步: 如何安装Oracle 11g

    操作系统:  Windows 7 数据库   :  Oracle 11gR2 第一步: 下载Oracle安装包 Oracle官网:  https://www.oracle.com/index.html ...