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. 时间写入文件名 nohup 原理 Command In Background your shell may have its own version of nohup

    echo 123 > `date +%Y-%m-%d-%H.tmp` echo 123 > /home/`date +%Y-%m-%d-%H.tmp` nohup --help [root ...

  2. CNN延拓至 复数域

  3. 如果数据需要被多个应用程序消费的话,推荐使用 Kafka,如果数据只是面向 Hadoop 的,可以使用 Flume

    https://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html Kafka 与 Flume 很多功能确实是重复的.以下是 ...

  4. 3行代码 多元线性方程组 rank=4 多元-一元 降元

    对于线性方程组Ax=b 对A和b执行同样的一串行初等运算, 那么该方程组的解集不发生变化. [未知-已知   高阶--低阶] http://mathworld.wolfram.com/CramersR ...

  5. Javascript模块化编程-规范[2]

    实现Javascript模块化,固然很重要,但是怎样才能实现国际上都能认可的模块化呢?模块化编程规范随应运而生. 目前Javascript模块化规范主要有两种:CommonJS和AMD. Common ...

  6. 关于SAP的编码范围

    [转自 http://blog.sina.com.cn/s/blog_6466e5f70100ithw.html ] 1.Number Range的通用Tcode:SNRO 2.Number Rang ...

  7. Java for LeetCode 088 Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: ...

  8. 最新App Store审核指南与10大被拒理由

    最近,苹果在官网给出了截至2015年2月份应用被拒绝的十大理由,其中50%以上的应用被拒绝都是因为这10个原因,其中7个理由和2014年相同,其中排名前三的原因分别是:需要补充更多信息.存在明显的bu ...

  9. IDEAL葵花宝典:java代码开发规范插件 Rainbow Brackets 插件

    前言: 最近在Jetbrains IDEA插件网站逛发现了 Rainbow Brackets这款插件,非常棒,推荐给大家. 可以实现配对括号相同颜色,并且实现选中区域代码高亮的功能. 对增强写代码的有 ...

  10. 使用TortoiseGit同步代码到github远程仓库

    1.clone github上的代码仓库的URL 可以用HTTPS,SSH, or Subversion 2.同步push 到远程仓库时 要用 SSH地址,同生成SSH private key ,在g ...