POJ - 3311 Hie with the Pie

1.题面

http://poj.org/problem?id=3311

2.题意

给你n个点,已知两两之间的距离,让你用最短的时间走完n个点,一个点可以走多次,求最短时间

3.思路

瞎写了一发,居然过了。。。。

4.代码

/*****************************************************************
    > File Name: Cpp_Acm.cpp
    > Author: Uncle_Sugar
    > Mail: uncle_sugar@qq.com
    > Created Time: 2016年07月28日 星期四 08时57分03秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;


template<class T>void PrintArray(T* first,T* last,char delim=' '){ 
	for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim); 
}

const int debug = 1;
const int size  = 10 + 10 ; 
const int INF = INT_MAX>>1;
typedef long long ll;


int n;
int dist[size][size];

void floyd(){
	for (int k=0;k<n;k++){
		for (int i=0;i<n;i++){
			for (int j=0;j<n;j++){
				dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
			}
		}
	}
}

int dp[20][1<<11];

int main()
{
	std::ios::sync_with_stdio(false);cin.tie(0);
	int i,j;
	while (cin >> n){
		if (n==0) break;
		n++;
		for (i=0;i<n;i++){
			for (j=0;j<n;j++){
				cin >> dist[i][j];
			}
		}
		floyd();
		for (i=0;i<20;i++){
			for (j=0;j<=(1<<n);j++){
				dp[i][j] = INF;
			}
		}
		dp[0][(1<<n)-1] = 0;
		for (int s=(1<<n);s>=0;s--){
			for (int u=0;u<n;u++){
				for (int v=0;v<n;v++){
					if (!(s&(1<<v)))
						dp[u][s] = min(dp[u][s],dp[v][s|(1<<v)]+dist[v][u]);
				}
			}
		}
		cout << dp[0][0] << endl;
	}
	return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值