跳到正文

题解

究极乱搞

Womind约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年10月28日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 3 分钟

版本

  1. v1当前

沃是用:

代码
dp[i][0] 表示坐一直汽车,从1到i最短的路
dp[i][1] 表示坐过火车,从1到i最短的路

坐火车后只能坐火车,所以:

代码
dp[j][1] = min(dp[j][1], min(dp[i][0], dp[i][1]) + a[i][j] * b1 + c1); // 状态可以储存 
dp[j][0] = min(dp[j][0], dp[i][0] + a[i][j] * a1);

最多只能转 n 次车,即每个城市最多只能经过一次,所以可以从 1 开始按连通性遍历。

代码
rep(i,1,n) {
	rep(j,1,n) {
		if(a[i][j]) {
			dp[j][1] = min(dp[j][1], min(dp[i][0], dp[i][1]) + a[i][j] * b1 + c1); // 状态可以储存 
			dp[j][0] = min(dp[j][0], dp[i][0] + a[i][j] * a1);
		}
	}
}

但我一提交错了,

我白丝不得其解,但我发现时间只跑了 6ms,dp 松弛不可能错,只有可能是松弛少了,所以我加了 30 层循环,但时间仿佛在告诉我:上强度!

于是我循环了 1000 次(大雾。

然后一秒多过了。

C++
/*
    Name:
    Copyright:
    Author: 不要和 std 命名空间冲突!
    Date: 2023/10/27 19:40:21
    Description:
*/
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b)  for (int i = (a); i <= (b); i++)
#define repe(i,a,b) for (int i = (a); i >= (b); i--)
#define int long long
inline int read() {
	int ans = 0, f = 1; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -f;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) ans = (ans << 3) + (ans << 1) + ch - '0';
	return ans * f;
}
const int N = 1e3 + 1;
int a[N][N], dp[N][2], n, t, m;
bool vis[N];
signed main(){
		n = read(); int a1 = read(), b1 = read(), c1 = read();
		rep(i,1,n) 
			rep(j,1,n) a[i][j] = read();
		memset(dp,0x3f,sizeof(dp));
		dp[1][1] = dp[1][0] = 0;
		int cnt = 1000;
		while(cnt--) {
			rep(i,1,n) {
				rep(j,1,n) {
					if(a[i][j]) {
						dp[j][1] = min(dp[j][1], min(dp[i][0], dp[i][1]) + a[i][j] * b1 + c1); // 状态可以储存 
						dp[j][0] = min(dp[j][0], dp[i][0] + a[i][j] * a1);
					}
				}
			}		
		}

		cout << min(dp[n][0], dp[n][1]);
}

我一直在想为什么会松弛不够,是我每个城市只经过一次的结论是错的吗?虽然这篇题解有误导性,但不失为一种考场应试的能力体现,希望审核能放过。

祝我 noip 能取得好成绩(最后一年了呜呜呜。

当然,你也一样QwQ!

原文