【P2349 金字塔】题解


题目链接

观察数据范围发现边权都小于255,所以我们可以枚举最大边权。

对于每个最大边权,我们都在不大于这个边权的剩下的边里跑一次最短路。

最后再用最短路求出的答案+所枚举的最大边权=在这个最大边权下的答案。

Code

// Problem: P2349 金字塔
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2349
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define M 2010
//#define mo
#define N 110
struct node
{
	int x, y, z, n; 
}d[2*M]; 
int n, m, i, j, k; 
int sum=0x3f3f3f3f3f3f3f3f; 
int h[N], ans[N], c[N]; 
int u, v, w, g; 
queueq; 

int spfa(int mx)
{
	while(!q.empty()) q.pop(); 
	memset(ans, 0x3f, sizeof(ans)); 
	ans[1]=0; q.push(1); 
	while(!q.empty())
	{
		u=q.front(); q.pop(); 
		c[u]=0; 
		for(g=h[u]; g; g=d[g].n)
		{
			v=d[g].y; w=d[g].z; 
			if(w>mx) continue; 
			if(ans[u]+w