Hie with the Pie POJ - 3311 状压DP

本文介绍了一种解决比萨速递路径优化问题的方法,利用Floyd算法预处理点间最短路径,再通过状态压缩DP求解TSP问题,找到从比萨店出发并返回的最短回路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7637 Accepted: 4115

Description

The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you to write a program to help him.

Input

Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.

Output

For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.

Sample Input

3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

8


Floyd + 状态压缩DP  
题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最短  
也就是TSP(旅行商)问题,首先不难想到用FLOYD先求出任意2点的距离dis[i][j]  
接着枚举所有状态,用11位二进制表示10个城市和pizza店,1表示经过,0表示没有经过  

定义状态DP(S,i)表示在S状态下,到达城市I的最优值  
接着状态转移方程:DP(S,i) = min{DP(S^(1<<i-1),j) + dis[j][i],DP(S,i)},
    其中S^(1<<i-1)表示未到达城市i的所有状态  
对于全1的状态,即S = (1<<n)-1则表示经过所有城市的状态,最终还需要回到PIZZA店0  
那么最终答案就是min{DP(S,i) + dis[i][0]} 


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 5;

int dp[1<<11][12];
int dis[12][12];
int main(void)
{
//	freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
	int n, i, j, k, ans, S;
	while (cin >> n && n)
	{
		for (i = 0; i <= n; i++)
			for (j = 0; j <= n; j++)
				cin >> dis[i][j];
		for (k = 0; k <= n; k++)
			for (i = 0; i <= n; i++)
				for (j = 0; j <= n; j++)
					dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
					
		for (S = 0; S < (1<<n); S++){ // 枚举所有状态 用位运算表示 
			for (i = 1; i <= n; i++){
				if (!(S & (1<<(i-1)))) // 如果当前状态中没有i 
					continue;			//  说明当前状态没有经过i城市 
				if (S == (1<<(i-1)))
					dp[S][i] = dis[0][i];
				// 状态S只经过城市I,最优解自然是从0出发到i的dis,这也是DP的边界 
				else {
					dp[S][i] = INF;
					for (j = 1; j <= n; j++){
						if (S & (1<<(j-1)) && j != i)
							dp[S][i] = min(dp[S][i], dp[S^(1<<(i-1))][j] + dis[j][i]);
							//在没经过城市I的所有状态中,寻找合适的中间点J使得距离更短,和FLOYD一样
							// 异或运算 ^   S^(1<<(i-1)) 作用是在状态中去掉i城市
							//  			S|(1<<(i-1)) 作用是在状态中加上i城市 
					}
				}
			}
		}
		ans = INF;
		for (i = 1; i <= n; i++) // 找出最小的 
			ans = min(ans, dp[(1<<n)-1][i] + dis[i][0]);
		cout << ans << endl;
	}
	
	return 0;
}





### HIE-FDTD 方法概述 混合隐式显式有限差分时域 (Hybrid Implicit Explicit Finite-Difference Time-Domain, HIE-FDTD) 是一种用于计算电磁场传播的技术,特别适用于处理具有高对比度材料特性的复杂结构。该方法通过引入隐式和显式的离散化方案来提高数值稳定性并减少计算资源消耗[^1]。 ### 数学模型与算法原理 HIE-FDTD 的核心在于将传统的 FDTD 显式更新公式与 Crank-Nicolson 隐式求解器相结合。对于电导率较高的区域采用隐式格式,在低损耗介质部分则继续沿用标准FDTD的显式迭代方式: \[ \frac{E^{n+1}-E^n}{\Delta t}=\mu^{-1}\left(\nabla\times H+\sigma E^{(n+1)+E^n}/2\right)\] 其中 \(E\) 表示电场强度矢量;\(H\) 代表磁场强度矢量;\(\mu,\epsilon,\sigma\) 分别对应磁导率、介电常数以及电导率参数[\^2]. ### 技术实现要点 为了有效实施 HIE-FDTD 算法,需注意以下几个方面: - **网格划分**: 对于不同性质(如金属/绝缘体界面)处应适当加密单元格尺寸; - **边界条件设置**: 可选用吸收边界条件(PML),以降低反射误差影响仿真精度; - **时间步长选取**: 应满足 Courant–Friedrichs–Lewy(CFL)稳定准则的同时尽可能大些以便加速收敛过程. ```matlab % 初始化参数 dx = dy = dz = 0.01; % 空间分辨率 dt = dx/(sqrt(mu*eps)*c); % 时间间隔遵循CFL条件限制 Nt = round(T/dt); % 总的时间步数 for n=1:Nt update_H_field(); % 更新半整时刻磁场分布 apply_implicit_scheme_to_conductive_regions(); % 处理传导区内的电场变化 explicit_update_E_field_outside_conductors(); % 直接推进自由空间中的电波演化 end ``` ### 实际应用场景举例 此技术广泛应用于微波工程领域内天线设计优化、高速互连建模分析等方面。例如,在研究印刷电路板上信号传输特性时,可以利用 HIE-FDTD 来模拟高频电流如何穿过多层基片,并评估由此产生的串扰效应等问题[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值