Travel

  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
题意:1能否经过h个点并回到1处?在经过边时花费边权,第一次到达h中任意点时先花费后点权,再收获前点权,若在次过程中无法保证非负,输出NO。
先预处理出h个点之间两两的最短路,然后状压dp求出在满足限制下的最大收益,-1输出NO。
#include<bits/stdc++.h>
#define MAX 105
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX][MAX],b[][];
int dp[<<][];
int mp[]; int main()
{
int t,n,m,mon,h,i,j,k;
int x,y,z;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&mon);
memset(a,INF,sizeof(a));
for(i=;i<=n;i++){
a[i][i]=;
}
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
if(a[x][y]==INF){
a[x][y]=z;
a[y][x]=z;
}
else if(z<a[x][y]){
a[x][y]=z;
a[y][x]=z;
}
}
for(k=;k<=n;k++){
for(i=;i<=n;i++){
for(j=;j<=n;j++){
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
}
}
}
scanf("%d",&h);
memset(mp,,sizeof(mp));
for(i=;i<=h;i++){
scanf("%d%d%d",&x,&y,&z);
mp[i]=x;
b[i][]=y;
b[i][]=z;
}
memset(dp,-,sizeof(dp));
dp[][]=mon;
for(i=;i<=h;i++){
if(mon-a[][mp[i]]-b[i][]<) continue;
dp[<<(i-)][i]=mon-a[][mp[i]]-b[i][]+b[i][];
}
for(i=;i<(<<h);i++){
for(j=;j<=h;j++){
if(!(i&(<<(j-)))) continue;
for(k=;k<=h;k++){
if(j==k||!(i&(<<(k-)))) continue;
if(dp[i^(<<(j-))][k]<||a[mp[k]][mp[j]]==INF) continue;
if(dp[i^(<<(j-))][k]-a[mp[k]][mp[j]]-b[j][]<) continue;
dp[i][j]=max(dp[i][j],dp[i^(<<(j-))][k]-a[mp[k]][mp[j]]-b[j][]+b[j][]);
}
}
}
int maxx=-;
for(i=;i<=h;i++){
maxx=max(maxx,dp[(<<h)-][i]-a[][mp[i]]);
}
if(maxx<) printf("NO\n");
else printf("YES\n");
}
return ;
}

 

HDU - 4284 Travel(floyd+状压dp)的更多相关文章

  1. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  2. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp

    题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...

  4. [hdu5418 Victor and World]floyd + 状压DP 或 SPFA

    题意:给n个点,m条边,每次只能沿边走,花费为边权值,求从1出发经过所有其它点≥1次最后回到1的最小花费. 思路: 状压DP.先用Floyd得到任意两点间的最短距离,转移时沿两个点的最短路转移.此时的 ...

  5. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

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

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

  7. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  8. poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp

    题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...

  9. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

随机推荐

  1. php函数: set_include_path

    <?php $p =get_include_path(); $p.=PATH_SEPARATOR.'./bp/'; $p.=PATH_SEPARATOR.'./CLI/'; $p.=PATH_S ...

  2. serve-index用法、实现原理(源码解读)

    本文主要讲解serve-index的用法和实现原理(源代码分析). 一 说明 serve-index的功能是将文件夹中文件列表显示到浏览器中. serve-index是一个NodeJS模块,可以通过N ...

  3. centos安装php5.6

    配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...

  4. java中的clone方法

    Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象.所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象.那 ...

  5. selenium WebDriverException: Message: unknown error: DevToolsActivePort file doesnt exist

    在centos中使用无头chrome报以下错误 selenium.common.exceptions.WebDriverException: Message: unknown error: DevTo ...

  6. StreamWriter结合UTF-8编码使用不当,会造成BOM(Byte Order Mark )问题生成乱码(转载)

    问: I was using HttpWebRequest to try a rest api in ASP.NET Core MVC.Here is my HttpWebRequest client ...

  7. luoguP3769 [CH弱省胡策R2]TATT

    luoguP3769 [CH弱省胡策R2]TATT PS:做这题前先切掉 P4148简单题,对于本人这样的juruo更助于理解,当然dalao就当练练手吧 题目大意: 现在有n个四维空间中的点,请求出 ...

  8. iPad actionsjeet

    在iphone和ipad上使用UIActionShee控件t的效果会不一样,在苹果的官方文档中有相关说明: 在ipad上使用UIActionSheet控件改控件不再从底部弹出,而是从屏幕中间弹出与UI ...

  9. html5--2.3新的布局元素(2)-article

    html5--2.3新的布局元素(2)-article 学习要点 了解article元素的语义和用法 完成一个简单的实例 article元素(标签) 用于定义一个独立的内容区块,比如一篇文章,一篇博客 ...

  10. ffmpeg截取视频

    ffmpeg -i ./suen071520.mp4 -vcodec copy -acodec copy -ss 00:55:00 -to 01:14:50 ./suen071520sp3.mp4-- ...