DAG上的最长最短路

è¿éåå¾çæè¿°

如果把当前所需要凑齐的面值设为一个状态量的话,那么这个题就是由S到0(一文钱没有凑够和完全凑够的两个端点)的一个DAG(因为钱不能取回),仍然是使用记忆化搜索对这个图进行遍历即可。

在搜索时,可以有两个方式标记已经被访问过的状态:初始DP为-1(无论是什么数都要和题目中可能出现的状态量区分开),或者是用VIS数组标记这个状态。

在搜索开始之前也需要注意一些初始状态的设定,比如这里,如果一分钱不用凑(或者说是终点状态),那就是dp[0]=0,如果用vis标记的话还需要有一个vis[0]=1,这都代表这个这个状态量是被访问过的(也意味着这是终点),在此可以直接退出递归得到答案。

#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
	ll tmp = 0, si = 1;
	char c;
	c = getchar();
	while (c > '9' || c < '0')
	{
		if (c == '-')
			si = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		tmp = tmp * 10 + c - '0';
		c = getchar();
	}
	return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
ll coins[500000], dp[500000];
bool vis[500000];
ll n, S;
ll dfs(ll current)
{
	if (vis[current])
		return dp[current];//vis标记
	else
	{
		ll &tmp = dp[current];
		vis[current] = 1;
		tmp = INF;//找最小值要初始化为最大,找最大值初始化为最小
		for (int i = 1; i <= n; i++)
			if (current >= coins[i])
				tmp = min(tmp, dfs(current - coins[i]) + 1);
		return tmp;
	}
}
void output(ll current)//字典序最小解
{
	for (int i = 1; i <= n; i++)
	{
		if (current >= coins[i] && dp[current] == dp[current - coins[i]] + 1)
		{
			output(current - coins[i]);
			break;
		}
	}
	cout << current << " ";
}
int DETERMINATION()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> coins[i];
	reset(vis, 0);
	cin >> S;
	dp[0] = 0;
	vis[0] = 1;//初始状态
	dfs(S);
	cout << dp[S] << endl;
	output(S);
	return 0;
}

递推版

#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
	ll tmp = 0, si = 1;
	char c;
	c = getchar();
	while (c > '9' || c < '0')
	{
		if (c == '-')
			si = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		tmp = tmp * 10 + c - '0';
		c = getchar();
	}
	return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
ll coins[500000], dp[500000];
ll n, S;
ll path[15555];
void output(ll current)
{
	if (current == 0)
		return;
	output(current - coins[path[current]]);
	cout << current << " ";
}
int DETERMINATION()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> coins[i];
	cin >> S;
	for (int i = 0; i <= S; i++)
		dp[i] = -INF;
	dp[0] = 0;
	for (int i = 0; i <= S; i++)
		for (int j = 1; j <= n; j++)
		{
			if (i >= coins[j])//如同在记忆化搜索里的判断,这是一个必须存在的模块
			{
				if (dp[i] < dp[i - coins[j]] + 1)
				{
					dp[i] = dp[i - coins[j]] + 1;
					path[i] = j;//记录路径
				}
			}
		}
	cout << dp[S] << endl;
	output(S);
	return 0;
}

 

posted @ 2019-07-29 08:50  完全墨染的樱花  阅读(163)  评论(0)    收藏  举报