*【HDU - 5711】Ingress(tsp旅行商问题,优先队列贪心,状压dp,floyd最短路,图论)

题干:

Brickgao, who profited from your accurate calculating last year, made a great deal of money by moving bricks. Now he became ``gay shy fool'' again and recently he bought an iphone and was deeply addicted into a cellphone game called Ingress. Now he is faced with a problem so he turns to you for help again. We make some slight modifications based on the original rules, so please draw attention to the details below. 

There are NN portals (indexed from 1 to NN) around Brickgao's home, and he can get some substances called XM by hacking the portals. It's known that for each portal ii, he can get AiAi XM during the first hack, and after each hack, the amount of XM he will get during the next hack will decrease by BiBi. If the amount of XM he can get is less than or equal to zero, Brickgao can't get XM from that portal anymore. For the ii-th portal, if Ai=10,Bi=2Ai=10,Bi=2 and he hacks 3 times, he will get 10, 8, 6 XM during each hack. 

There are MM bidirectional roads between some pairs of portals and between Brickgao's home and some portals. Now he is eager to start his Ingress journey from home and finally return home, but due to the extremely hot weather, Brickgao will feel sick when you hack more than KK times or the distance he covers is more than LL. So how much XM he can get at most during this journey? 
 

Input

The first line contains a single integer T(T≤20)T(T≤20), indicating the number of test cases. 

The first line of each case are four integers N(1≤N≤16),M(0≤M≤N(N+1)2),K(1≤K≤50)N(1≤N≤16),M(0≤M≤N(N+1)2),K(1≤K≤50) and L(2≤L≤2000)L(2≤L≤2000). 
The second line of each case contains NN non-negative integers where the ii-th denotes Ai(Ai≤500)Ai(Ai≤500). 
The third line of each case contains NN non-negative integers where the ii-th denotes Bi(Bi≤50)Bi(Bi≤50). 
Each of next MM line contains 3 non-negative integers u,v(0≤u,v≤n)u,v(0≤u,v≤n) and c(0≤c≤1000)c(0≤c≤1000) , denotes that there is a road with the length of cc between the uu-th and the vv-th portal. If uu or vv equals to 0, it means Brickgao's home. 

Output

For each test case, output the case number first, then the amount of maximum XM Brickgao can get. 

Sample Input

2
1 1 3 2
5
3
0 1 1
3 6 3 5
10 7 5
2 3 1
0 1 3
0 2 1
0 3 1
1 2 2
2 3 3
1 3 4

Sample Output

Case 1: 7
Case 2: 16

题目大意:

n(<=16)个点(其实还有个0点为home),m(<=n^2)条双向边和边权(代表两点之间距离<=1000),K(<=50)次hack机会,最远走L(<=2000)路程

每个点的初始点权为a[i](<=500),每个点每hack一次点权下降b[i](<=50)(变为负则当然就不选啦)

对于每个点,可以只经过不hack,也可以到这个点之后一次性hack多次。

一个人从home出发,最多hackK次,问在路程不超过L的情况下,可以获得的最大价值。

解题报告:

因为可以只经过不hack,所以先floyd跑一边最短路(套路了),然后不难发现可以认为:走过一个点之后不会再走回来,或者说,选择了这个点hack了,就不会回来再hack这个点(因为还要多走距离,何必呢)(emmm其实上面说走过一个点之后不会再走回来不太合适,因为毕竟因为最短路,所以可能还是要回来的(因为这一点是最短路需要经过的点嘛))这样我们只需要记录状态:走过了哪些点,当前在哪个点上,就可以跑(2^n * n)的dp了。
dp[sta][i]表示目前经过点的状态集合为i,当前所在点的位置为 i 的最小路径(当然要加上返程到0点的路径)
这是典型的旅行商问题。
由此得出所有可以到达的合法点集,其状态用2进制表示为sta。

