首页 > 其他分享 >Victor and World(状压DP)

Victor and World(状压DP)

时间:2024-08-22 16:03:57浏览次数:8  
标签:typedef int country 状压 leq dp World DP Victor

题目描述
After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are \(n\) countries on the earth, which are numbered from \(1\) to \(n\). They are connected by \(m\) undirected flights, detailedly the \(i\)-th flight connects the \(u_i\)-th and the \(v_i\)-th country, and it will cost Victor's airplane \(w_i\) L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.

Victor now is at the country whose number is \(1\), he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country.
输入
The first line of the input contains an integer \(T\), denoting the number of test cases.
In every test case, there are two integers \(n\) and \(m\) in the first line, denoting the number of the countries and the number of the flights.

Then there are \(m\) lines, each line contains three integers \(u_i\), \(v_i\) and \(w_i\), describing a flight.

\(1\leq T\leq 20\).

\(1\leq n\leq 16\).

\(1\leq m\leq 100000\).

\(1\leq w_i\leq 100\).

\(1\leq u_i, v_i \leq n\).
输出
Your program should print \(T\) lines : the \(i\)-th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.

很明显的状压DP,注意初始化dp,f即可

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
const int N = 16,INF = 0x3f3f3f3f;
int dp[N][N];
int f[1<<N][N];
void solve()
{
	memset(dp,0x3f,sizeof dp);
	memset(f,0x3f,sizeof f);
	int n,m;
	cin>>n>>m;
	//状压时注意标号
	for(int i=0;i<n;++i) dp[i][i] = 0;
	for(int i=0;i<m;++i)
    {
    	int u,v,w;
    	cin>>u>>v>>w;
    	u--, v--;
    	dp[u][v] = min(dp[u][v],w);
    	dp[v][u] = min(dp[v][u],w);
    }
   
	for(int k=0;k<n;++k)
     for(int i=0;i<n;++i)
      for(int j=0;j<n;++j)
	    if(dp[i][k]!=INF&&dp[k][j]!=INF)
	    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
	
	f[1][0] = 0;
	for(int S=1;S<(1<<n);S++)
	{
		for(int i=0;i<n;++i)
		{
			if(!(S>>i&1)) continue;
			for(int j=0;j<n;++j)
			{
				if(S>>j&1) continue;
				f[S|(1<<j)][j] = min(f[S|(1<<j)][j],f[S][i]+dp[i][j]);
			}
		}
	}
	int ans = INF;

	for(int i=0;i<n;++i) ans = min(ans,f[(1<<n)-1][i] + dp[i][0]);
	cout<<ans<<'\n';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		solve();
	}
}

标签:typedef,int,country,状压,leq,dp,World,DP,Victor
From: https://www.cnblogs.com/ruoye123456/p/18374101

相关文章

  • 浅谈TCP和UDP协议的区别
    **传输模式**TCP协议:数据流(DataStream)--没有消息边界,比如服务端给客户端发来2048字节大小的数据,而客户端设置的一次最大接收大小为1024,这时候就意味着还有1024没能接收过来,要再接收一次。所以容易出现粘包的情况。所谓粘包,就是数据都粘在一起了。UDP协议:数据报(Da......
  • 网络编程UDP、TCP
    1UDP通信客户端UDPClientpublicclassUDPClient{publicstaticvoidmain(String[]args)throwsIOException{//获取本地服务器地址InetAddressserver_address=InetAddress.getLocalHost();//创建数据报套接字以连接到服务器......
  • DP斜率优化学习笔记
    最后一次修改:2024.7.1614:39P.MBy哈哈铭简介“斜率优化”顾名思义就是用斜率进行优化,让\(DP\)的时间复杂度更优。一般情况下,将动态转移方程化简后得到这样的关系式:\[\frac{y_1-y_2}{x_1-x_2}\leqK\]然后通过该式进行转移,以达到优化时间复杂度的目的。小tip:推公式前......
  • 服务器主机wordpress多网站启用redis缓存数据“混乱”解决办法
    近两天在搞网站数据迁移搬家的事情,是将A网站做为B网站的一个子目录,这样就牵涉到一个服务器两个网站的问题,因为这两个wordpress网站都使用了redis缓存,但在建站之初并没有设定不同的数据表前缀,后期修改我也不懂,直接导致了因为redis缓存两个网站数据“混乱”的问题。但好在网络......
  • AcWing 1078. 旅游规划 (DFS找树的直径+直径中点性质求解,无DP)
    原题链接题目描述算法引用自树的直径-OI-Wiki:若树上所有边边权均为正,则树的所有直径中点重合证明:使用反证法。设两条中点不重合的直径分别为\(\delta(s,t)与\delta(s',t')\),中点分别为\(x\)与\(x'\)。显然,\(\delta(s,x)=\delta(x,t)=\delta(s',x')=\delta(......
  • NPDP|产品管理做的好不好,客户觉得很好用就对了。
    在当今这个竞争激烈的市场环境中,产品管理作为企业战略与消费者需求之间的桥梁,其重要性不言而喻。一句简洁而深刻的话——“产品管理做的好不好,客户觉得很好用就对了”,不仅道出了产品管理的核心要义,也揭示了企业在追求商业成功时不可或缺的真理。产品管理:以用户为中心的艺术与......
  • Wordpress漏洞
    WPScanWPScan是KaliLinux默认自带针对wordpress的一款扫描神器1、刺探基础信息:wpscan--urlhttp://www.example.com2、猜解后台用户名wpscan--urlhttp://www.example.com--enumerateu3、使用字典暴破用户名admin的密码wpscan--urlhttp://www.example.com-Pp......
  • TCP,UDP,Socket,Http网络编程面试题 47道
    1.什么是网络编程        1.网络编程的本质是多台计算机之间的数据交换。数据传递本身没有多大的难度,不就是把一个设备中的数据发送给其他设备,然后接受另外一个设备反馈的数据。现在的网络编程基本上都是基于请求/响应方式的,也就是一个设备发送请求数据给另......
  • 高密度计算 – 400G超大容量DPI解决方案
    高密度计算–400G超大容量DPI解决方案FPGA硬件加速| 单卡400G流量卸载| 通用x86环境| DPI硬件加速 架构困境:性能的天花板触手可及在过去的二十多年中的大部分时间里,处理器性能以每年大约55%速度快速提升,而内存性能的提升速度却只有每年10%左右。早在1994年,计算机......
  • 线性DP P1020 [NOIP1999 提高组] 导弹拦截
    前置:二分查找,最长单调不升子序列,最长单调不降子序列(dilworth)。题解:可以用来练习手写二分,二分优化的最长上升子序列时间复杂度O(nlogn)。但是坑是非常多的。代码:#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e5+10;inta[N],n,......