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. SQL中的四种连接方式

    转自:http://www.cnblogs.com/afirefly/archive/2010/10/08/1845906.html 联接条件可在FROM或WHERE子句中指定,建议在FROM子句中指 ...

  2. google guice

    1 google guice是什么 google guice是一个轻量的DI容器. 2 guice和spring对比 spring的配置放在xm文件中,guice的配置放在Module中. guice ...

  3. 20179209课后作业之od命令重写

    一.问题描述: 1 复习c文件处理内容 2 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 3. main与其他分开,制作静态库和动态库 4. 编写Makefi ...

  4. C语言实现 操作系统 银行家算法

    /**************************************************** 银行家算法 算法思想: 1. 在多个进程中,挑选资源需求最小的进程Pmin. 可能存在多类资 ...

  5. No provisioned iOS devices are available with a compatible iOS version. Connect an iOS device with a

    No provisioned iOS devices are available with a compatible iOS version. Connect an iOS device with a ...

  6. SAP-财务知识点

    [转自 http://blog.itpub.net/195776/viewspace-1023912/] SAP FI/CO Reading RepositorySAP财务成本知识库 目 录前言.一. ...

  7. hadoop磁盘空间不均衡的解决办法

    hadoop集群在运行一段时间后,总是会出现某台机器的磁盘使用率特别高,有的使用率特别低,针对这种情况,hadoop提供了balancer工具调整磁盘负载 使用命令:start-balancer.sh ...

  8. Database: coursera assignment 1

    q.1: Find the titles of all movies directed by Steven Spielberg. select title from moviewhere direct ...

  9. HDU2243 考研路茫茫——单词情结 ——AC自动机、矩阵优化

    题目链接:https://vjudge.net/problem/HDU-2243 考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  10. 【html学习整理】常用标签

    什么是html 超文本标记语言 html语法规则       所有的命令放到<> 大部分成对存在,以<tag>开始,</tag>结束 网页的基本框架,常用的标记 & ...