UVA 116 Unidirectional TSP 经典dp题

题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小。

用dp记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路。

因为左边到右边的字典序最小就必须从左边开始找,于是我们可以换个思路,dp时从右边走到左边,这样寻找路径就可以从左向右了。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        uva116.cpp
*  Create Date: 2013-09-20 20:56:07
*  Descripton:  dp, memorial 
*/

#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 102;
int dis[MAXN][MAXN], map[MAXN][MAXN], n, m;

int cg(int x) {
	if (x == 0) x = n;
	else if (x == n + 1) x = 1;
	return x;
}

int dp(int x, int y) {
	x = cg(x);
	if (dis[x][y] != -0xffffff) return dis[x][y];
	return dis[x][y] = map[x][y] + min(min(dp(x - 1, y + 1), dp(x, y + 1)), dp(x + 1, y + 1));
}

void print(int x, int y) {
	if (y < m)
		printf("%d ", x);
	else {
		printf("%d\n", x);
		return;
	}
	int a[3] = {cg(x - 1), cg(x), cg(x + 1)};
	sort(a, a + 3);
	int tt = dis[x][y] - map[x][y];
	if (tt == dis[a[0]][y + 1])
		print(a[0], y + 1);
	else if (tt == dis[a[1]][y + 1])
		print(a[1], y + 1);
	else
		print(a[2], y + 1);
}
int main() {
	while (scanf("%d%d", &n, &m) != EOF) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				scanf("%d", &map[i][j]);
				if (j == m) dis[i][j] = map[i][j];
				else dis[i][j] = -0xffffff;
			}
		int Min = 0xffffff, t, rx, ry;
		for (int i = 1; i <= n; i++) {
			t = dp(i, 1);
			if (t < Min)
				rx = i, Min = t;
		}
		print(rx, 1);
		printf("%d\n", Min);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
最新发布
05-28

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值