对于每一个状态sta,只要dp[sta][i]有一个i符合<=L,那么称sta是合法状态。
然后依次枚举所有的合法sta,
对于每一个合法sta的所有点权,用贪心原则,用优先队列选取最大的权值。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 22;
int dis[MAX][MAX],dp[1<<17][17];
int a[MAX],b[MAX];
int n,m,k,L;
int up;
bool ok[1<<17];
void floyd() {for(int k = 0; k<=n; k++) {for(int i = 0; i<=n; i++) {for(int j = 0; j<=n; j++) {dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]);}}}
}
int tsp() {int ans = 0;for(int sta = 0; sta < up; sta++) ok[sta] = 0;memset(dp,0x3f,sizeof dp);dp[1][0]=0;for(int sta = 0; sta < up; sta++) {for(int i = 0; i<=n; i++) {if(dp[sta][i] <= L) {if(dp[sta][i] + dis[i][0] <= L) ok[sta] = 1;for(int j = 1; j<=n; j++) {dp[sta|(1<<j)][j] = min(dp[sta|1<<j][j], dp[sta][i] + dis[i][j]);}}}if(ok[sta]) {priority_queue<PII> pq;for(int i = 1; i<=n; i++) {if((sta&(1<<i) )!= 0) pq.push(pm(a[i],b[i]));}int tmp = 0;for(int i = 1; i<=k; i++) {if(pq.empty()) break;PII cur = pq.top();pq.pop();tmp += cur.F;cur.F -= cur.S;if(cur.F > 0) pq.push(pm(cur.F,cur.S)); }ans = max(ans,tmp);}}return ans;
}int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d%d%d%d",&n,&m,&k,&L);up = (1<<(n+1));memset(dis,0x3f,sizeof dis);for(int i = 0; i<=n; i++) dis[i][i]=0;for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=n; i++) scanf("%d",b+i);for(int q,w,e,i = 1; i<=m; i++) {scanf("%d%d%d",&q,&w,&e);if(e < dis[q][w]) dis[q][w]=dis[w][q]=e;}floyd();printf("Case %d: %d\n",++iCase,tsp());} return 0 ;
}

贴另一个做法:https://blog.csdn.net/qq_37025443/article/details/83513413

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440325.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ajax get请求成功,成功()函数的AJAX GET请求

后不叫我有一个jQuery的AJAX脚本像下面&#xff1a;成功()函数的AJAX GET请求function FillCity() {var stateID $("#ddlState").val();$.ajax({url: Url.Action("Employee", "Index"),type: "GET",dataType: "json",data:…

《TCP/IP详解》学习笔记(二):数据链路层

数据链路层有三个目的&#xff1a; 为IP模块发送和 接收IP数据报。为ARP模块发送ARP请求和接收ARP应答。为RARP发送RARP请 求和接收RARP应答ip大家都听说过。至于ARP和RARP&#xff0c;ARP叫做地址解析协议&#xff0c;是用IP地址换MAC地址的一种协议&#xff0c;而RARP则叫…

【POJ - 2762】Going from u to v or from v to u?(Tarjan缩点,树形dp 或 拓扑排序,欧拉图相关)

题干&#xff1a; In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the o…

《TCP/IP详解》学习笔记(三):IP协议、ARP协议

把这三个协议放到一起学习是因为这三个协议处于同一层,ARP 协议用来找到目标主机的 Ethernet 网卡 Mac 地址,IP 则承载要发 送的消息。数据链路层可以从 ARP 得到数据的传送信息,而从 IP 得到要传输的数据信息。 IP 协议 IP 协议是 TCP/IP 协议的核心,所有的 TCP,UDP,IMCP,IGCP…

光与夜之恋服务器维护中,光与夜之恋7月16日停服维护说明 维护详情一览

光与夜之恋7月16日停服维护说明维护详情一览。光与夜之恋7月16日停服维护更新了哪些内容?我们去了解一下。【7月16日停服维护说明】亲爱的设计师&#xff1a;为了给设计师们提供更好的游戏体验&#xff0c;光启市将于7月16日(周五)00:00进行预计5小时的停服维护&#xff0c;可…

10.Partial Dependence Plots

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将学习如何创建和解释部分依赖图&#xff0c;这是从模型中提取洞察力的最有价值的方法之一。 What Are Partial Dependence Plots 有人抱怨机器学习模型是黑盒子。这些人会争辩说我们无法看到这些模型如何处理任何给定的数据…

springboot监控服务器信息,面试官:聊一聊SpringBoot服务监控机制

目录前言任何一个服务如果没有监控&#xff0c;那就是两眼一抹黑&#xff0c;无法知道当前服务的运行情况&#xff0c;也就无法对可能出现的异常状况进行很好的处理&#xff0c;所以对任意一个服务来说&#xff0c;监控都是必不可少的。就目前而言&#xff0c;大部分微服务应用…

0.《Apollo自动驾驶工程师技能图谱》

