HDU 3001 Travelling

转载注明出处:http://blog.youkuaiyun.com/u013480600/article/details/19832395       感谢大佬

After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
Input
There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
Sample Input
2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10
Sample Output
100
90
7 
 
 
//经典好题:三进制旅行商问题
/*
现在有一个具有n个顶点和m条边的无向图(每条边都有一个距离权值),
小明可以从任意的顶点出发,他想走过所有的顶点而且要求走的总距
离最小,并且他要求过程中走过任何一个点的次数不超过2次。
输入:多组实例。每个实例第一行为n(1<=n<=10)和m,接下来m行对m条
边描述,每行包括a,b(1<=a,b<=n)和c表示节点a和b之间有一条长c的路。
输出:输出他需要走的最短距离,如果不存在这样的路,输出-1.


如果本题没有说任意一个点走过的次数不超过2这个条件,那么
本题可以用类似TSP问题的解法即可,即用d[i][S]=x,表示当前
人在i点,并且走过了集合S中的所有点,所行走的最小距离为x。
d[i][S]=min{ d[j][S-i]+min_dist[j][i] } j,i属于S。但是
现在要求点出现的次数<=2,所以要把点出现的次数也纳入到d
的第二维下标(即S)中去。 令d[i][S]=x,表示当前人在i点,
并且走过了集合S(用S的三进制表示点出现次数的集合)中的
所有点以及对应点出现的次数满足S集合时,所行走的最小距离为x。
状态转移方程是:d[i][S]=min{ d[j][S1]+dist[j][i] } ,S与S1
的关系是S的三进制形式除了一位减一之外(从2变1或从1变0)
其他所有位都与S1的三进制数对应位相同,j在S1集合中出现的
次数属于[1,2],i在S1集合中出现的次数属于[0,1],dist[i][j]
表示节点i与j之间的初始距离(不是最小距离,如果从i到j
没有边则不能执行该状态转移)。初值为:d[i][(3^i)]=0,
其他为-1.(本题顶点的标号,题目说是1到n,我们程序中用
0到n-1处理。最终我们所求为:max{ d[i][S] },0<=i<=n-1。
S的三进制形式中没有一个0(最多2次是通过状态转移方程来
限制住的)。
*/ 
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;

int p[11];
int n, m, mn;
int dis[11][11];
int dp[11][59050];
int state[11][59050]; //表示状态S的第i位 
void init() { //初始化计算p[i] 为3的i次方, 
	p[0] = 1;
	for(int i = 1; i <= 10; i++) {
		p[i] = p[i-1]*3;
	}
	for(int i = 0; i <= p[10]; i++) {
		int t = i, k = 0;
		while(t) {
			state[k++][i] = t%3;
			t /= 3;
		}
	}
}
void init1() {
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j <= n; j++) {
			if(i != j) dis[i][j] = inf;
			else dis[i][j] = 0;
		}
	}
}
int main() {
	init();
	int a, b, c;
	while(~scanf("%d%d",&n,&m)) {
		init1();
		mn = inf;
		for(int i = 0; i < m; i++) {
			scanf("%d%d%d",&a,&b,&c);
			a--;b--;
			if(dis[a][b] > c)
			dis[a][b] = dis[b][a] = c;
		}
		memset(dp, inf, sizeof(dp));
		for(int i = 0; i < n; i++)
		dp[i][p[i]] = 0;//初始化dp,只有i访问,费用为0 
		for(int S = 0; S < p[n]; S++) {
			int sum = 1;//遍历的点记录 
			for(int i = 0; i < n; i++) {
				if(state[i][S] == 0) sum = 0;
				if(dp[i][S] == inf) continue;
				for(int k = 0; k < n; k++) {//枚举下一个点 
					if(i == k) continue;
					//表示不存在边,或者这种状态已经被访问两次以上 
					if(dis[i][k] == inf || state[k][S] >= 2)
					continue;
					int ans = S + p[k]; //表示此时状态 
					dp[k][ans] = min(dp[k][ans], dp[i][S] + dis[i][k]);
				}
			}
			if(sum) {
				for(int i = 0; i < n; i++) {
					mn = min(mn , dp[i][S]);
				}
			}
		}
		if(mn == inf) mn = -1;
		printf("%d\n",mn);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值