问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10

样例输出:

100
90
7

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[60000][11];
int cnt[11][11];
int three[12];
int m,n;
int f[60000][11];
void init()
{
three[1]=1;
for(int i=2;i<=11;i++)
three[i]=three[i-1]*3;
for(int i=0;i<three[11];i++)
{
int tmp=i;
for(int j=1;j<=10;j++)
{
f[i][j]=tmp%3;
tmp/=3;
}
}
}
int main()
{
init();
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0x7f,sizeof(dp));
memset(cnt,0x7f,sizeof(cnt));
int ans=dp[0][0];
int inf=ans;
int x,y,d;
while(m--)
{
scanf("%d%d%d",&x,&y,&d);
cnt[x][y]=cnt[y][x]=min(cnt[x][y],d);
}
for(int i=1;i<=n;i++)
dp[three[i]][i]=0;
for(int state=1;state<three[n+1];state++)
{
bool ok=1;
for(int i=1;i<=n;i++)
{
if(f[state][i]==0)
ok=0;
if(dp[state][i]==inf)
continue;
for(int j=1;j<=n;j++)
{
if(i==j)
continue;
if(f[state][j]==2)
continue;
if(cnt[i][j]==inf)
continue;
dp[state+three[j]][j]=min(dp[state+three[j]][j],dp[state][i]+cnt[i][j]);
}
}
if(ok)
{
for(int i=1;i<=n;i++)
{
ans=min(ans,dp[state][i]);
}
}
}
if(ans==inf)
ans=-1;
printf("%d\n",ans);
}
return 0;
}

【算法】DP解决旅行路径问题的更多相关文章

  1. 蓝桥杯 试题 算法提高 宰羊 DP解决

    问题描述 炫炫回了内蒙,肯定要吃羊肉啦,所有他家要宰羊吃. 炫炫家有N只羊,羊圈排成一排,标号1~N.炫炫每天吃掉一只羊(这食量!其实是放生啦),吃掉的羊的邻居会以为它被放生了,然后又会告诉他们的邻居 ...

  2. POJ 3264 RMQ问题 用dp解决

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...

  3. Manacher (马拉车) 算法:解决最长回文子串的利器

    最长回文子串 回文串就是原串和反转字符串相同的字符串.比如 aba,acca.前一个是奇数长度的回文串,后一个是偶数长度的回文串. 最长回文子串就是一个字符串的所有子串中,是回文串且长度最长的子串. ...

  4. 北京地铁站点遍历最少经站次数问题普遍意义上是一个NP问题,目前不存在多项式时间算法能够解决该问题

    http://www.cnblogs.com/jiel/p/5852591.html 众所周知求一个图的哈密顿回路是一个NPC问题: In the mathematical field of grap ...

  5. [转贴]C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) /* * 计算该日在本年中是第几天,注意闰年问题 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 * 特殊情况,闰年且输入月份大于3时 ...

  6. Java或web中解决所有路径问题

    Java开发中使用的路径,分为两种:绝对路径和相对路径.归根结底,Java本质上只能使用绝对路径来寻找资源.所有的相对路径寻找资源的方法,都不过是一些便利方法.不过是API在底层帮助我们构建了绝对路径 ...

  7. UVA1292-----Strategic game-----树形DP解决树上的最小点覆盖问题

    本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...

  8. 【C/C++】C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) /* * 计算该日在本年中是第几天,注意闰年问题 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 * 特殊情况,闰年且输入月份大于3时 ...

  9. request.getContextPath是为了解决相对路径的问题,可返回站点的根路径

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

随机推荐

  1. 齐博CMS:最常用的一些变量名,方便二次开发.必须要熟悉的

    ROOT_PATH程序所在硬盘目录的绝对地址,等同于以前的PHP168_PATH$webdb网站的全局变量,模块的全局变量也是这个$onlineip当前用户的IP$timestamp当前时间$WEBU ...

  2. 简单的jQ代码

    简单的jQ代码 /* * Lazy Load - jQuery plugin for lazy loading images * * Copyright (c) 2007-2012 Mika Tuup ...

  3. Django项目的创建与管理和pycharm与Github的秘密

    随笔 - 174  文章 - 21  评论 - 19 Django项目创建与管理   1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关 ...

  4. helm 更改为国内源

     helm init --upgrade -i slpcat/tiller:v2.8.2 --stable-repo-url https://kubernetes.oss-cn-hangzhou.al ...

  5. centos 7 下多网卡绑定+ vlan 网卡配置

    一.前言 CentOS7之前系统提供给用户的是bonding driver来实现链路聚合,实际上bonding适用于大多数应用.Bonding driver的架构是由内核空间完全控制.管理. Team ...

  6. 微信小程序编译包的获取与解压——在手机中获取小程序编译包wxapkg

    准备工作: 微信关注需要下载编译包的小程序,然后点进去看一下,微信就会自动下载相应的编译包到手机上了. 获取小程序编译包: 据说root手机可以直接在手机的文件管理中查找wxapkg文件,自己尝试了下 ...

  7. 【Linux 线程】线程同步《二》

    1.读写锁 与互斥量类似,但读写锁允许更高的并行性.其特性为:写独占,读共享. 读写锁状态: 一把读写锁具备三种状态: (1)读模式下加锁状态 (读锁) (2)写模式下加锁状态 (写锁) (3)不加锁 ...

  8. 使用python读写CSV文件

    # -*- coding:UTF-8 -*- __autor__ = 'zhouli' __date__ = '2018/10/25 21:14' import csv with open('resu ...

  9. swift4.2 打印devicetoken

    import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicati ...

  10. AttributeError: 'WebElement' object has no attribute 'send_keys'

    这个是没问题的代码:用来打开谷歌搜索cheese并退出 from selenium import webdriver from selenium.common.exceptions import Ti ...