【新年礼物】开工第一天&#xff0c;送你一份自动驾驶工程师技能图谱&#xff01; 布道团队 Apollo开发者社区 1月 2日 AI时代到来&#xff0c;人才的缺乏是阻碍行业大步发展的主要因素之一。Apollo平台发布以来&#xff0c;我们接触到非常多的开发者他们并不是专业自动驾驶领…

【HDU - 1116】【POJ - 1386】Play on Words(判断半欧拉图,欧拉通路)

题干&#xff1a; Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large number…

11.Pipelines

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将了解如何以及为何使用管道清理建模代码。 What Are Pipelines 管道是保持数据处理和建模代码有序的简单方法。 具体来说&#xff0c;管道捆绑了预处理和建模步骤&#xff0c;因此您可以像使用单个包一样使用整个捆绑包。…

ubuntu服务器创建共享文件夹,Ubuntu samba安装创建共享目录及使用

Ubuntu samba更新了很多版本更新&#xff0c;我本人认为Ubuntu samba是很好使的文件系统&#xff0c;在此向大家推荐。如今技术不断更新&#xff0c;各种使用文件都已经淘汰。我认为还是有很不错的如Ubuntu samba值得大家来运用。一. Ubuntu samba的安装:sudo apt-get insall s…

【POJ - 2337】Catenyms(欧拉图相关,欧拉通路输出路径,tricks)

题干&#xff1a; A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms: dog.gophergopher.ratrat.tigeraloha.alohaarachnid.dog A…

12.Cross-Validation

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将学习如何使用交叉验证来更好地衡量模型性能。 What is Cross Validation 机器学习是一个迭代过程。 您将面临关于要使用的预测变量&#xff0c;要使用的模型类型&#xff0c;提供这些模型的参数等的选择。我们通过测量各…

服务器不显示u盘,服务器不读u盘启动

服务器不读u盘启动 内容精选换一换介绍使用Atlas 200 DK前需要准备的配件及开发服务器。Atlas 200 DK使用需要用户提前自购如表1所示配件。准备一个操作系统为Ubuntu X86架构的服务器&#xff0c;用途如下&#xff1a;为Atlas 200 DK制作SD卡启动盘。读卡器或者Atlas 200 DK会通…

【FZU - 2039】Pets(二分图匹配,水题)

题干&#xff1a; 有n个人&#xff0c;m条狗&#xff0c;然后会给出有一些人不喜欢一些狗就不会购买&#xff0c;问最多能卖多少狗。。 Input There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the fir…

Leetcode刷题实战(1):Two Sum

Leetcode不需要过多介绍了&#xff0c;今天一边开始刷题一边开始总结&#xff1a; 官网链接如下&#xff1a;https://leetcode.com/problemset/all/ 题1描述&#xff1a; 1Two Sum38.80%Easy Given an array of integers, return indices of the two numbers such that they…

信息服务器为什么选择在贵州,为啥云服务器在贵州

为啥云服务器在贵州 内容精选换一换当用户已在ECS服务购买GPU加速型云服务器&#xff0c;并且想在该云服务器上运行应用时&#xff0c;可以通过纳管的方式将该云服务器纳入VR云渲游平台管理。登录控制台&#xff0c;在服务列表中选择“计算 > VR云渲游平台”。在左侧导航栏&…

LeetCode刷题实战(2):Add Two Numbers

题2描述&#xff1a; 2Add Two Numbers29.10%Medium You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a…

【BZOJ - 1305】dance跳舞(拆点网络流,建图,最大流,残留网络上跑最大流)

题干&#xff1a; 一次舞会有n个男孩和n个女孩。每首曲子开始时&#xff0c;所有男孩和女孩恰好配成n对跳交谊舞。每个男孩都不会和同一个女孩跳两首&#xff08;或更多&#xff09;舞曲。有一些男孩女孩相互喜欢&#xff0c;而其他相互不喜欢&#xff08;不会“单向喜欢”&am…

《TCP/IP详解》学习笔记(四):ICMP 协议、ping 和 Traceroute

ICMP 协议介绍 前面讲到了&#xff0c;IP 协议并不是一个可靠的协议&#xff0c;它不保证数据被成功送达&#xff0c;那么自然的&#xff0c;保证数据送达的工作应该由其他的模块来完 成。其中一个重要的模块就是 ICMP(网络控制报文)协议。 当传送 IP 数据包发生错误--比如主机…