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. python中装饰器你真的理解吗?

    def w1(func): print('装饰器1....') def w1_in(): print('w1_in.....') func() return w1_in def w2(func): p ...

  2. MongoDB查询语句(转)

    目录 查询操作 集合查询方法 find() 查询内嵌文档 查询操作符(内含 数组查询) "$gt" ."$gte". "$lt". &quo ...

  3. Redis实现主从复制(转)

    一.Redis的Replication: 这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了.相信在阅读完这篇Blog之后你也可以轻松做到.这里我们还是先列出一些理论性的 ...

  4. 关于String,StringBuffer与StringBuilder的区别

    String是字符串常量对象,对其进行改变时会相当影响效率,特别注意在循环中直接拼接字符串效率非常差. 如果你想改变字符串的值,更加推荐使用StringBuffer与StringBuilder两种可变 ...

  5. Hadoop实战-Flume之Source regex_filter(十三)

    a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = ...

  6. 微软Azure区块链开发工具包三大功能详解

    2018年11月15日,微软宣布了Azure区块链开发工具包,它基于微软的无服务器技术构建,并且利用微软和第三方SaaS,完美集成了区块链.该工具包扩展了微软的区块链开发模板和Azure Blockc ...

  7. Swift 烧脑体操(二) - 函数的参数

    前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融入了大量新特性.这也使得我们学习掌握这门语言变得相对来说更加困 ...

  8. vs2012环境配置

    快捷键 css格式设置 字体设置 新建项目 项目创建失败? 更改默认开发语言环境 1.快捷键 代码格式化:Ctrl+K+D 2.css格式设置: 工具→选项→文本编辑器→CSS→格式设置→选择“紧凑模 ...

  9. java图形界面设计

    1.       基本的java Frame操作. Java的图形界面的类主要包括AWT和Swing.在AWT中图形元素的父类为Component. 继承关系如下:Component->Cont ...

  10. bzoj5093: [Lydsy1711月赛]图的价值

    不难想到考虑每个点的贡献,ans=n*sigema(1~n)i C(n-1,i)*(2^C(n-1,2))*i^k 直接套第二类斯特林拆柿子即可.提示:sigema(1~n)i C(n,i)*C(i, ...