首页 > 其他分享 >hdu 4284 Travel(压缩DP,4级)

hdu 4284 Travel(压缩DP,4级)

时间:2023-02-24 11:31:56浏览次数:57  
标签:city hdu int money Travel DP id dp she


Travel


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


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


​2012 ACM/ICPC Asia Regional Tianjin Online​


 



Recommend


liuyiding   |   We have carefully selected several similar problems for you:   ​​4268​​​  ​​​4269​​​  ​​​4270​​​  ​​​4271​​​  ​​​4272​​ 



预处理两点距离,然后压缩DP。

dp[mask][k];最后在k 走过状态mask

注意重边


#include <iostream>
#include <cstring>
#include <cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)
using namespace std;
const int mp=109;
const int oo=0x3f3f3f3f;
int g[mp][mp],dis[mp][mp],C[mp],D[mp],id[mp];
int dp[ ll(16) ][16];
int n,m,money;

int main()
{
int cas,a,b,c,H;
while(~scanf("%d",&cas))
{
while(cas--)
{
clr(g,0x3f);
scanf("%d%d%d",&n,&m,&money);
FOR(i,1,m)
{
scanf("%d%d%d",&a,&b,&c);
g[a][b]=g[b][a]=min(g[a][b],c);
}

scanf("%d",&H);
FOR(i,0,H-1)
{
scanf("%d%d%d",&id[i],&C[i],&D[i]);
}

clr(dis,0x3f);
FOR(i,1,n)
g[i][i]=0;
FOR(i,1,n)FOR(j,1,n)FOR(k,1,n)
{
g[j][k]=min(g[j][k],g[j][i]+g[i][k]);
}

clr(dp,-1);
int zzz = ll(H)-1,then;
FOR(i,0,H-1)
if(money >= g[1][ id[i] ] + D[i])
dp[ ll(i) ][i] = money - g[1][ id[i] ] - D[i] + C[i];

FOR(i,1,zzz)FOR(j,0,H-1)FOR(k,0,H-1)
if(j != k && (i&ll(k)) && (i&ll(j)) && dp[ i^ll(j) ][k] != -1)
{
then = i^ll(j);
if(dp[then][k] >= g[ id[k] ][ id[j] ] + D[j])
dp[i][j] = max(dp[i][j],dp[then][k] - g[ id[k] ][ id[j] ] - D[j] + C[j]);
}

bool flag=0;
FOR(i,0,H-1)
if(dp[zzz][i]>=g[ id[i] ][1])
flag=1;
if(flag)puts("YES");
else puts("NO");
}
}
return 0;
}






标签:city,hdu,int,money,Travel,DP,id,dp,she
From: https://blog.51cto.com/u_15953788/6083323

相关文章

  • hdu 2608 0 or 1(数论)
    0or1TimeLimit:6000/2000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):1659    AcceptedSubmission(s):418Pro......
  • HDU 2888 Check Corners (二维RMQ,3级)
    A-CheckCornersCrawlinginprocess...CrawlingfailedTimeLimit:10000MS    MemoryLimit:32768KB    64bitIOFormat:%I64d&%I64u​​Submit......
  • 【微元贡献法 & 连续段 dp】「JOI Open 2016」摩天大楼 - 题解
    「JOIOpen2016」摩天大楼-题解注意到绝对值求和的形式,比较自然地想到将权值\(a\)排序以规避绝对值,下文默认\(a\)按从小到大排序,且插入元素的顺序亦为从小到大。......
  • k8s部署wordpress
    nginxnginx.confserver{listen80;server_namelocalhost;location/{root/apps/nginx/wordpress;indexindex.phpind......
  • TCP和UDP的区别及使用场景
    一、TCP和UDP是什么?   TCP:   传输控制协议(TCP,TransmissionControlProtocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC793定义。   ......
  • m基于LDPC+QPSK通信链路误码率matlab仿真
    1.算法描述       LDPC(Low-densityParity-check,低密度奇偶校验)码是由Gallager在1963年提出的一类具有稀疏校验矩阵的线性分组码(linearblockcodes),然而......
  • ARC121F Logical Operations on Tree【DP】
    给定一棵树,给每个点填\(0\)或\(1\),给每条边填\(\text{AND}\)或\(\text{OR}\),求有多少种填法满足存在一种缩边的顺序,使得每次把一条边的两个端点缩成一个点,权为原端点......
  • 基于MATLAB的LDPC编译码误码率仿真,仿真调制为64QAM,对比不同译码迭代次数
    1.算法描述LDPC码是麻省理工学院RobertGallager于1963年在博士论文中提出的一种具有稀疏校验矩阵的分组纠错码。几乎适用于所有的信道,因此成为编码界近年来的研究热点。它......
  • UDP协议
    UDP协议概述UDP(UserDatagramProtocol)协议和TCP协议都是传输层协议,UDP仅在IP数据报的基础上增加了两个基本的服务:复用和分用以及差错检测。UDP的优点如下:UDP无需建......
  • ESP8266配置UDP数据传输
    1.ESP8266简介   ESP8266是一款高性能的WIFI串口模块,内部集成MCU能实现单片机之间串口通信,是目前使用最广泛的一种WIFI模块之一。可以简单理解为一个WIFI转串口的设备......