信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn)
基础动态模型练习题集
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
int n, m, k, A, B, N, M, K, res, ok;
const int maxn = 1e3 + 10;
int MAP[maxn][maxn], dp[maxn];
vector<int>pre(maxn,-1);
void myans(int x)
{
if(pre[x] == -1){cout << x << " ";return;}
myans(pre[x]);
cout << x << " ";
}
void slove()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> MAP[i][j];
memset(dp,0x3f3f3f3f,sizeof(dp));
//定义状态dp[i] : 以起始点到i点的最短距离
dp[1] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i; j++)
if (MAP[j][i] != 0 && dp[i] > dp[j] + MAP[j][i])
{
pre[i] = j;
dp[i] = dp[j] + MAP[j][i];
}
}
cout << "minlong=" << dp[n] << endl;
myans(n);
}
int main()
{
cin.tie(0)->sync_with_stdio(false);
slove();
return 0;
}